using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils;
using PageAdmin.Web.Areas.Admin.Controllers.Member;
using PageAdmin.Web.Filter;
using System;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{

    public class MemberController : DefaultBaseController
    {
        public MemberController():base("pa_member", "User")
        {
            base.MvcContext.IncludeAttachments= true;
            base.MvcContext.IncludeCustomField = true;

            base.CheckRepeatFieldDic.Add("username", "用户名");
            base.EditViewName = "Edit";

            base.ListDataExecuting = new Action(OnListDataExecuting);

            base.DeleteExecuted = DeleteRelative;
            base.DeleteExecuted += UpdateLoginUser;
            base.EditExecuted = UpdateLoginUser;

            base.AddViewExecuting = GetAddRelative;
            base.EditViewExecuting = GetEditRelative;
        }

        /// <summary>
        /// 删除和更新时同时更新LoginingUser
        /// </summary>
        private void DeleteRelative()
        {
            var form = MvcContext.Data;
            int id = StringHelper.Format<int>(form["id"]);
            MemberBLL memberBLL = new MemberBLL();
            memberBLL.DeleteRelative(id);
        }


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


        [ValidateInput(false)]
        [HttpPost]
        [AjaxOnly]
        public override JsonResult Add(FormCollection form)
        {
            string pass = form["password"];
            form["password"] = EncryptHelper.MD5(pass);
            string ip = SysHelper.ClientIP();
            form["regIp"] = ip;
            string nowDt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            form["regDate"] = nowDt;
            form["lastDate"] = nowDt;
            form["logins"] = "0";
            return base.Add(form);
        }


        [ValidateInput(false)]
        [HttpPost]
        [AjaxOnly]
        public override JsonResult Edit(FormCollection form)
        {
            string pass = form["Password"];
            string oldpass = form["oldPassword"];
            if (!string.IsNullOrEmpty(pass))
            {
                form["password"] = EncryptHelper.MD5(pass);
            }
            else
            {
                form["password"] = oldpass;
            }
            return base.Edit(form);
        }

        private void GetAddRelative()
        {
            int memberGroupId = StringHelper.Format<int>(Request.QueryString["memberGroupId"]);
            if(memberGroupId==0)
            {
                memberGroupId = 1;
            }
            ViewBag.memberGroupId = memberGroupId;
        }

        /// <summary>
        /// 获取相关信息
        /// </summary>
        private void GetEditRelative()
        {
            int memberGroupId = base.EditDetailData.MemberGroupId;
            ViewBag.MemberGroupId = memberGroupId; //重新获取EditUser
        }

        /// <summary>
        /// 非超级管理员限制member_group_ids
        /// </summary>
        private void OnListDataExecuting()
        {
            CurrentUser user = base.MvcContext.CurrentUser;
            if (user.IsSuperAdmin)
            {
                return;
            }
            AdminBLL adminBll = new AdminBLL();
            string canIds = adminBll.GetMemberGroupIds(user.Uid);
            int isLoad = StringHelper.Format<int>(Request.QueryString["_isload"]); //加载模式
            if (canIds == "0" && isLoad == 1)
            {
                return;
            }
            else if (canIds == "")
            {
                //base.MvcContext.SqlCondition = "id=0";
            }
            else
            {
                base.MvcContext.SqlCondition = "memberGroupId in(" + canIds + ")";
            }
        }


        /// <summary>
        /// 其他页面根据uid获取资料，如信息列表页等等
        /// </summary>
        /// <returns></returns>
        [NotCheckMenuItemPermissions]
        [AjaxOnly]
        [HttpGet]
        public JsonResult GetMemberInfo()
        {
            int uid = StringHelper.Format<int>(Request.QueryString["id"]);
            MemberBLL memberBll = new MemberBLL();
            dynamic data = memberBll.GetData(uid);
            return Json(data);
        }

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

        [UpdateLoginingUser]
        [HttpPost]
        public JsonResult MemberGroupSet(FormCollection form)
        {
            return base.SetField(form);
        }


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

        [UpdateLoginingUser]
        [HttpPost]
        public JsonResult DepartmentSet(FormCollection form)
        {
            return base.SetField(form);
        }

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

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