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>
	/// frmPatientEdit 的摘要说明。
	/// </summary>
	public class frmPatientEdit : System.Windows.Forms.Form
	{
		private PatientsEx.t_PatientRow _patientRow;

		private System.Windows.Forms.RadioButton rdoFemale;
		private System.Windows.Forms.RadioButton rdoMale;
		private System.Windows.Forms.DateTimePicker dtpBirthDate;
		private System.Windows.Forms.TextBox txtAddress;
		private System.Windows.Forms.TextBox txtMobile;
		private System.Windows.Forms.TextBox txtTelephone;
		private System.Windows.Forms.TextBox txtCardNumber;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Label lblAddress;
		private System.Windows.Forms.Label lblMobile;
		private System.Windows.Forms.Label lblTelephone;
		private System.Windows.Forms.Label lblBirthDate;
		private System.Windows.Forms.Label lblSex;
		private System.Windows.Forms.Label lblCardNumber;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnFindPatient;
		private System.Windows.Forms.GroupBox grpPatientInfo;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.PictureBox pictureBox3;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmPatientEdit()
		{
			//
			// 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(frmPatientEdit));
			this.rdoFemale = new System.Windows.Forms.RadioButton();
			this.rdoMale = new System.Windows.Forms.RadioButton();
			this.dtpBirthDate = new System.Windows.Forms.DateTimePicker();
			this.txtAddress = new System.Windows.Forms.TextBox();
			this.txtMobile = new System.Windows.Forms.TextBox();
			this.txtTelephone = new System.Windows.Forms.TextBox();
			this.txtCardNumber = new System.Windows.Forms.TextBox();
			this.txtName = new System.Windows.Forms.TextBox();
			this.lblAddress = new System.Windows.Forms.Label();
			this.lblMobile = new System.Windows.Forms.Label();
			this.lblTelephone = new System.Windows.Forms.Label();
			this.lblBirthDate = new System.Windows.Forms.Label();
			this.lblSex = new System.Windows.Forms.Label();
			this.lblCardNumber = new System.Windows.Forms.Label();
			this.lblName = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnFindPatient = new System.Windows.Forms.Button();
			this.grpPatientInfo = new System.Windows.Forms.GroupBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.pictureBox3 = new System.Windows.Forms.PictureBox();
			this.grpPatientInfo.SuspendLayout();
			this.SuspendLayout();
			// 
			// rdoFemale
			// 
			this.rdoFemale.Location = new System.Drawing.Point(360, 70);
			this.rdoFemale.Name = "rdoFemale";
			this.rdoFemale.Size = new System.Drawing.Size(40, 24);
			this.rdoFemale.TabIndex = 3;
			this.rdoFemale.Text = "女";
			// 
			// rdoMale
			// 
			this.rdoMale.Checked = true;
			this.rdoMale.Location = new System.Drawing.Point(310, 70);
			this.rdoMale.Name = "rdoMale";
			this.rdoMale.Size = new System.Drawing.Size(40, 24);
			this.rdoMale.TabIndex = 2;
			this.rdoMale.TabStop = true;
			this.rdoMale.Text = "男";
			// 
			// dtpBirthDate
			// 
			this.dtpBirthDate.CustomFormat = "yyyy-MM";
			this.dtpBirthDate.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.dtpBirthDate.Location = new System.Drawing.Point(310, 100);
			this.dtpBirthDate.Name = "dtpBirthDate";
			this.dtpBirthDate.ShowUpDown = true;
			this.dtpBirthDate.Size = new System.Drawing.Size(70, 21);
			this.dtpBirthDate.TabIndex = 5;
			// 
			// txtAddress
			// 
			this.txtAddress.Location = new System.Drawing.Point(90, 160);
			this.txtAddress.MaxLength = 80;
			this.txtAddress.Name = "txtAddress";
			this.txtAddress.Size = new System.Drawing.Size(300, 21);
			this.txtAddress.TabIndex = 8;
			this.txtAddress.Text = "";
			// 
			// txtMobile
			// 
			this.txtMobile.Location = new System.Drawing.Point(90, 130);
			this.txtMobile.MaxLength = 15;
			this.txtMobile.Name = "txtMobile";
			this.txtMobile.Size = new System.Drawing.Size(130, 21);
			this.txtMobile.TabIndex = 7;
			this.txtMobile.Text = "";
			// 
			// txtTelephone
			// 
			this.txtTelephone.Location = new System.Drawing.Point(90, 100);
			this.txtTelephone.MaxLength = 15;
			this.txtTelephone.Name = "txtTelephone";
			this.txtTelephone.Size = new System.Drawing.Size(130, 21);
			this.txtTelephone.TabIndex = 6;
			this.txtTelephone.Text = "";
			// 
			// txtCardNumber
			// 
			this.txtCardNumber.Location = new System.Drawing.Point(90, 31);
			this.txtCardNumber.MaxLength = 15;
			this.txtCardNumber.Name = "txtCardNumber";
			this.txtCardNumber.Size = new System.Drawing.Size(130, 21);
			this.txtCardNumber.TabIndex = 0;
			this.txtCardNumber.Text = "";
			// 
			// txtName
			// 
			this.txtName.Location = new System.Drawing.Point(90, 70);
			this.txtName.MaxLength = 20;
			this.txtName.Name = "txtName";
			this.txtName.Size = new System.Drawing.Size(130, 21);
			this.txtName.TabIndex = 4;
			this.txtName.Text = "";
			// 
			// lblAddress
			// 
			this.lblAddress.AutoSize = true;
			this.lblAddress.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblAddress.Location = new System.Drawing.Point(15, 160);
			this.lblAddress.Name = "lblAddress";
			this.lblAddress.Size = new System.Drawing.Size(66, 17);
			this.lblAddress.TabIndex = 117;
			this.lblAddress.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(15, 130);
			this.lblMobile.Name = "lblMobile";
			this.lblMobile.Size = new System.Drawing.Size(66, 17);
			this.lblMobile.TabIndex = 116;
			this.lblMobile.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(15, 100);
			this.lblTelephone.Name = "lblTelephone";
			this.lblTelephone.Size = new System.Drawing.Size(66, 17);
			this.lblTelephone.TabIndex = 114;
			this.lblTelephone.Text = "联系电话：";
			// 
			// lblBirthDate
			// 
			this.lblBirthDate.AutoSize = true;
			this.lblBirthDate.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lblBirthDate.Location = new System.Drawing.Point(240, 100);
			this.lblBirthDate.Name = "lblBirthDate";
			this.lblBirthDate.Size = new System.Drawing.Size(66, 17);
			this.lblBirthDate.TabIndex = 111;
			this.lblBirthDate.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(240, 70);
			this.lblSex.Name = "lblSex";
			this.lblSex.Size = new System.Drawing.Size(54, 17);
			this.lblSex.TabIndex = 109;
			this.lblSex.Text = "性  别：";
			// 
			// lblCardNumber
			// 
			this.lblCardNumber.AutoSize = true;
			this.lblCardNumber.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(186)), ((System.Byte)(209)), ((System.Byte)(247)));
			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(25, 35);
			this.lblCardNumber.Name = "lblCardNumber";
			this.lblCardNumber.Size = new System.Drawing.Size(54, 17);
			this.lblCardNumber.TabIndex = 108;
			this.lblCardNumber.Text = "卡  号：";
			this.lblCardNumber.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			// 
			// 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(25, 70);
			this.lblName.Name = "lblName";
			this.lblName.Size = new System.Drawing.Size(54, 17);
			this.lblName.TabIndex = 106;
			this.lblName.Text = "姓  名：";
			// 
			// btnCancel
			// 
			this.btnCancel.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.btnCancel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnCancel.BackgroundImage")));
			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.Location = new System.Drawing.Point(376, 204);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(64, 23);
			this.btnCancel.TabIndex = 10;
			this.btnCancel.Text = "取 消";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnSave
			// 
			this.btnSave.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.btnSave.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnSave.BackgroundImage")));
			this.btnSave.Enabled = false;
			this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnSave.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnSave.Location = new System.Drawing.Point(296, 204);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(64, 23);
			this.btnSave.TabIndex = 9;
			this.btnSave.Text = "保 存";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnFindPatient
			// 
			this.btnFindPatient.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.btnFindPatient.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnFindPatient.BackgroundImage")));
			this.btnFindPatient.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnFindPatient.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnFindPatient.Location = new System.Drawing.Point(220, 30);
			this.btnFindPatient.Name = "btnFindPatient";
			this.btnFindPatient.TabIndex = 1;
			this.btnFindPatient.Text = "显示信息";
			this.btnFindPatient.Click += new System.EventHandler(this.btnFindPatient_Click);
			// 
			// grpPatientInfo
			// 
			this.grpPatientInfo.BackColor = System.Drawing.Color.AliceBlue;
			this.grpPatientInfo.Controls.Add(this.pictureBox2);
			this.grpPatientInfo.Controls.Add(this.pictureBox1);
			this.grpPatientInfo.Controls.Add(this.lblMobile);
			this.grpPatientInfo.Controls.Add(this.lblAddress);
			this.grpPatientInfo.Controls.Add(this.rdoFemale);
			this.grpPatientInfo.Controls.Add(this.txtName);
			this.grpPatientInfo.Controls.Add(this.txtCardNumber);
			this.grpPatientInfo.Controls.Add(this.txtTelephone);
			this.grpPatientInfo.Controls.Add(this.txtMobile);
			this.grpPatientInfo.Controls.Add(this.txtAddress);
			this.grpPatientInfo.Controls.Add(this.lblName);
			this.grpPatientInfo.Controls.Add(this.btnFindPatient);
			this.grpPatientInfo.Controls.Add(this.lblCardNumber);
			this.grpPatientInfo.Controls.Add(this.dtpBirthDate);
			this.grpPatientInfo.Controls.Add(this.lblSex);
			this.grpPatientInfo.Controls.Add(this.lblBirthDate);
			this.grpPatientInfo.Controls.Add(this.rdoMale);
			this.grpPatientInfo.Controls.Add(this.lblTelephone);
			this.grpPatientInfo.Controls.Add(this.pictureBox3);
			this.grpPatientInfo.ForeColor = System.Drawing.SystemColors.Desktop;
			this.grpPatientInfo.Location = new System.Drawing.Point(6, 6);
			this.grpPatientInfo.Name = "grpPatientInfo";
			this.grpPatientInfo.Size = new System.Drawing.Size(440, 194);
			this.grpPatientInfo.TabIndex = 118;
			this.grpPatientInfo.TabStop = false;
			// 
			// pictureBox2
			// 
			this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
			this.pictureBox2.Location = new System.Drawing.Point(0, 25);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(440, 5);
			this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox2.TabIndex = 119;
			this.pictureBox2.TabStop = false;
			// 
			// 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(440, 25);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 118;
			this.pictureBox1.TabStop = false;
			// 
			// pictureBox3
			// 
			this.pictureBox3.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox3.BackgroundImage")));
			this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox3.Location = new System.Drawing.Point(0, 30);
			this.pictureBox3.Name = "pictureBox3";
			this.pictureBox3.Size = new System.Drawing.Size(440, 23);
			this.pictureBox3.TabIndex = 120;
			this.pictureBox3.TabStop = false;
			// 
			// frmPatientEdit
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.ClientSize = new System.Drawing.Size(454, 232);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.grpPatientInfo);
			this.ForeColor = System.Drawing.SystemColors.Desktop;
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmPatientEdit";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "修改信息";
			this.grpPatientInfo.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void btnFindPatient_Click(object sender, System.EventArgs e)
		{
			if(txtCardNumber.Text.Trim().Length<=0)
			{
				MessageProcess.ShowInformation("卡号不能为空值，请输入一个有效的卡号。");
				txtCardNumber.Focus();
				return;
			}
			try
			{
				PatientsEx patients=(new Patient()).FindPatientByCardNumber(txtCardNumber.Text);
				_patientRow=(patients.t_Patient.Rows.Count>0)?(Patients.t_PatientRow)patients.t_Patient.Rows[0]:null;

				//显示找到的病人信息
				if(_patientRow!=null)
				{
					txtCardNumber.Text=_patientRow.CardNumber;
					txtName.Text=_patientRow.Name;
					rdoMale.Checked=_patientRow.Sex;
					rdoFemale.Checked=!rdoMale.Checked;

					if(_patientRow[PatientsEx.t_PatientDataTableEx.FIELD_BirthDate]!=DBNull.Value)
					{
						dtpBirthDate.Value=Convert.ToDateTime(_patientRow.BirthDate);
					}
					else
					{
						dtpBirthDate.Value=DateTime.Today;
					}
					txtTelephone.Text=_patientRow.Telephone;
					txtMobile.Text=_patientRow.Mobile;
					txtAddress.Text=_patientRow.Address;				
				}
				else
				{
					MessageProcess.ShowInformation("未能找到持有该卡的病人信息。");
					WaitForFindNextPatient();
				}
			}
			catch(Exception ex)
			{
				MessageProcess.ShowError(ex.Message);
			}
			
			btnSave.Enabled=!(_patientRow==null);
		}
		
		/// <summary>
		/// 将所有的控件设为初始状态，等待查找下一个病人的信息
		/// </summary>
		private void WaitForFindNextPatient()
		{
			txtName.Text="";
			rdoMale.Checked=true;			
			dtpBirthDate.Value=DateTime.Today;
			txtTelephone.Text="";
			txtMobile.Text="";
			txtAddress.Text="";
			
			txtCardNumber.Focus();
			txtCardNumber.SelectAll();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			//校验数据的合法性
			if(txtName.Text.Trim().Length<=0)
			{
				MessageProcess.ShowWarning("姓名不能为空值。");
				txtName.Focus();
				return;
			}

			if(txtCardNumber.Text.Trim().Length<=0)
			{
				MessageProcess.ShowWarning("卡号不能为空值。");
				txtCardNumber.Focus();
				return;
			}
			
			//判断设置的出生年月是否超过今天
			TimeSpan timeSpan=DateTime.Today-dtpBirthDate.Value.Date;
			if(timeSpan.TotalDays<0)
			{
				MessageProcess.ShowWarning("出生年月设置不合法。");
				dtpBirthDate.Focus();
				return;
			}

			try
			{
				//保存修改的病人信息
				_patientRow.Name=txtName.Text;
				_patientRow.Sex=rdoMale.Checked?true:false;				

				//如果出生日期等于当天，则认为是病人提供的出生日期不详
				if(!DateTime.Today.Equals(dtpBirthDate.Value.Date))
				{
					_patientRow.BirthDate=dtpBirthDate.Value.Date;
				}				

				_patientRow.Telephone=txtTelephone.Text.Trim();
				_patientRow.Mobile= txtMobile.Text.Trim();
				_patientRow.Address=txtAddress.Text.Trim();
				_patientRow.CardNumber=txtCardNumber.Text.Trim();

				(new Patient()).EditPatientInfo(_patientRow);
				MessageProcess.ShowInformation("保存病人信息成功!");
				this.DialogResult=DialogResult.OK;
			}
			catch(Exception ex)
			{
				MessageProcess.ShowError(ex.Message);
			}
		}
	}
}
