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

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

namespace HISClinic.Controls
{
	/// <summary>
	/// PatientInfo 的摘要说明。
	/// </summary>
	public class PatientInfo : System.Windows.Forms.UserControl
	{
		//获取时病人信息之后触发的事件
		public event OnGetPatientInfoCompleted GetPatientInfoCompleted;
        public event OnGetPatientInfoFailed GetPatientInfoFailed;
		
		private Patients.t_PatientRow _PatientRow;

		private System.Windows.Forms.Label lblCardNumber;
		private System.Windows.Forms.TextBox txtCardNumber;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.Label lblSex;
		private System.Windows.Forms.Label lblAge;
		private System.Windows.Forms.Label lblTelephone;
		private System.Windows.Forms.Label lblNameValue;
		private System.Windows.Forms.Label lblSexValue;
		private System.Windows.Forms.Label lblTelephoneValue;
		private System.Windows.Forms.Label lblMobile;
		private System.Windows.Forms.Label lblAgeValue;
		private System.Windows.Forms.Label lblMobileValue;

		/// <summary>
		/// 供控件外部的程序引用病人的信息
		/// </summary>
		public Patients.t_PatientRow PatientRow 
		{
			get
			{
				return _PatientRow;
			}
		}

		/// <summary> 
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public PatientInfo()
		{
			// 该调用是 Windows.Forms 窗体设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitializeComponent 调用后添加任何初始化

		}

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

