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

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class MyMemberInfoController : DefaultBaseController
    {

        public MyMemberInfoController(): base("pa_member", "My")
        {
            base.IndexViewExecuting = GetMemberInfo;
            base.MvcContext.IncludeCustomField = true;
        }

        private void GetMemberInfo()
        {
            CurrentUser currentUser = base.CurrentUser;
            MemberBLL memberBll = new MemberBLL();
            dynamic data = memberBll.GetData(currentUser.Uid);
            ViewBag.MemberData = JsonHelper.JsonParse(data);
            FieldBLL fieldBll = new FieldBLL();
            ViewBag.FieldData = fieldBll.GetFieldList("pa_member", "edit", currentUser, FromEnum.Member);
            MemberGroupBLL memberGroupBLL = new MemberGroupBLL();
            ViewBag.MemberGroupName= memberGroupBLL.GetName(currentUser.MemberGroupId);
            DepartmentBLL departmentBLL = new DepartmentBLL();
            string departmentName= departmentBLL.GetName(currentUser.DepartmentId);
            if(string.IsNullOrEmpty(departmentName))
            {
                departmentName = "未设置";
            }
            ViewBag.DepartmentName = departmentName;
        }


        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        [AjaxOnly]
        public virtual JsonResult ModifyPassword(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo()
            {
                Msg = "修改成功！"
            };

            MemberBLL memberBll = new MemberBLL();
            int uid = base.CurrentUser.Uid;
            string oldPass = form["oldPassword"];
            string newPass = form["newPassword"];
            if(!memberBll.IsExists(uid, oldPass))
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "对不起，旧密码输入错误！";
            }
            else
            {
                memberBll.ModifyPassword(newPass, uid);
            }
            return Json(tipsInfo);
        }
    }
}