namespace SK.MemberWeb.Templates.Default
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using SK.MemberBLL;

	/// <summary>
	///		cu_ProfileEdit 的摘要说明。
	/// </summary>
	public class cu_ProfileEdit : BaseControl
	{
		MemberSystem member = new MemberSystem();
		QuestionSystem question = new QuestionSystem();
		CountrySystem country = new CountrySystem();
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder5;
		protected System.Web.UI.WebControls.Label lblMemberName;
		protected System.Web.UI.WebControls.Label lblEmail;
		protected System.Web.UI.WebControls.DropDownList dpSex;
		protected System.Web.UI.WebControls.DropDownList dpQuestion;
		protected System.Web.UI.WebControls.TextBox txtAnswer;
		protected Dune5.WebControls.DateTimePicker cpBirthDay;
		protected System.Web.UI.WebControls.DropDownList dpCountry;
		protected System.Web.UI.HtmlControls.HtmlSelect dpProvince;
		protected System.Web.UI.HtmlControls.HtmlInputHidden txtProvince;
		protected System.Web.UI.WebControls.TextBox txtEmail2;
		protected System.Web.UI.WebControls.TextBox txtTrueName;
		protected System.Web.UI.WebControls.TextBox txtCity;
		protected System.Web.UI.WebControls.TextBox txtZip;
		protected System.Web.UI.WebControls.Button btnSave;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder1;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder2;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder3;
		protected System.Web.UI.WebControls.TextBox txtTel;
		protected System.Web.UI.WebControls.DropDownList lstCompany;
		protected System.Web.UI.HtmlControls.HtmlGenericControl CompanyAll;
		ProvinceSystem province = new ProvinceSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			dpCountry.Attributes["onchange"] = String.Format("CityChange(this,'{0}','{1}');",dpProvince.ClientID,txtProvince.ClientID);
			dpProvince.Attributes["onchange"] = String.Format("RegionChange(this,'{0}');",txtProvince.ClientID);
			btnSave.Attributes["onclick"] = "return Check_Input();";
			RegisterArray();

			if(!IsPostBack)
			{
				BindCountry();
				BindData();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		设计器支持所需的方法 - 不要使用代码编辑器
		///		修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		private void BindCountry()
		{
			dpCountry.DataSource = country.List();
			dpCountry.DataTextField = "CountryName";
			dpCountry.DataValueField = "CountryID";
			dpCountry.DataBind();
			if(dpCountry.Items.Count>0)
				BindProvince();
		}
		private void BindProvince()
		{
			dpProvince.DataSource = province.List(dpCountry.SelectedValue);
			dpProvince.DataTextField = "ProvinceName";
			dpProvince.DataValueField = "ProvinceID";
			dpProvince.DataBind();
		}
		private void RegisterArray()
		{
			foreach(DataRow drCountry in country.List().Tables[0].Rows)
			{
				string arrayinfo = "";
				foreach(DataRow drProvince in province.List(drCountry["CountryID"]).Tables[0].Rows)
				{
					arrayinfo+=String.Format("new Array('{0}','{1}'),",drProvince["ProvinceID"],drProvince["ProvinceName"]);
				}
				arrayinfo = arrayinfo.Substring(0,arrayinfo.Length-1);
				Page.RegisterArrayDeclaration("City"+drCountry["CountryID"].ToString(), arrayinfo);
			}
			String scriptString = "<script language=JavaScript> ";
			scriptString += String.Format("CityChange(document.getElementById('{0}'),'{1}','{2}', true);",dpCountry.ClientID,dpProvince.ClientID,txtProvince.ClientID);
			scriptString += "<";
			scriptString += "/" + "script>";
     
			Page.RegisterStartupScript("arrayScript", scriptString);

		}
		private void BindData()
		{
			using(DataSet ds = question.List())
			{
				dpQuestion.Items.Add(new ListItem("[选择一个问题]","0"));
				foreach(DataRow dr in ds.Tables[0].Rows)
				{
					dpQuestion.Items.Add(new ListItem(dr["QuestionName"].ToString(),dr["QuestionID"].ToString()));
				}
			}

			DataRow drMember = member.Info(this.CurrentMemberID);
			lblMemberName.Text = drMember["MemberName"].ToString();
			lblEmail.Text = drMember["Email"].ToString();
			dpSex.SelectedValue = drMember["Sex"].ToString();
			
			txtAnswer.Text = drMember["Answer"].ToString();
			cpBirthDay.Value = Convert.ToDateTime(drMember["BirthDay"]);
			txtEmail2.Text = drMember["Email2"].ToString();
			txtTrueName.Text = drMember["TrueName"].ToString();
			txtTel.Text = drMember["Tel"].ToString();
			try
			{
				dpQuestion.SelectedValue = drMember["QuestionID"].ToString();
				dpCountry.SelectedValue = drMember["CountryID"].ToString();
				BindProvince();
				dpProvince.Value = drMember["ProvinceID"].ToString();
				txtProvince.Value = drMember["ProvinceID"].ToString();
			}
			catch
			{
			}
			txtCity.Text = drMember["City"].ToString();
			txtZip.Text = drMember["Zip"].ToString();

			if(Convert.ToBoolean(drMember["IsCompany"]))
			{
				CompanyAll.Visible=false;
			}
			else
			{
				lstCompany.DataSource = member.MemberList_Company();
				lstCompany.DataTextField = "TrueName";
				lstCompany.DataValueField = "MemberID";
				lstCompany.DataBind();
			}
			try
			{
				lstCompany.SelectedValue = drMember["CompanyMemberID"].ToString();
			}
			catch
			{
			
			}
			
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			
			member.Update(this.CurrentMemberID,"",lblMemberName.Text,txtTrueName.Text,dpSex.SelectedValue,txtEmail2.Text,dpQuestion.SelectedValue,txtAnswer.Text,cpBirthDay.Value,txtZip.Text,dpCountry.SelectedValue,txtProvince.Value,txtCity.Text,txtTel.Text);
			if(CompanyAll.Visible)
			{
				member.UpdateCompanyID(this.CurrentMemberName,lstCompany.SelectedValue);
			}
		Response.Redirect("MemberMsg.aspx?msg="+Server.UrlEncode("资料修改成功"));
		}
	}
}