		#region 组件设计器生成的代码
		/// <summary> 
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.lblCardNumber = new System.Windows.Forms.Label();
			this.txtCardNumber = new System.Windows.Forms.TextBox();
			this.lblName = new System.Windows.Forms.Label();
			this.lblSex = new System.Windows.Forms.Label();
			this.lblAge = new System.Windows.Forms.Label();
			this.lblTelephone = new System.Windows.Forms.Label();
			this.lblNameValue = new System.Windows.Forms.Label();
			this.lblSexValue = new System.Windows.Forms.Label();
			this.lblMobile = new System.Windows.Forms.Label();
			this.lblAgeValue = new System.Windows.Forms.Label();
			this.lblTelephoneValue = new System.Windows.Forms.Label();
			this.lblMobileValue = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblCardNumber
			// 
			this.lblCardNumber.AutoSize = true;
			this.lblCardNumber.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblCardNumber.Location = new System.Drawing.Point(8, 8);
			this.lblCardNumber.Name = "lblCardNumber";
			this.lblCardNumber.Size = new System.Drawing.Size(42, 17);
			this.lblCardNumber.TabIndex = 0;
			this.lblCardNumber.Text = "卡号：";
			// 
			// txtCardNumber
			// 
			this.txtCardNumber.Location = new System.Drawing.Point(48, 4);
			this.txtCardNumber.MaxLength = 15;
			this.txtCardNumber.Name = "txtCardNumber";
			this.txtCardNumber.Size = new System.Drawing.Size(112, 21);
			this.txtCardNumber.TabIndex = 0;
			this.txtCardNumber.Text = "";
			this.txtCardNumber.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtCardNumber_KeyPress);
			// 
			// lblName
			// 
			this.lblName.AutoSize = true;
			this.lblName.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblName.Location = new System.Drawing.Point(176, 8);
			this.lblName.Name = "lblName";
			this.lblName.Size = new System.Drawing.Size(42, 17);
			this.lblName.TabIndex = 2;
			this.lblName.Text = "姓名：";
			// 
			// lblSex
			// 
			this.lblSex.AutoSize = true;
			this.lblSex.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblSex.Location = new System.Drawing.Point(296, 8);
			this.lblSex.Name = "lblSex";
			this.lblSex.Size = new System.Drawing.Size(42, 17);
			this.lblSex.TabIndex = 3;
			this.lblSex.Text = "性别：";
			// 
			// lblAge
			// 
			this.lblAge.AutoSize = true;
			this.lblAge.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblAge.Location = new System.Drawing.Point(392, 8);
			this.lblAge.Name = "lblAge";
			this.lblAge.Size = new System.Drawing.Size(42, 17);
			this.lblAge.TabIndex = 4;
			this.lblAge.Text = "年龄：";
			// 
			// lblTelephone
			// 
			this.lblTelephone.AutoSize = true;
			this.lblTelephone.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblTelephone.Location = new System.Drawing.Point(8, 40);
			this.lblTelephone.Name = "lblTelephone";
			this.lblTelephone.Size = new System.Drawing.Size(42, 17);
			this.lblTelephone.TabIndex = 5;
			this.lblTelephone.Text = "电话：";
			// 
			// lblNameValue
			// 
			this.lblNameValue.Location = new System.Drawing.Point(216, 8);
			this.lblNameValue.Name = "lblNameValue";
			this.lblNameValue.Size = new System.Drawing.Size(69, 17);
			this.lblNameValue.TabIndex = 6;
			this.lblNameValue.Text = "[姓名]";
			// 
			// lblSexValue
			// 
			this.lblSexValue.Location = new System.Drawing.Point(336, 8);
			this.lblSexValue.Name = "lblSexValue";
			this.lblSexValue.Size = new System.Drawing.Size(48, 17);
			this.lblSexValue.TabIndex = 7;
			this.lblSexValue.Text = "[卡号]";
			// 
			// lblMobile
			// 
			this.lblMobile.AutoSize = true;
			this.lblMobile.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblMobile.Location = new System.Drawing.Point(176, 40);
			this.lblMobile.Name = "lblMobile";
			this.lblMobile.Size = new System.Drawing.Size(42, 17);
			this.lblMobile.TabIndex = 9;
			this.lblMobile.Text = "手机：";
			// 
			// lblAgeValue
			// 
			this.lblAgeValue.Location = new System.Drawing.Point(432, 8);
			this.lblAgeValue.Name = "lblAgeValue";
			this.lblAgeValue.Size = new System.Drawing.Size(48, 17);
			this.lblAgeValue.TabIndex = 10;
			this.lblAgeValue.Text = "[年龄]";
			// 
			// lblTelephoneValue
			// 
			this.lblTelephoneValue.Location = new System.Drawing.Point(48, 40);
			this.lblTelephoneValue.Name = "lblTelephoneValue";
			this.lblTelephoneValue.Size = new System.Drawing.Size(112, 17);
			this.lblTelephoneValue.TabIndex = 11;
			this.lblTelephoneValue.Text = "[电话]";
			// 
			// lblMobileValue
			// 
			this.lblMobileValue.Location = new System.Drawing.Point(216, 40);
			this.lblMobileValue.Name = "lblMobileValue";
			this.lblMobileValue.Size = new System.Drawing.Size(112, 17);
			this.lblMobileValue.TabIndex = 12;
			this.lblMobileValue.Text = "[手机]";
			// 
			// PatientInfo
			// 
			this.Controls.Add(this.lblMobileValue);
			this.Controls.Add(this.lblTelephoneValue);
			this.Controls.Add(this.lblAgeValue);
			this.Controls.Add(this.lblMobile);
			this.Controls.Add(this.lblSexValue);
			this.Controls.Add(this.lblNameValue);
			this.Controls.Add(this.lblTelephone);
			this.Controls.Add(this.lblAge);
			this.Controls.Add(this.lblSex);
			this.Controls.Add(this.lblName);
			this.Controls.Add(this.txtCardNumber);
			this.Controls.Add(this.lblCardNumber);
			this.Name = "PatientInfo";
			this.Size = new System.Drawing.Size(472, 64);
			this.Load += new System.EventHandler(this.PatientInfo_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void txtCardNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if((char)Keys.Enter==e.KeyChar)
			{
				//是否没有输入卡号
				if(txtCardNumber.Text.Trim().Length==0)
				{
					MessageProcess.ShowInformation("请输入一个有效的卡号。");
					txtCardNumber.Focus();
					return;
				}

				PatientsEx patient=(new Patient()).FindPatientByCardNumber(txtCardNumber.Text);

				if(patient.t_Patient.Rows.Count>0)
				{
					_PatientRow=(Patients.t_PatientRow)patient.t_Patient.Rows[0];
					
					lblNameValue.Text=_PatientRow.Name;
					lblSexValue.Text=(_PatientRow.Sex)?"男":"女";
					lblAgeValue.Text=(_PatientRow[PatientsEx.t_PatientDataTableEx.FIELD_Age]!=DBNull.Value)?_PatientRow.Age.ToString():"";
					lblTelephoneValue.Text=_PatientRow.Telephone;
					lblMobileValue.Text=_PatientRow.Mobile;		
			
					if(GetPatientInfoCompleted!=null)
					{
						GetPatientInfoCompleted();
					}
				}
				else
				{
					_PatientRow=null;
					MessageProcess.ShowInformation("未能找到持有该卡的病人信息。");		
					WaitForFindNextPatient();

					if(GetPatientInfoFailed!=null)
					{
						GetPatientInfoFailed();
					}
				}
			}
		}

		private void PatientInfo_Load(object sender, System.EventArgs e)
		{
			WaitForFindNextPatient();
		}

		/// <summary>
		/// 将显示病人信息的控件设为初始状态，等待显示下一个病人的信息
		/// </summary>
		public void WaitForFindNextPatient()
		{
			_PatientRow=null;
			
			lblNameValue.Text="";
			lblSexValue.Text="";
			lblAgeValue.Text="";
			lblTelephoneValue.Text="";
			lblMobileValue.Text="";

			txtCardNumber.Focus();
			txtCardNumber.SelectAll();
		}
	}
	public delegate void OnGetPatientInfoCompleted();
	public delegate void OnGetPatientInfoFailed();
}
