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

namespace PageAdmin.Web.Areas.Admin.Controllers

{
    public class SiteController : DefaultBaseController
    {
        public SiteController() : base("pa_site", "System/Configure/")
        {

            base.MvcContext.ListOrder = "id asc";

            base.ListDataExecuting=new Action(GetPermissions);
            base.DeleteExecuting = new Action(OnDeleteExecuting);
            base.DeleteExecuted = new Action(DeleteRelative);

            base.AddExecuting=SetRelative;
            base.EditExecuting = SetRelative;
        }

        private void SetRelative()
        {
            base.CheckRepeatFieldDic.Add("name", "站点名称");
            base.CheckRepeatFieldDic.Add("directory", "站点目录");
            var form = base.MvcContext.Data;
            string domain = form["domain"].Trim();
            form["domain"] = StringHelper.DomainFormat(domain);
            if (!string.IsNullOrEmpty(domain))
            {
                base.CheckRepeatFieldDic.Add("domain", "站点域名");
            }
            else
            {
                form["dommain"] = "";
            }
            ViewBag.Guid =SysHelper.NewGuid();
            int id = StringHelper.Format<int>(Request.Form["id"]);
            WebUrlHelper.RemoveSiteUrlPrefixDicKey(id);
        }


        //根据uid获取可管理的站点
        private void GetPermissions()
        {
            //根据管理员uid显示
            int uid = StringHelper.Format<int>(Request.QueryString["uid"]);
            int byUser = StringHelper.Format<int>(Request.QueryString["_byuser"]);
            int isLoad = StringHelper.Format<int>(Request.QueryString["_isload"]);
            if (isLoad == 1)
            {
                MvcContext.Fields = "id,name,remark,directory,domain";
            }
            if (uid == 0 && byUser==0)//没有条件
            {
                return;
            }
            if(byUser==1) //byUser不未空，则默认当前用户
            {
                if(uid==0)
                {
                    uid = base.CurrentUser.Uid;
                }
            }
            AdminBLL adminBll = new AdminBLL();
            string canSiteids = adminBll.GetSiteIds(uid);
            canSiteids = StringHelper.Unique<int>(canSiteids);
            if (canSiteids=="0")//超级管理员
            {
                return;
            }
            else if (canSiteids == "")
            {
                return;
            }
            else if(StringHelper.IsInt(canSiteids))
            {
                base.MvcContext.SqlCondition = "id="+ canSiteids;
            }
            else
            {
                base.MvcContext.SqlCondition = "id in(" + canSiteids + ")";
            }
        }

        /// <summary>
        /// 删除前的检测
        /// </summary>
        private void OnDeleteExecuting()
        {
            base.MvcContext.SqlCondition = "isDefault=0";//默认站点不能删除
            int siteId = StringHelper.Format<int>(Request.Form["id"]);
            SiteBLL siteBll = new SiteBLL();
            TipsInfo tipsInfo = base.MvcContext.TipsInfo;
            if (siteBll.IsExists(siteId))
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "当前站点下存在栏目，不能直接删除！";
            }
        }


        //删除相关表
        private void DeleteRelative()
        {
            int id = StringHelper.Format<int>(Request.Form["id"]);
            WebUrlHelper.RemoveSiteUrlPrefixDicKey(id);//删除前缀静态字典的对应key
        }

        //设置当前管理站点
        [HttpGet]
        public ActionResult Select()
        {
            return View();
        }


        //设置当前管理站点
        [HttpGet]
        [NotCheckMenuItemPermissions]
        public ActionResult SetCurrentSite()
        {
            return View();
        }

        //设置当前管理站点
        [HttpPost]
        [AjaxOnly]
        [NotCheckMenuItemPermissions]
        public void SetCurrentSite(FormCollection form)
        {
            int siteId = StringHelper.Format<int>(form["id"]);
            SiteBLL siteBll = new SiteBLL();
            siteBll.BuildSiteCookie(siteId);
        }

    }
}