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 System.Web.Security;
	using SK.MemberBLL;

	/// <summary>
	///		cu_LogonInfo 的摘要说明。
	/// </summary>
	public class cu_LogonInfo : BaseControl
	{
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbLogin;
		protected System.Web.UI.WebControls.ImageButton btnLogin;
		protected System.Web.UI.WebControls.TextBox txtMemberName;
		protected System.Web.UI.WebControls.TextBox txtPwd;
		protected System.Web.UI.HtmlControls.HtmlGenericControl tbUser;
		protected System.Web.UI.HtmlControls.HtmlInputHidden currhref;
		protected System.Web.UI.WebControls.HyperLink lnkUser;
		protected System.Web.UI.WebControls.LinkButton lnkLogout;
		protected System.Web.UI.WebControls.TextBox txtVerifyCode;
		MemberSystem member = new MemberSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			string scriptHref ="<script language=JavaScript>document.getElementById('"+currhref.ClientID+"').value=window.location.href;<";
			scriptHref += "/" + "script>";
			if(!Page.IsStartupScriptRegistered("HrefScript"))
				Page.RegisterStartupScript("HrefScript",scriptHref);

			this.GiveFocus(txtMemberName);
			txtMemberName.Attributes["onkeypress"] = GetExecuteControlScript(btnLogin);
			txtPwd.Attributes["onkeypress"] = GetExecuteControlScript(btnLogin);
			btnLogin.Attributes["onclick"] = "return LogonInfo_checklogin();";

			if(!IsPostBack)
			{
				//lnkUser.NavigateUrl = this.MemberURL;
				if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
				{
					tbLogin.Visible = false;
					tbUser.Visible = true;
					_MemberName = this.CurrentMemberName.ToString();
				}
				else
				{
					tbLogin.Visible = true;
					tbUser.Visible = false;
				}
			}
		}

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

		}
		#endregion
		string _MemberName;
		public string MemberName
		{
			get{return _MemberName;}
			set{_MemberName=value;}
		}
		private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if(Session["verifycode"]==null)
			{
				this.DisplayAlert("超时，验证码丢失，请刷新登录页");
				return;
			}
			if(txtVerifyCode.Text.ToUpper()!=Session["verifycode"].ToString().ToUpper())
			{
				this.DisplayAlert("验证码错误");
				return;
			}
			BasePape des = new BasePape();
			des.EncryptKey = "sorkednc";
			des.InputString = txtPwd.Text;
			des.DesEncrypt();
			string EncryptString = des.OutString;
			object MemberID    = member.Login(txtMemberName.Text,EncryptString);
			//if(member.LoginOK(txtMemberName.Text,EncryptString))
			if(MemberID != null)
			{
				DataRow info = member.Info(MemberID);
				//MainWS.Main1 main = new MainWS.Main1();
				if(Convert.ToInt32(Main.GetValue("NeedValidEmail"))==1 && !Convert.ToBoolean(info["EmailHasValid"]))
				{
					Response.Redirect("~/NeedValidEmail.aspx");
				}
				FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
					MemberID +";"+txtMemberName.Text,
					DateTime.Now,
					DateTime.Now.AddMinutes(20),
					true,
					info["Roles"].ToString() ,
					FormsAuthentication.FormsCookiePath);
				string hash = FormsAuthentication.Encrypt(ticket);
				HttpCookie cookie = new HttpCookie(
					FormsAuthentication.FormsCookieName, 
					hash); 
				cookie.Domain = this.Domain;

				if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

				Response.Cookies.Add(cookie);

				/*string returnUrl = Request.QueryString["ReturnUrl"];
				if (returnUrl == null) returnUrl = "/Users/";
				Response.Redirect(returnUrl);*/
				Response.Redirect(currhref.Value);
			}
			else
			{
				//log.Add(null,"后台以用户名["+txtLoginID.Text+"]登录失败",WebLogType.Security);
				this.DisplayAlert("帐号或密码错误，请检查后重新登录！");
			}
		}

		private void lnkLogout_Click(object sender, System.EventArgs e)
		{
			FormsAuthentication.SignOut();
			FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
				this.CurrentMemberID+";"+this.CurrentMemberName ,
				DateTime.Now,
				DateTime.Now.AddMinutes(-20),
				false,
				"",
				FormsAuthentication.FormsCookiePath);
			string hash = FormsAuthentication.Encrypt(ticket);
			HttpCookie cookie = new HttpCookie(
				FormsAuthentication.FormsCookieName, 
				hash);
			cookie.Domain = this.Domain;

			if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
			

			System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

			Response.Redirect("~/");
		}

		
	}
}
