
using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using System;
using System.Collections.Specialized;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{

    [CurrentSite]
    public class ColumnController : DefaultBaseController
    {
        public ColumnController() : base("pa_column", "Website")
        {

            base.MvcContext.IsMultilevelTable = true;

            base.AddViewExecuting += new Action(GetParentSet);
            //base.AddViewExecuting += () => { GetColumnContentSetField("add"); };

            base.ListDataExecuting = new Action(GetWhere);

            base.AddExecuting = new Action(CheckHome); //检测首页
            base.AddExecuting += new Action(SetExistsDic); //设置重复检测的字典

            base.EditExecuting = CheckHome; //设置重复检测的字典
            base.EditExecuting += new Action(SetExistsDic); //设置重复检测的字典

            base.AddExecuted = new Action(SetRelative); //设置相关

            base.EditExecuted = new Action(SetRelative); //编辑时候

            base.DeleteExecuted = new Action(DeleteRelative); //删除相关设置

            base.EditViewExecuting += new Action(GetRelative); //获取相关参数
            //base.EditViewExecuting += () => { GetColumnContentSetField("edit"); };
        }


        /// <summary>
        /// 执行完毕后清理缓存
        /// </summary>
        /// <param name="context"></param>
        protected override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.RequestContext.HttpContext.Request.HttpMethod == "POST")
            {
                new ColumnTemplateSetBLL().RemoveCache();
                new ColumnContentSetBLL().RemoveCache();
                new ColumnSeoSetBLL().RemoveCache();
                //new ColumnPublishSetBLL().RemoveCache();
                //new ColumnPermissionsSetBLL().RemoveCache();
                //new ColumnChargeSetBLL().RemoveCache();
            }
            base.OnActionExecuted(context);
        }

        //获取栏目内容设置表的相关字段,暂时没有用处
        private void GetColumnContentSetField(string pageType)
        {
            ViewBag.ContentSetFieldData = new FieldBLL().GetFieldList("pa_column_content_set", pageType, MvcContext.CurrentUser);
        }
        /// <summary>
        /// 批量设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SetMultiple()
        {
            return View();
        }

        [AjaxOnly]
        [HttpPost]
        public JsonResult SetMultiple(FormCollection form)
        {
            ColumnSetMultipleBLL bll = new ColumnSetMultipleBLL();
            var tipsInfo = bll.Set(form);
            return Json(tipsInfo);
        }


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

        /// <summary>
        /// listData 条件
        /// </summary>
        private void GetWhere()
        {
            string zdyWhere = "";
            int siteId = StringHelper.Format<int>(Request.QueryString["siteId"]);
            if (siteId == 0)
            {
                siteId = (int)ViewBag.CurrentSiteId; //ViewBag.SiteId在CurrentSite特性文件中已定义
            }
            zdyWhere = "siteId=" + siteId;
            base.MvcContext.SqlCondition = zdyWhere;
        }

        //检测重复
        private void SetExistsDic()
        {
            var actionName = RouteData.Values["action"].ToString();
            var form = Request.Form;
            string path = form["path"];
            if (form["columnType"] == "0")
            {
                return;
            }
            else if (!string.IsNullOrEmpty(path)) //非首页进行目录验证
            {
                base.CheckRepeatFieldDic.Add("path", "访问路径");
            }
            int siteId = StringHelper.Format<int>(form["siteId"]);
            //base.CheckRepeatFieldDic.Add("name", "栏目名称"); //栏目名称不检测
            base.CheckExixtsWhere = "siteId=" + siteId;
        }


        private void GetParentSet()
        {
            int parentId = StringHelper.Format<int>(Request.QueryString["parentId"]);
            if (parentId > 0)
            {
                ColumnBLL columnBLL = new ColumnBLL();
                dynamic data = columnBLL.GetParentData(parentId);
                if (data != null)
                {
                    ViewBag.TableId = data.TableId;
                    ViewBag.ColumnType = data.ColumnType;
                    ViewBag.ParentId = parentId;
                    GetRelativeSet(data.Id);
                }

            }

        }

        /// <summary>
        /// 检测首页是否存在
        /// </summary>
        private void CheckHome()
        {
            var form = Request.Form;
            TipsInfo tipsInfo = base.MvcContext.TipsInfo;
            int id = StringHelper.Format<int>(form["id"]);
            if (form["columnType"] == "0")//检测首页是否存在
            {
                ColumnBLL columnBll = new ColumnBLL();
                int siteId = (int)ViewBag.CurrentSiteId;
                if (columnBll.HasHomeColumn(siteId, id))
                {
                    tipsInfo.State = 0;
                    tipsInfo.Msg = "首页栏目已经存在,请重新选择栏目类型！";
                }
            }

        }

        //设置相关
        private void SetRelative()
        {
            NameValueCollection form = base.MvcContext.Data;
            int columnId = base.MvcContext.DetailId;

            if (columnId > 0)
            {
                new ColumnTemplateSetBLL().Set(form, columnId); //模板设置
                new ColumnContentSetBLL().Set(form, columnId);//栏目内容设置
                new ColumnSeoSetBLL().Set(form, columnId); //seo设置
                                                           //new ColumnPublishSetBLL().Set(form, columnId); //投稿设置规则
                                                           //new ColumnPermissionsSetBLL().Set(form, columnId); //浏览权限
                                                           //new ColumnChargeSetBLL().Set(form, columnId); //收费设置
                                                           //new ColumnCommentSetBLL().Set(form, columnId); //comment设置
                                                           //移出栏目url字典缓存
                WebUrlHelper.RemoveColumnUrlDicKey(columnId);
            }

        }



        //删除相关
        private void DeleteRelative()
        {
            int columnId = StringHelper.Format<int>(Request.Form["id"]);
            new ColumnTemplateSetBLL().Delete(columnId); //模板设置删除
            new ColumnSeoSetBLL().Delete(columnId); //seo设置删除
            //new ColumnPublishSetBLL().Delete(columnId); //投稿设置删除
            new ColumnContentSetBLL().Delete(columnId); //删除内容设置
                                                        // new ColumnPermissionsSetBLL().Delete(columnId); //浏览权限删除
                                                        //new ColumnChargeSetBLL().Delete(columnId); //收费设置删除
                                                        //new ColumnCommentSetBLL().Delete(columnId); //评论设置删除


            //移出栏目url字典缓存
            WebUrlHelper.RemoveColumnUrlDicKey(columnId);
        }

        private void GetRelative()
        {
            int columnId = StringHelper.Format<int>(Request.QueryString["id"]);
            if (columnId > 0)
            {
                GetRelativeSet(columnId);
            }
        }
        private void GetRelativeSet(int columnId)
        {
            if (columnId > 0)
            {
                ViewBag.ContentSet = JsonHelper.JsonParse<dynamic>((new ColumnContentSetBLL()).Get(columnId));

                ViewBag.SeoSet = JsonHelper.JsonParse<dynamic>((new ColumnSeoSetBLL()).Get(columnId));
                ViewBag.TemplateSet = JsonHelper.JsonParse<dynamic>(new ColumnTemplateSetBLL().Get(columnId));
                //ViewBag.PublishSet = JsonHelper.JsonParse<dynamic>((new ColumnPublishSetBLL()).Get(columnId));
                //ViewBag.ViewPermissions = JsonHelper.JsonParse(new ColumnPermissionsSetBLL().Get(columnId));
                //ViewBag.ChargeSet = JsonHelper.JsonParse(new ColumnChargeSetBLL().Get(columnId));
                //ViewBag.CommentSet = JsonHelper.JsonParse((new ColumnCommentSetBLL()).Get(columnId));
            }
        }


    }
}