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

namespace PageAdmin.Web.Areas.Member.Controllers
{
    public class MyLoginController:BaseController
    {
        public MyLoginController()
        {

        }

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

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

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [ValidateAntiForgeryToken]
        [HttpPost]
        public JsonResult UserInfo(FormCollection form)
        {
            MvcControllerContext mvcContext = new MvcControllerContext();
            form["id"] = base.CurrentUser.Uid.ToString();
            mvcContext.Data = form;
            mvcContext.Table = "pa_member";
            mvcContext.IncludeCustomField = true;
            mvcContext.IncludeAttachments = true;
            EditBLL editBLL = new EditBLL();
            var tipsInfo=editBLL.Edit(mvcContext);
            return Json(tipsInfo);
        }


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

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

        //修改密码
        [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);
        }

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

        //邮箱绑定
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        [AjaxOnly]
        public virtual JsonResult EmailBind(FormCollection form)
        {
            string email = form["email"];
            string emailCode = form["_EmailCode"];
            int uid = StringHelper.Format<int>(form["id"]);
            EmailCodeBLL emailCodeBLL = new EmailCodeBLL();
            emailCodeBLL.Uid = uid;
            TipsInfo tipsInfo = emailCodeBLL.Validate(email, emailCode);
            if(tipsInfo.State==1)
            {
                emailCodeBLL.Clear();
                LoginingUseBLL loginUserBll = new LoginingUseBLL();
                loginUserBll.UpdateEmail(email, uid);
            }
            return Json(tipsInfo);
        }

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

        //绑定手机
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        [AjaxOnly]
        public virtual JsonResult MobileBind(FormCollection form)
        {
            string mobile = form["mobile"];
            string smsCode = form["_SmsCode"];
            int uid = StringHelper.Format<int>(form["id"]);
            SmsCodeBLL smsCodeBLL = new SmsCodeBLL();
            smsCodeBLL.Uid = uid;
            TipsInfo tipsInfo = smsCodeBLL.Validate(mobile, smsCode);
            if (tipsInfo.State == 1)
            {
                smsCodeBLL.Clear();
                LoginingUseBLL loginUserBll = new LoginingUseBLL();
                loginUserBll.UpdateMobile(mobile, uid);
            }
            return Json(tipsInfo);
        }


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


        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public virtual JsonResult LoginLogList()
        {
            int uid= base.CurrentUser.Uid;
            MvcControllerContext mvcContext = new MvcControllerContext();
            NameValueCollection form = Request.QueryString;
            mvcContext.Table = "pa_web_log";
            mvcContext.Data = form;
            mvcContext.DbContext.SetDbParamter(new { uid = uid, logType = 6 });
            mvcContext.SqlCondition = "uid=@uid and logType=@logType";
            PageInfo pageInfo = mvcContext.PageInfo;
            ListBLL listBll = new ListBLL();
            IEnumerable<dynamic> dataList = listBll.PageList(mvcContext);
            return Json(dataList, pageInfo);
        }

    }
}