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 Spaces;
public partial class Admin_UpdateUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RegScript();
        if (!this.IsPostBack)
        {
            BindUser();
        }
    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
        Spaces.User user = Users.GetUserByAccount(SpacesContext.Current.Context.User.Identity.Name);
        user.Address = Utils.InputText(txtAddress.Text, 200);
        user.Email = Utils.InputText(txtEmail.Text, 30);
        user.IdNumber = Utils.InputText(txtIDCard.Text, 20);
        user.Phone = Utils.InputText(txtPhone.Text, 20);
        user.Postcode = Utils.InputText(txtPostcode.Text, 20);
        user.NickName = Utils.InputText(txtTrueName.Text, 20);
        user.City = Utils.InputText(Request.Form["City"], 30);
        if (chkPassword.Checked)
        {
            if (Utils.PasswordFormat(txtPassword.Text).ToLower() != user.Password.ToLower())
            {
                string script = "<script>alert('修改密码原密码输入错误！');history.go(-1);</script>";
                this.RegisterClientScriptBlock("versdysdrr", script);
                return;
            }

            if (txtNewPassword1.Text.Trim() != string.Empty && txtNewPassword1.Text != txtNewPassword2.Text)
            {
                string script = "<script>alert('新密码和确认密码不一置！');history.go(-1);</script>";
                this.RegisterClientScriptBlock("sdsdr", script);
                return;
            }

            user.Password = Utils.PasswordFormat(txtNewPassword1.Text);  
        
        }
        Response.Write(Gender.SelectedValue);
        if (Gender.SelectedValue!="") user.Gender = (Gender)int.Parse(Gender.SelectedValue);
        try
        {
            user.Birthday = new DateTime(Convert.ToInt32(dropYear.SelectedValue), Convert.ToInt32(dropMonth.SelectedValue), Convert.ToInt32(DropDay.SelectedValue));
        }
        catch
        {
            user.Birthday = new DateTime(1900, 1, 1);
        }

        Users.UpdateUser(user);

        this.BindUser();

        Message.Text = Msg.GetMsg("您已成功更改了个人信息！");
    }
    void BindUser()
    {
        Spaces.User user = Users.GetUserByAccount(SpacesContext.Current.Context.User.Identity.Name);
        this.lblAccount.Text = user.Account;
        this.txtAddress.Text = user.Address;
        this.txtEmail.Text = user.Email;
        this.txtIDCard.Text = user.IdNumber;
        this.txtPhone.Text = user.Phone;
        this.txtPostcode.Text = user.Postcode;
        this.txtTrueName.Text = user.NickName;

        dropYear.Items.Clear();

        for (int y = 1930; y < 2007; y++)
        {
            ListItem yli = new ListItem(y.ToString(), y.ToString());
            if (y.ToString() == (user.Birthday.Year.ToString()))
                yli.Selected = true;

            dropYear.Items.Add(yli);
        }

        dropMonth.Items.Clear();
        for (int m = 1; m < 13; m++)
        {
            ListItem mli = new ListItem(m.ToString(), m.ToString());
            if (m.ToString() == (user.Birthday.Month.ToString()))
                mli.Selected = true;
            dropMonth.Items.Add(mli);
        }
        DropDay.Items.Clear();
        for (int d = 1; d < 32; d++)
        {
            ListItem dli = new ListItem(d.ToString(), d.ToString());
            if (d.ToString() == (user.Birthday.Day.ToString()))
                dli.Selected = true;
            DropDay.Items.Add(dli);
        }

        foreach (ListItem li in Gender.Items)
        {
            li.Selected = false;
            if (li.Value == ((int)user.Gender).ToString())
                li.Selected = true;
        }

        #region 设置城市脚本
        string script = string.Empty;
        if (user.City == null || user.City.Trim() == string.Empty)
            return;
        string province = string.Empty;
        if (user.City.Length>=3&&(user.City.Substring(0, 3) == "黑龙江" || user.City.Substring(0, 3) == "内蒙古"))
            province = user.City.Substring(0, 3);
        else
            province = user.City.Substring(0, 2);
        script = @"<script language=javascript>                                             
                                    
  function CheckSelect(selectname,CheckValue)                                             
  { var len = selectname.length                                             
    for (var i = 1; i<= len ;++i)                                             
     { if (selectname[i-1].value==CheckValue)                                             
         { selectname[i-1].selected = true;                                             
           break;                                             
          }                                             
     }                                             
  }                                                                                  
CheckSelect(document.forms[0].prov,'";


        script += province + "')" +
            "\n	selectcity();\n" +

            "CheckSelect(document.forms[0].city,'" +
            user.City + "')" +

            "</script>      ";
        this.RegisterStartupScript("city", script);
        #endregion



      
    }

    void RegScript()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<SCRIPT LANGUAGE=\"JavaScript\">\n");
        sb.Append("function psdisplay()\n{");
        sb.AppendFormat(" \nif(document.{0}.{1}.checked)\n", this.formbill.ClientID, chkPassword.ClientID.Replace("_", "$"));
        sb.Append("{ps1.style.display=\"block\"; }\n else{ ps1.style.display=\"none\"; }}</script>");

        RegisterStartupScript("chkpass", sb.ToString());

    }
}
