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

namespace PageAdmin.Web.Areas.Admin.Controllers
{

    public class AdminController : DefaultBaseController
    {
        private AdminBLL _AdminBll;
        public AdminController():base("pa_admin", "User")
        {
            _AdminBll = new AdminBLL();
            base.AddExecuting =CheckUserName;
            base.AddExecuting += SetRelative;

            base.EditExecuting = SetRelative;
            base.EditExecuted= UpdateLoginUser;

            base.DeleteExecuted= DeleteRelative;
            base.EditViewExecuting = GetEditRelative;

            base.ListDataExecuting = SetSearch;
        }


        private void SetSearch()
        {
            var form = MvcContext.Data;
            var siteIds = form["siteIds"];
            if (!string.IsNullOrEmpty(siteIds))
            {
                siteIds = "," + siteIds + ","; //构造模糊搜索值
                form["siteIds"] = siteIds;
            }
        }

  

        //获取编辑页相关信息
        private void GetEditRelative()
        {
            var dbContext = base.MvcContext.DbContext;
            LoadListByIdsBLL loadDataBll = new LoadListByIdsBLL();
            string site_ids = base.EditDetailData.SiteIds;
            string member_group_ids=base.EditDetailData.MemberGroupIds;
            int default_site_id= base.EditDetailData.DefaultSiteId;
            ViewBag.DefaultSiteId =JsonHelper.JsonParse(loadDataBll.LoadListByIds(dbContext,"pa_site", "name", default_site_id.ToString()));
            ViewBag.SiteIds = JsonHelper.JsonParse(loadDataBll.LoadListByIds(dbContext,"pa_site","name", site_ids));
            ViewBag.MemberGroupIds = JsonHelper.JsonParse(loadDataBll.LoadListByIds(dbContext,"pa_member_group", "name", member_group_ids));
        }


        /// <summary>
        /// 添加时检测用户，用户角色
        /// </summary>
        private void CheckUserName()
        {
            TipsInfo tipsInfo = base.MvcContext.TipsInfo;
            _AdminBll.CheckUserName(base.MvcContext,tipsInfo);
        }

        /// <summary>
        /// 设置 is_superadmin和site_ids字段
        /// </summary>
        private void SetRelative()
        {
            var form = MvcContext.Data;
            var siteIds = form["siteIds"];
            if(!string.IsNullOrEmpty(siteIds))
            {
                siteIds = "," + siteIds + ","; //方便模糊搜索
                form["siteIds"] = siteIds;
            }
            TipsInfo tipsInfo = base.MvcContext.TipsInfo;
            _AdminBll.SetSuperAdmin(base.MvcContext, tipsInfo);
        }

        /// <summary>
        ///更新时同时更新LoginingUser
        /// </summary>
        private void UpdateLoginUser()
        {
            var form = MvcContext.Data;
            int uid = StringHelper.Format<int>(form["uid"]);
            LoginingUseBLL loginUserBll = new LoginingUseBLL();
            loginUserBll.UpdateData(uid);
        }

        /// <summary>
        /// 同步删除关联数据
        /// </summary>
        private void DeleteRelative()
        {
            _AdminBll.Delete(base.MvcContext);
        }


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