using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
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;
using System.IO;

namespace JuSNS.Web.home.ibase
{
    public partial class index : JuSNS.UI.Page.UserPage
    {
        Space sp = new Space();
        MyInfo ac = new MyInfo();
        public string StrEmail = string.Empty;
        public string uName = string.Empty;
        public string eMail = string.Empty;
        public string modiTime = string.Empty;
        public string userXML = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getInfo();
            }
            userXML = "~/user/config/xml/" + UserID.ToString() + ".xml";
            if (File.Exists(Server.MapPath(userXML)))
            {
                int eTime = Convert.ToInt32(JuSNS.Common.Public.getXML(userXML, "moditime", "0", 1));
                if (eTime >= 5)
                {
                    this.realname.Disabled = true;
                    this.realnameFormSubmit.Disabled = true;
                    this.ttimes.InnerHtml = "你的5次改名机会已经用完！你不能更改真实姓名了。";
                }
                else
                {
                    this.realname.Disabled = false;
                    this.realnameFormSubmit.Disabled = false;
                    this.ttimes.InnerHtml = "你还有" + (5 - eTime).ToString() + "次改名机会。请谨慎使用！";
                }
            }
            else
            {
                int eTime = Convert.ToInt32(JuSNS.Common.Public.getXML(userXML, "moditime", "0", 2));
                modiTime = "5";
                this.ttimes.InnerHtml = "您还有5次改名的机会。请谨慎使用！";
            }
            string r = Request.QueryString["r"];
            UserInfo mdl = new UserInfo();
            User us = new User();
            mdl = us.GetUserInfo(UserID);
            uName = mdl.Username;
            this.realname.Value = uName;
            eMail = mdl.Email;
            if (r == "delaccount")
            {
                if (ac.DelUserSelf(UserID) > 0)
                {
                    string rStr = "注销账户 (姓名：" + uName + ",邮件：" +eMail + ") 成功，感谢您注册" + JuSNS.Config.UiConfig.SiteName + "!";
                    Response.Redirect("~/?r=" + JuSNS.Common.Input.URLEncode(rStr) + "");
                }
            }
            try
            {
                this.txtFlag.Value = Request.QueryString["flag"].ToString();
            }
            catch
            {
                this.txtFlag.Value = "0";
            }

            try
            {
                string type = Request.Form["Type"];
                if (type == "Name")
                {
                    Submit();
                }
                else if (type == "Email")
                {
                    UpdateEmail();
                }
                else if (type == "Pwd")
                {
                    if (ac.ChanagePwd(JuSNS.Common.Input.MD5(Request.Form["oldPwd"], false), JuSNS.Common.Input.MD5(Request.Form["newPwd"], false), this.UserID) == 1)
                    {
                        try
                        {
                            SetCookie(this.UserID, this.UserName, JuSNS.Common.Input.MD5(Request.Form["newPwd"], false), this.UserPortrait, false);
                        }
                        catch
                        { }
                        Response.Write("修改密码成功！");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("旧密码错误！");
                        Response.End();
                    }
                }
                else if (type == "Del")
                {
                    if (ac.DelInfo(Request.Form["Email"]) == 1)
                    {
                        Response.Write("修改邮箱成功！");
                        Response.End();
                    }
                }
            }
            catch{ }
        }

        //网络设置
        public void Submit()
        {
            if (this.UserName != Request.Form["newName"])
            {
                try
                {
                    int eTime = Convert.ToInt32(JuSNS.Common.Public.getXML(userXML, "moditime", "0", 1));
                    if (eTime >= 5)
                    {
                        Response.Write("您修改名字的机会已用完！");
                        Response.End();
                    }
                    JuSNS.Common.Public.getXML(userXML, "moditime", "" + (eTime + 1).ToString() + "", 2);

                    if (ac.UpdateUserName(this.UserName, Request.Form["newName"], this.UserID) == 1)
                    {
                        SNSToKenCookie = Context.Request.Cookies["SNSUserPassPort"];
                        SNSToKenCookie["username"] = Input.URLEncode(Request.Form["newName"]);
                    }
                    Response.Write("修改姓名成功!");
                    Response.End();

                }
                catch
                { }

            }
            else
            {
                Response.Write("修改姓名失败，与你现在使用的姓名相同，请重新输入!");
                Response.End();
            }
        }

        //更改邮箱
        public void UpdateEmail()
        {// 判断密码是否正确
            if (ac.getPwd(this.UserID, JuSNS.Common.Input.MD5(Request.Form["Pwd"], false)))
            {
                int n = 0;
                if (Request.Form["flag"] == "0")
                {
                    if (!ac.CheckEmail(Request.Form["newEmail"]))//判断邮箱是否被用了 
                    {
                        n = ac.UpdateEmail(Request.Form["newEmail"], Request.Form["oldEmail"], this.UserID, JuSNS.Common.Public.GetClientIP(), true);
                    }
                    else //被用了
                    {
                        Response.Write("2");
                        Response.End();
                    }
                }
                else
                {
                    n = ac.UpdateEmail(Request.Form["newEmail"], Request.Form["oldEmail"], this.UserID, JuSNS.Common.Public.GetClientIP(), false);
                }
                if (n == 1)
                {
                    //为用户发送电子邮件确认！
                    Response.Write("3");
                    Response.End();
                }
            }
            else
            {
                Response.Write("1");
                Response.End();
            }
        }

        //初始化邮箱信息
        public void getInfo()
        {
            DataTable dt = ac.getEmail(this.UserID, 0);
            int k = 0;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["flag"].ToString() == "0")
                        {
                            StrEmail += "<label class=\"f-check\">&nbsp;<input type=\"radio\" name=\"email\" checked=\"checked\" value=\"" + dt.Rows[i]["Email"].ToString() + "\" id=\"rad" + k + "\"/>" + dt.Rows[i]["Email"].ToString() + "</label>";
                            this.txtOldEmail.Value = dt.Rows[i]["Email"].ToString();
                        }
                        else
                        {
                            StrEmail += "<label class=\"f-check\">&nbsp;<input type=\"radio\" name=\"email\" value=\"" + dt.Rows[i]["Email"].ToString() + "\" id=\"rad" + k + "\"/>" + dt.Rows[i]["Email"].ToString();
                            StrEmail += "(<a href=\"javascript:DelEmail('" + dt.Rows[i]["Email"].ToString() + "')\" class=\"mail-del\">删除</a>)";
                        }
                        k++;
                    }
                }
            }
            StrEmail += "<label>&nbsp;<input type=\"radio\"  name=\"email\" id=\"rad" + k + "\" /></label> <input type=\"text\" class=\"f-text\" onfocus=\"setChecked('rad" + k + "');\"  id=\"newemail\" size=\"30\" />";
            k++;
            this.txtRadioCount.Value = k.ToString();
        }
	}
}
