using System;
using System.Data;
using System.Web.Mvc;
using System.Web.Routing;

using BrnShop.Core;
using BrnShop.Services;
using BrnShop.Web.Framework;
using BrnShop.Web.Models;

namespace BrnShop.Web.Controllers
{
    /// <summary>
    /// 商品评价控制器类
    /// </summary>
    public class ProductReviewController : BaseWebController
    {
        /// <summary>
        /// 列表
        /// </summary>
        public ActionResult List()
        {
            int pid = WebHelper.GetQueryInt("pid");
            int page = WebHelper.GetQueryInt("page");

            PageModel pageModel = new PageModel(10, page, ProductReviews.GetProductReviewCount(pid));
            ProductReviewListModel model = new ProductReviewListModel()
            {
                PageModel = pageModel,
                ProductReviewList = ProductReviews.GetProductReviewList(pid, pageModel.PageSize, pageModel.PageNumber)
            };

            return View(model);
        }

        /// <summary>
        /// 信息
        /// </summary>
        public ActionResult Info()
        {
            //商品评价id
            int reviewId = GetRouteInt("reviewId");
            if (reviewId == 0)
                reviewId = WebHelper.GetQueryInt("reviewId");
            //商品评价及其回复列表
            DataTable list = ProductReviews.GetProductReviewWithReplyById(reviewId);
            if (list.Rows.Count < 1)
                return PromptView("商品评价不存在");

            return View(list);
        }

        /// <summary>
        /// 回复
        /// </summary>
        public ActionResult Reply()
        {
            //不允许游客访问
            if (WorkContext.Uid < 1)
                return Content("nologin");

            int reviewId = WebHelper.GetFormInt("reviewId");
            string message = WebHelper.GetFormString("message");

            if (string.IsNullOrWhiteSpace(message))
                return Content("emptymessage");
            if (message.Length > 100)
                return Content("muchmessage");
            if (!SecureHelper.IsSafeSqlString(message))
                return Content("dangermessage");
            string bannedWord = FilterWords.GetWord(message);//禁止词
            if (bannedWord != "")
                return Content(bannedWord);

            ProductReviewInfo productReviewInfo = ProductReviews.GetProductReviewById(reviewId);
            if (productReviewInfo == null || productReviewInfo.ParentId != 0)
                return Content("noproductreview");

            ProductReviews.ReviewProduct(new ProductReviewInfo()
            {
                Pid = productReviewInfo.Pid,
                Uid = WorkContext.Uid,
                OPRId = productReviewInfo.OPRId,
                Oid = productReviewInfo.Oid,
                ParentId = productReviewInfo.ReviewId,
                State = 0,
                Star = 0,
                Quality = 0,
                Message = FilterWords.HideWords(message),
                ReviewTime = DateTime.Now,
                PayCredits = 0,
                PName = "",
                PShowImg = "",
                BuyTime = productReviewInfo.BuyTime,
                IP = WorkContext.IP
            });
            return Content("success");
        }
    }
}