using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using HISClinic.DataModel;
using HISClinic.BLL;
using HISClinic.UIProcess;

namespace HISClinic
{
	/// <summary>
	/// frmLogin 的摘要说明。
	/// </summary>
	public class frmLogin : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblUserName;
		private System.Windows.Forms.TextBox txtUserName;
		private System.Windows.Forms.Button btnConfirm;
		private System.Windows.Forms.Label lblPassword;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.Button btnLoginWiz;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmLogin()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmLogin));
			this.lblUserName = new System.Windows.Forms.Label();
			this.txtUserName = new System.Windows.Forms.TextBox();
			this.btnConfirm = new System.Windows.Forms.Button();
			this.lblPassword = new System.Windows.Forms.Label();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.btnCancel = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.btnLoginWiz = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// lblUserName
			// 
			this.lblUserName.AutoSize = true;
			this.lblUserName.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblUserName.Location = new System.Drawing.Point(13, 70);
			this.lblUserName.Name = "lblUserName";
			this.lblUserName.Size = new System.Drawing.Size(54, 17);
			this.lblUserName.TabIndex = 0;
			this.lblUserName.Text = "用户名：";
			// 
			// txtUserName
			// 
			this.txtUserName.Location = new System.Drawing.Point(73, 67);
			this.txtUserName.MaxLength = 20;
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new System.Drawing.Size(150, 20);
			this.txtUserName.TabIndex = 0;
			this.txtUserName.Text = "reg1";
			// 
			// btnConfirm
			// 
			this.btnConfirm.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.btnConfirm.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnConfirm.BackgroundImage")));
			this.btnConfirm.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnConfirm.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnConfirm.ForeColor = System.Drawing.SystemColors.Desktop;
			this.btnConfirm.Location = new System.Drawing.Point(62, 126);
			this.btnConfirm.Name = "btnConfirm";
			this.btnConfirm.Size = new System.Drawing.Size(54, 22);
			this.btnConfirm.TabIndex = 2;
			this.btnConfirm.Text = "确 定";
			this.btnConfirm.Click += new System.EventHandler(this.btnConfirm_Click);
			// 
			// lblPassword
			// 
			this.lblPassword.AutoSize = true;
			this.lblPassword.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblPassword.Location = new System.Drawing.Point(13, 99);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new System.Drawing.Size(54, 17);
			this.lblPassword.TabIndex = 0;
			this.lblPassword.Text = "密  码：";
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(73, 97);
			this.txtPassword.MaxLength = 20;
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(150, 20);
			this.txtPassword.TabIndex = 1;
			this.txtPassword.Text = "P@ssword";
			// 
			// btnCancel
			// 
			this.btnCancel.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.btnCancel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnCancel.BackgroundImage")));
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnCancel.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnCancel.ForeColor = System.Drawing.SystemColors.Desktop;
			this.btnCancel.Location = new System.Drawing.Point(123, 126);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(54, 22);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "取 消";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(0, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(267, 46);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 4;
			this.pictureBox1.TabStop = false;
			// 
			// pictureBox2
			// 
			this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
			this.pictureBox2.Location = new System.Drawing.Point(0, 46);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(267, 5);
			this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox2.TabIndex = 5;
			this.pictureBox2.TabStop = false;
			// 
			// btnLoginWiz
			// 
			this.btnLoginWiz.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.btnLoginWiz.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnLoginWiz.BackgroundImage")));
			this.btnLoginWiz.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnLoginWiz.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnLoginWiz.Location = new System.Drawing.Point(182, 126);
			this.btnLoginWiz.Name = "btnLoginWiz";
			this.btnLoginWiz.Size = new System.Drawing.Size(66, 22);
			this.btnLoginWiz.TabIndex = 6;
			this.btnLoginWiz.Text = "登录指引";
			this.btnLoginWiz.Click += new System.EventHandler(this.btnLoginWiz_Click);
			// 
			// frmLogin
			// 
			this.AcceptButton = this.btnConfirm;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.Control;
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(259, 155);
			this.Controls.Add(this.btnLoginWiz);
			this.Controls.Add(this.pictureBox2);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.txtPassword);
			this.Controls.Add(this.lblPassword);
			this.Controls.Add(this.txtUserName);
			this.Controls.Add(this.lblUserName);
			this.Controls.Add(this.btnConfirm);
			this.ForeColor = System.Drawing.SystemColors.Desktop;
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmLogin";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "用户登录";
			this.Load += new System.EventHandler(this.frmLogin_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmLogin_Load(object sender, System.EventArgs e)
		{
		
		}

		private void btnConfirm_Click(object sender, System.EventArgs e)
		{
			//判断用户名是否为空
			if(txtUserName.Text.Trim().Length == 0)
			{
				MessageProcess.ShowWarning("请输入一个有效的用户名。");
				txtUserName.Focus();
				return;
			}
			
			//判断密码是否为空
			if(txtPassword.Text.Trim().Length == 0)
			{
				MessageProcess.ShowWarning("请输入密码。");
				txtPassword.Focus();
				return;
			}			 
			
			User.LoginResult loginResult;
			UsersEx usersEx=(new User()).Login(txtUserName.Text,txtPassword.Text,out  loginResult);

			switch(loginResult)
			{
				//登录成功
				case User.LoginResult.Success:	
					
					//缓存已登录成功的用户信息
					CacheDataManager.SetCacheData(UsersEx.t_UserDataTableEx.TableName_User,usersEx);
					this.DialogResult=DialogResult.OK;					
					break;
				
				case User.LoginResult.InvalidLoginName:
					MessageProcess.ShowError("输入的用户名无效！请重新输入。");
					txtUserName.Focus();
					break;

				case User.LoginResult.InvalidPassword:
					MessageProcess.ShowError("输入的密码错误！请重新输入。");
					txtPassword.Focus();
					break;
			}			
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnLoginWiz_Click(object sender, System.EventArgs e)
		{
			frmLoginWiz frm=new frmLoginWiz();
			frm.ShowDialog();
		}
	}
}
