using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Common;
using JuSNS.Config;
namespace JuSNS.Web
{
    public partial class Register : JuSNS.UI.Page.BasePage
    {
        protected string province = string.Empty;
        protected string cityarray = string.Empty;
        protected int succeed = 0;
        protected string error = string.Empty;
        protected string email = string.Empty;
        protected string sex = string.Empty;
        protected string isRegMobile = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            string RegSwitch = Public.GetXMLValue("OpenReg");
            string isRegMobile = Public.GetXMLValue("isRegMobile");
            if (RegSwitch != "1")
            {
                if (RegSwitch == "0")
                {
                    PageError("管理员禁止注册！", Public.GetXMLValue("Url") + rooDir, true);
                }
                else if (RegSwitch == "2")
                {
                    PageError("系统未开启注册功能，必须要朋友邀请才能注册！", Public.GetXMLValue("Url") +rooDir, true);
                }
            }
            if (isRegMobile == "1")
            {
                isRegMobile = "或手机";
            }
            if (!IsPostBack)
            {
                if (Request.Form["Register"] == "Submit")
                {
                    DoRegister();
                }
                if (succeed != 1 && succeed != 2)
                {
                    Dict dict = new Dict();
                    IList<DictAreaInfo> list = dict.GetArea();
                    foreach (DictAreaInfo info in list)
                    {
                        if (info.ParentID.Equals(0))
                        {
                            province += "<option value=\"" + info.ID + "\">" + info.Name + "</option>";
                        }
                        else
                        {
                            if (!cityarray.Equals(string.Empty))
                                cityarray += ",";
                            cityarray += "new Array('" + info.ParentID + "','" + info.ID + "','" + info.Name + "')";
                        }
                    }
                }
                DataBind();
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        protected void DoRegister()
        {
            UserInfo ui = new UserInfo();
            ui.Email = email = Request["TxtEmail"];
            ui.Username = Request["TxtName"];
            ui.Password = Request["TxtPassword"];
            ui.Province = Convert.ToInt32(Request["SlctProvince"]);
            ui.City = Convert.ToInt32(Request["SlctCity"]);
            bool isFull = false;
            if (string.IsNullOrEmpty(Request["TxtEmail"]) 
                || string.IsNullOrEmpty(Request["TxtName"]) 
                || string.IsNullOrEmpty(Request["TxtPassword"]) 
                || string.IsNullOrEmpty(Request["SlctProvince"]))
            {
                isFull = true;
            }

            UserBasicInfo bi = new UserBasicInfo();

            bi.Sex = Convert.ToInt32(Request["Slctgender"]);
            if (string.IsNullOrEmpty(Request["Slctgender"])){isFull = true;}
            if (isFull){error = "所有项目必须填写！";}
            JuSNS.Home.User User = new User();
            EnumRegister ret = User.Register(ui, bi);
            if (ret == EnumRegister.Succeed)
            {
                succeed = 1;
            }
            else if (ret == EnumRegister.SucceedNotMail)
            {
                succeed = 2;
            }
            else if (ret == EnumRegister.EmailRepeat)
            {
                error = "该Email(" + Request["TxtEmail"] + ")地址已注册过帐户！";
            }
            else if (ret == EnumRegister.EmailMayNotSend)
            {
                error = "你已经注册成功！请你稍后检查邮箱(" + Request["TxtEmail"] + ")。如果没有收到帐号确认邮件，请与客服联系！";
            }
            else
            {
                error = "系统错误,请稍后再试！";
            }
            if (!string.IsNullOrEmpty(error))
            {
                PageError(error, "javascript:history.back()");
            }
        }
    }
}

