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

namespace PageAdmin.Web.Areas.Member.Controllers
{
    [AllowAnonymous]
    public class FindPasswordController : BaseController
    {

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

        [FirstValidate]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Index(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo();
            MemberBLL memberBLL = new MemberBLL();
            string account = form["account"];
            string userName = memberBLL.FindUser(account);
            if (string.IsNullOrEmpty(userName))
            {
                tipsInfo.State = 0;
                if (StringHelper.IsMobile(account))
                {
                    tipsInfo.Msg = "对不起，您输入的手机号没有绑定账号";
                }
                else if (StringHelper.IsEmail(account))
                {
                    tipsInfo.Msg = "对不起，您输入的邮箱没有绑定的账号";
                }
                else
                {
                    tipsInfo.Msg = "对不起，您输入的账户不存在";
                }
            }
            else
            {
                string findUserName = EncryptHelper.DesEncrypt(userName, SystemConfig.Get().SecretKey);
                CookieHelper.Add("_FindUserName", findUserName);
            }
            return base.Json(tipsInfo);
        }

        [ConfirmValidate]
        [Config]
        [HttpGet]
        public ActionResult Confirm()
        {
            return View();
        }

        /// <summary>
        /// 身份确认
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>

        [ValidateAntiForgeryToken]
        [ConfirmValidate]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Confirm(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo()
            {
                State = 0,
                Msg = "验证失败"
            };
            string findType = form["findType"];
            if (findType == "email")
            {
                string email = form["email"];
                string emailCode = form["_EmailCode"];
                EmailCodeBLL emailCodeBLL = new EmailCodeBLL();
                tipsInfo = emailCodeBLL.Validate(email, emailCode);
            }
            else if (findType == "mobile")
            {
                string mobile = form["mobile"];
                string smsCode = form["_SmsCode"];
                EmailCodeBLL emailCodeBLL = new EmailCodeBLL();
                tipsInfo = emailCodeBLL.Validate(mobile, smsCode);
            }
            //验证成功后
            if (tipsInfo.State == 1)
            {
                CurrentUser currentUser = (CurrentUser)ViewBag.FindUser;
                string userName = currentUser.UserName + "," + currentUser.Uid;
                string findUsername = EncryptHelper.DesEncrypt(userName, SystemConfig.Get().SecretKey);
                CookieHelper.Add("_FindUserName", findUsername);

                SmsCodeBLL smsCodeBLL = new SmsCodeBLL();
                smsCodeBLL.Clear();
                EmailCodeBLL emailCodeBLL = new EmailCodeBLL();
                emailCodeBLL.Clear();

            }
            return base.Json(tipsInfo);
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [ModifyPasswordValidate]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult ModifyPassword(FormCollection form)
        {
            CurrentUser currentUser = (CurrentUser)ViewBag.FindUser;
            TipsInfo tipsInfo = new TipsInfo()
            {
                State = 1,
                Msg = "密码修改成功!",
                Description = currentUser.UserName
            };
            int uid = ViewBag.Uid;
            string passord = form["password"];
            MemberBLL memberBLL = new MemberBLL();
            memberBLL.ModifyPassword(passord, uid);
            CookieHelper.Remove("_FindUserName");
            return base.Json(tipsInfo);
        }


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

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


    }
}
