
using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils.ORM;
using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using System;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    [CheckTable(TableType=TableTypeEnum.CustomInfoTable)]
    [NotCheckMenuItemPermissions]
    [CurrentSite]
    [CheckInfoAdminPermissions] //检测管理员信息发布权限
    public class InfoDataController : DefaultBaseController
    {
        public InfoDataController() : base("", "Website")
        {
            base.MvcContext.TableType = TableTypeEnum.CustomInfoTable;

            base.MvcContext.IncludeCustomField = true;
            base.MvcContext.IncludeAttachments = true;

            base.ListDataExecuting = new Action(OnListDataExecuting);

            base.EditViewExecuting += new Action(GetRelative);

            base.AddExecuted = new Action(SetRelative);
            base.EditExecuted = new Action(SetRelative);
            base.DeleteExecuted = new Action(OnDeleteExecuted);

        }

        private void OnListDataExecuting()
        {
            string where = "1=1";
            int state = StringHelper.Format<int>(Request.QueryString["state"]);
            string username = StringHelper.Format<string>(Request.QueryString["username"]);
            if (!string.IsNullOrEmpty(Request.QueryString["state"]))
            {
                base.MvcContext.DbContext.AddDbParamter("@state", state);
                where += " and [state]=@state";
            }
            if (StringHelper.IsUserName(username))
            {
                base.MvcContext.DbContext.AddDbParamter("@username", username);
                where += " and username=@username";
            }
            string filterListWhere = ViewBag.FilterListWhere;//获取过滤器中的权限条件条件
            if (!string.IsNullOrEmpty(filterListWhere))
            {
                DbKeyValueList filterParam = ViewBag.FilterListParam;
                foreach (var item in filterParam)
                {
                    base.MvcContext.DbContext.AddDbParamter(item.Key, item.Value);
                }
                where += filterListWhere;
            }
            else
            {
                filterListWhere = "";
            }
            // int columnId= StringHelper.Format<int>(Request.QueryString["columnId"]); //columnId转到过滤器中

            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";
                }
                else if (attr == "publishFromAdmin")
                {
                    where += " and publishFrom=0";
                }
                else if (attr == "publishFromMember")
                {
                    where += " and publishFrom=1";
                }
                else if (attr == "publishFromGuest")
                {
                    where += " and publishFrom=10";
                }  
            }
            if (where == "1=1")
            {
                where = "";
            }
            else
            {
                where = where.Replace("1=1 and", "");
            }
            base.MvcContext.SqlCondition = where;
            InfoTableBLL infoTableBLL = new InfoTableBLL();
            dynamic tableData = infoTableBLL.GetData(ViewBag.Table);
            base.MvcContext.ListOrder = tableData.Orderby;
            base.MvcContext.PageInfo.PageSize = tableData.Pagesize;
        }


        //添加和修改提交后设置相关
        private void SetRelative()
        {
            string table = base.MvcContext.Table;
            int infoId= base.MvcContext.DetailId;
            new InfoSeoSetBLL(table).Set(base.MvcContext, infoId); //seo设置，同时输出infoid
            new InfoTemplateSetBLL(table).Set(base.MvcContext, infoId); //模板设置
            new InfoUrlSetBLL(table).Set(base.MvcContext, infoId); //设置Url映射表

            new InfoOnlineSetBLL(table).Set(base.MvcContext, infoId); //设置信息预上线时间
            new InfoExpireSetBLL(table).Set(base.MvcContext, infoId); //设置信息过期时间

            // new InfoCommentSetBLL(table).Set(base.MvcContext, infoId); //评论设置
            // new InfoPermissionsSetBLL(table).Set(base.MvcContext, infoId); //权限设置
            //new InfoChargeSetBLL(table).Set(base.MvcContext, infoId); //收费设置

            //new InfoSignSetBLL(table).Set(base.MvcContext, infoId); //签收设置

            //new InfoIssueSetBLL(table).Set(base.MvcContext, infoId); //签发设置

            //if(postType=="add")
            //{
            //    //new InfoReplySetBLL(table).Set(base.MvcContext, infoId); //回复设置
            //    //new InfoPublishRewardBLL(table).Set(base.MvcContext, infoId); //投稿赠送的积分和奖励
            //}

            //new InfoVoteSetBLL(table).Set(base.MvcContext, infoId); //投票

            // new InfoOperationLogBLL(table).Set(base.MvcContext, infoId);//写信息日志


            //new InfoSubColumnSetBLL(table).Set(base.MvcContext,infoId); //此功能改到列表页实现

            //if (postType == "edit")
            //{
            //    string title = form["title"];
            //    //new InfoSignSetBLL(table).UpdateTitle(title, infoId); //更新标题
            //    //new InfoIssueSetBLL(table).UpdateTitle(title,infoId); //更新标题
            //    //new InfoReplySetBLL(table).UpdateTitle(title,infoId); //更新标题
            //}
        }

        //删除相关
        private void OnDeleteExecuted()
        {
            int infoId = StringHelper.Format<int>(base.MvcContext.Data["id"]);
            InfoDataBLL infoDataBLL = new InfoDataBLL(base.MvcContext.Table);
            infoDataBLL.DeleteCallBack(infoId);//放在bll层，方便其他页面共用
        }

        //编辑页获取相关数据
        public void GetRelative()
        {
            string table = base.MvcContext.Table;
            int infoId = StringHelper.Format<int>(Request.QueryString["id"]);
            if (infoId > 0)
            {

                ViewBag.SeoSet = JsonHelper.JsonParse<dynamic>(new InfoSeoSetBLL(table).Get(infoId)); //seo
                ViewBag.TemplateSet = JsonHelper.JsonParse<dynamic>(new InfoTemplateSetBLL(table).Get(infoId)); //模板
                ViewBag.UrlSet = JsonHelper.JsonParse<dynamic>(new InfoUrlSetBLL(table).Get(infoId)); //urlset

                // ViewBag.CommentSet = JsonHelper.JsonParse(new InfoCommentSetBLL(table).Get(infoId)); //评论
                ///ViewBag.PermissionsSet = JsonHelper.JsonParse(new InfoPermissionsSetBLL(table).Get(infoId)); //权限
                // ViewBag.ChargeSet = JsonHelper.JsonParse(new InfoChargeSetBLL(table).Get(infoId)); //收费
                // ViewBag.SignSet = JsonHelper.JsonParse(new InfoSignSetBLL(table).Get(infoId)); //签收

                //投票
                // InfoVoteSetBLL infoVoteSetBLL = new InfoVoteSetBLL(table);
                //ViewBag.VoteSet = JsonHelper.JsonParse(infoVoteSetBLL.Get(infoId)); //投票
                //ViewBag.VoteData = JsonHelper.JsonParse(infoVoteSetBLL.GetVoteData(infoId)); //投票项目

                //ViewBag.InfoSubColumnIds = new InfoSubColumnSetBLL(table).Get(infoId); //推送

                ViewBag.OnlineDate = new InfoOnlineSetBLL(table).Get(infoId); //预上线时间
                ViewBag.ExpireDate = new InfoExpireSetBLL(table).Get(infoId); //过期时间
            }
        }

        /// <summary>
        /// 批量设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PushData()
        {
            return View();
        }

        [AjaxOnly]
        [HttpPost]
        public JsonResult PushData(FormCollection form)
        {
            base.MvcContext.Data = form;
            InfoDataPushBLL bll = new InfoDataPushBLL(MvcContext.Table);
            var tipsInfo = bll.Push(base.MvcContext);
            return Json(tipsInfo);
        }

        /// <summary>
        /// 批量设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SetMultiple()
        {
            return View();
        }

        [AjaxOnly]
        [HttpPost]
        public JsonResult SetMultiple(FormCollection form)
        {
            base.MvcContext.Data = form;
            InfoDataMutipleSetBLL bll = new InfoDataMutipleSetBLL(MvcContext.Table);
            var tipsInfo = bll.Set(MvcContext);
            return Json(tipsInfo);
        }

    }

}