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

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

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


        [ValidateAntiForgeryToken]
        [Validate]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Index(FormCollection form)
        {
            MvcControllerContext mvcContext = new MvcControllerContext();
            TipsInfo tipsInfo = mvcContext.TipsInfo;
            string userName = form["username"];
            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";
            form["isAdmin"] = "0";
            form["state"] = ViewBag.DefaultState.ToString();
            //form["emailState"] = ViewBag.EmailState.ToString();
            //form["mobilestate"] = ViewBag.MobileState.ToString();
            mvcContext.Data = form;
            mvcContext.Table = "pa_member";
            mvcContext.IncludeAttachments = true;
            mvcContext.IncludeCustomField = true;

            CheckExistsBLL checkExistsBLL = new CheckExistsBLL();
            tipsInfo=checkExistsBLL.CheckExists(mvcContext, 0, "用户名", "username", userName);
            if(tipsInfo.State==1)
            {
                AddBLL addBLL = new AddBLL();
                tipsInfo = addBLL.Add(mvcContext);
                tipsInfo.Description = userName; //传递给前端
            }
            if(tipsInfo.State==1)
            {
                VerificationCodeBLL verificationCodeBLL = new VerificationCodeBLL();
                verificationCodeBLL.Clear();
                SmsCodeBLL smsCodeBLL = new SmsCodeBLL();
                smsCodeBLL.Clear();
                EmailCodeBLL emailCodeBLL = new EmailCodeBLL();
                emailCodeBLL.Clear();

                SendMemberNoticeBLL sendMemberNoticeBLL = new SendMemberNoticeBLL();
                sendMemberNoticeBLL.RegEmail(form["email"], userName);
                sendMemberNoticeBLL.RegSms(form["mobile"], userName);
            }
            return Json(tipsInfo);
        }

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

        [HttpGet]
        public ActionResult RegAgreement()
        {
            string content = XmlHelper.NodeValue("/configuration/content", "/Config/MemberRegAgreement.config");
            return View((object)content);
        }

    }
}
