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_ForgetPassword 的摘要说明。
	/// </summary>
	public class cu_ForgetPassword : BaseControl
	{
		QuestionSystem question = new QuestionSystem();
		protected System.Web.UI.WebControls.TextBox txtMemberName;
		protected System.Web.UI.WebControls.DropDownList dpQuestion;
		protected System.Web.UI.WebControls.TextBox txtAnswer;
		protected Dune5.WebControls.DateTimePicker cpBirthDay;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder5;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder2;
		protected System.Web.UI.HtmlControls.HtmlGenericControl  tbMemberName;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbQuestion;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbBirthDay;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbOK;
		protected System.Web.UI.WebControls.Button btnMemberName;
		protected System.Web.UI.WebControls.Button btnQuestion;
		protected System.Web.UI.WebControls.Button btnBirthDay;
		protected System.Web.UI.WebControls.Button btnOK;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbMember_Info;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbQuestion_Info;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbBirthDay_Info;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbOK_Info;

		MemberSystem member = new MemberSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			txtMemberName.Attributes["onkeypress"] = this.GetExecuteControlScript(btnMemberName);
			txtAnswer.Attributes["onkeypress"] = this.GetExecuteControlScript(btnQuestion);
			if(!IsPostBack)
			{
				ViewState["curr"] = "MemberName";
				tbQuestion.Visible = false;
				tbBirthDay.Visible = false;
				tbOK.Visible = false;
				tbQuestion_Info.Visible = false;
				tbBirthDay_Info.Visible = false;
				tbOK_Info.Visible = false;

				BindData();
			}
		}

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

		}
		#endregion
		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()));
				}
			}
		}
		private void btnNext_Click(object sender, System.EventArgs e)
		{
			DataRow drMember = member.Info_Name(txtMemberName.Text);
			switch(ViewState["curr"].ToString())
			{
				case "MemberName":
					if(drMember!=null)
					{
						ViewState["curr"] = "Question";
						tbMemberName.Visible = false;
						tbQuestion.Visible = true;
						tbBirthDay.Visible = false;
						tbOK.Visible = false;

						tbMember_Info.Visible = false;
						tbQuestion_Info.Visible = true;
						tbBirthDay_Info.Visible = false;
						tbOK_Info.Visible = false;
					}
					else
					{
						this.DisplayAlert("帐号不存在");
						return;
					}
					break;
				case "Question":
					if(txtAnswer.Text==drMember["Answer"].ToString())
					{
						ViewState["curr"] = "BirthDay";
						tbMemberName.Visible = false;
						tbQuestion.Visible = false;
						tbBirthDay.Visible = true;
						tbOK.Visible = false;

						tbMember_Info.Visible = false;
						tbQuestion_Info.Visible = false;
						tbBirthDay_Info.Visible = true;
						tbOK_Info.Visible = false;
					}
					else
					{
						this.DisplayAlert("答案错误");
						return;
					}
					break;
				case "BirthDay":
					if(cpBirthDay.Value == Convert.ToDateTime(drMember["BirthDay"]))
					{
						ViewState["curr"] = "Default";
						tbMemberName.Visible = false;
						tbQuestion.Visible = false;
						tbBirthDay.Visible = false;
						tbOK.Visible = true;

						tbMember_Info.Visible = false;
						tbQuestion_Info.Visible = false;
						tbBirthDay_Info.Visible = false;
						tbOK_Info.Visible = true;

						BasePape des = new BasePape();
						des.DecryptKey = "sorkednc";
						des.InputString = drMember["Password"].ToString();
						des.DesDecrypt();
						string DecryptString = des.OutString;
						string body = Main.GetValue("Mail_Template_ForgetPassword_Body").ToString();
						body = body.Replace("[[MemberID]]",drMember["MemberID"].ToString());
						body = body.Replace("[[MemberName]]",drMember["MemberName"].ToString());
						body = body.Replace("[[Password]]",DecryptString);
						body = body.Replace("[[Email]]",drMember["Email"].ToString());

						string subject = Main.GetValue("Mail_Template_ForgetPassword_Subject").ToString();
						subject = subject.Replace("[[MemberID]]",drMember["MemberID"].ToString());
						subject = subject.Replace("[[MemberName]]",drMember["MemberName"].ToString());
						subject = subject.Replace("[[Password]]",DecryptString);
						subject = subject.Replace("[[Email]]",drMember["Email"].ToString());

						bool inhtml = Convert.ToBoolean(Convert.ToInt16(Main.GetValue("Mail_Template_ForgetPassword_InHtml")));
						try
						{
							Utils.SendMail(drMember["Email"].ToString(),txtMemberName.Text,subject,body,inhtml?PatNorSoft.Mail.Message.Build.BodyPartFormat.HTML:PatNorSoft.Mail.Message.Build.BodyPartFormat.Plain);
						}
						catch
						{
							this.DisplayAlert("邮件发送失败");
							return;
						}
					}
					else
					{
						this.DisplayAlert("生日错误");
						return;
					}
					break;
				default:
					Response.Redirect("~/");
					break;
			}
		}
	}
}
