using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils.ORM;
using PageAdmin.Utils;
using PageAdmin.Web.Areas.Member.Controllers.InfoData;
using PageAdmin.Web.Filter;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Member.Controllers
{
    [CheckTable(TableType = Common.TableTypeEnum.CustomInfoTable)]
    [NotCheckMenuItemPermissions]
    [CurrentSite]
    [CheckInfoPermissions] //检测信息发布权限
    public class InfoDataController : BaseController
    {

        private int _Uid = 0;
        private MvcControllerContext MvcContext { set; get; }
        public InfoDataController()
        {
            MvcContext = new MvcControllerContext();
            MvcContext.IncludeCustomField = true;
            MvcContext.IncludeAttachments = true;
            MvcContext.TableType = TableTypeEnum.CustomInfoTable;
        }

        private void Init()
        {
            MvcContext.Table = ViewBag.Table;
            MvcContext.From = FromEnum.Member;
            CurrentUser currentUser = ViewBag.CurrentUser;
            _Uid = currentUser.Uid;
            MvcContext.CurrentUser = currentUser;
        }


        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public virtual JsonResult PageListData()
        {
            Init();
            NameValueCollection form = new NameValueCollection();
            form.Add(Request.QueryString);
            MvcContext.Data = form;
            PageInfo pageInfo = MvcContext.PageInfo;
            GetListSet();
            IPageListBLL IPageListBll = new ListBLL();
            IEnumerable<dynamic> dataList = IPageListBll.PageList(MvcContext);
            return Json(dataList, pageInfo);
        }


        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }


        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Add(FormCollection form)
        {
            Init();
            MvcContext.Data = form;
            AddBLL addBll = new AddBLL();
            var tipsInfo = addBll.Add(MvcContext);
            int newId = MvcContext.DetailId;
            new InfoUrlSetBLL(MvcContext.Table).Set(MvcContext, newId); //设置Url映射表
            return base.Json(tipsInfo);
        }


        [HttpGet]
        public ActionResult Edit()
        {
            Init();
            MvcContext.Data = Request.QueryString;
            MvcContext.SqlCondition = "uid=@uid";
            MvcContext.DbContext.SetDbParamter("uid", _Uid);
            DetailBLL detailBLL = new DetailBLL();
            dynamic detailData = detailBLL.Detail(MvcContext);
            return View("Add", (object)JsonHelper.JsonParse(detailData));
        }




        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Edit(FormCollection form)
        {
            Init();
            MvcContext.Data = form;
            EditBLL editBll = new EditBLL();
            MvcContext.SqlCondition = "uid=@uid";
            MvcContext.DbContext.SetDbParamter("uid", _Uid);
            var tipsInfo = editBll.Edit(MvcContext);
            if(tipsInfo.State==1) //同时修改映射表
            {
                new InfoUrlSetBLL(MvcContext.Table).Set(MvcContext, MvcContext.DetailId); //设置Url映射表
            }
            return base.Json(tipsInfo);
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        [HttpPost]
        [AjaxOnly]
        public JsonResult Delete(FormCollection form)
        {
            Init();
            MvcContext.Data = form;
            MvcContext.SqlCondition = "uid=@uid";
            MvcContext.DbContext.SetDbParamter("uid", _Uid);
            DeleteBLL deleteBll = new DeleteBLL();
            var tipsInfo = deleteBll.Delete(MvcContext);
            if (tipsInfo.State == 1)
            {
                int infoId = MvcContext.DetailId;
                InfoDataBLL infoDataBLL = new InfoDataBLL(MvcContext.Table);
                infoDataBLL.DeleteCallBack(infoId);//放在bll层，方便其他页面共用
            }
            return base.Json(tipsInfo);
        }


        /// <summary>
        /// 获取列表筛选条件，页面设置等参数
        /// </summary>
        private void GetListSet()
        {
            string where = "uid=@uid";
            MvcContext.DbContext.SetDbParamter("@uid", _Uid);
            int state = StringHelper.Format<int>(Request.QueryString["state"]);
            if (!string.IsNullOrEmpty(Request.QueryString["state"]))
            {
                MvcContext.DbContext.AddDbParamter("@state", state);
                where += " and [state]=@state";
            }

            int columnId= StringHelper.Format<int>(Request.QueryString["columnId"]); //columnId转到过滤器中
            if (columnId> 0)
            {
                string columnIds = columnId.ToString() + "," + CommonDataHelper.GetCacheChildrenIds("pa_column", columnId);
                columnIds = StringHelper.Unique<int>(columnIds);
                if (StringHelper.IsInt(columnIds))
                {
                    where+= " and columnId=@columnId";
                    MvcContext.DbContext.AddDbParamter("@columnId", columnId);
                }
                else if (!string.IsNullOrEmpty(columnIds))
                {
                    where += " and columnId in(" + columnIds + ")";
                }
            }

            string attrs = StringHelper.Format<string>(Request.QueryString["_attr"]);
            string[] arrAttrs = attrs.Split(',');
            for (int i = 0; i < arrAttrs.Length; i++)
            {
                string attr = arrAttrs[i];
                if (attr == "isGood" || attr == "isHot" || attr == "isTop")
                {
                    where += " and " + attr + "> 0";
                }
                else if (attr == "noColumn")
                {
                    where += " and columnId=0";
                }
                else if (attr == "notOnline")
                {
                    where += " and isOnline=0";
                }
                else if (attr == "isExpire")
                {
                    where += " and isExpire=1";
                }
                else if (attr == "hasThumbnail")
                {
                    where += " and hasThumbnail=1";
                }
                else if (attr == "notReply")
                {
                    where += " and replyState=0";
                }
            }
            MvcContext.SqlCondition = where;
            InfoTableBLL infoTableBLL = new InfoTableBLL();
            dynamic tableData = infoTableBLL.GetData(MvcContext.Table);
            MvcContext.ListOrder = tableData.Orderby;
            MvcContext.PageInfo.PageSize = tableData.Pagesize;

        }


        //}

    }

}