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>
	/// frmPatientNew 的摘要说明。
	/// </summary>
	public class frmPatientNew : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.Label lblCardNumber;
		private System.Windows.Forms.Label lblSex;
		private System.Windows.Forms.Label lblBirthDate;
		private System.Windows.Forms.Label lblTelephone;
		private System.Windows.Forms.Label lblMobile;
		private System.Windows.Forms.Label lblAddress;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.TextBox txtCardNumber;
		private System.Windows.Forms.TextBox txtTelephone;
		private System.Windows.Forms.TextBox txtMobile;
		private System.Windows.Forms.TextBox txtAddress;
		private System.Windows.Forms.DateTimePicker dtpBirthDate;
		private System.Windows.Forms.RadioButton rdoMale;
		private System.Windows.Forms.RadioButton rdoFemale;
		private System.Windows.Forms.GroupBox grpPatientInfo;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.PictureBox pictureBox3;
		private System.Windows.Forms.Panel panel1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmPatientNew()
		{
			//
			// 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(frmPatientNew));
			this.btnSave = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.lblName = new System.Windows.Forms.Label();
			this.lblCardNumber = new System.Windows.Forms.Label();
			this.lblSex = new System.Windows.Forms.Label();
			this.lblBirthDate = new System.Windows.Forms.Label();
			this.lblTelephone = new System.Windows.Forms.Label();
			this.lblMobile = new System.Windows.Forms.Label();
			this.lblAddress = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.txtCardNumber = new System.Windows.Forms.TextBox();
			this.txtTelephone = new System.Windows.Forms.TextBox();
			this.txtMobile = new System.Windows.Forms.TextBox();
			this.txtAddress = new System.Windows.Forms.TextBox();
			this.dtpBirthDate = new System.Windows.Forms.DateTimePicker();
			this.rdoMale = new System.Windows.Forms.RadioButton();
			this.rdoFemale = new System.Windows.Forms.RadioButton();
			this.grpPatientInfo = new System.Windows.Forms.GroupBox();
			this.pictureBox3 = new System.Windows.Forms.PictureBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.grpPatientInfo.SuspendLayout();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnSave
			// 
			this.btnSave.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.btnSave.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnSave.BackgroundImage")));
			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(304, 216);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(56, 23);
			this.btnSave.TabIndex = 8;
			this.btnSave.Text = "保 存";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// 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, 216);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(56, 23);
			this.btnCancel.TabIndex = 9;
			this.btnCancel.Text = "取 消";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// 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(24, 47);
			this.lblName.Name = "lblName";
			this.lblName.Size = new System.Drawing.Size(54, 17);
			this.lblName.TabIndex = 4;
			this.lblName.Text = "姓  名：";
			// 
			// 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(24, 80);
			this.lblCardNumber.Name = "lblCardNumber";
			this.lblCardNumber.Size = new System.Drawing.Size(54, 17);
			this.lblCardNumber.TabIndex = 5;
			this.lblCardNumber.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(232, 47);
			this.lblSex.Name = "lblSex";
			this.lblSex.Size = new System.Drawing.Size(54, 17);
			this.lblSex.TabIndex = 6;
			this.lblSex.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(232, 80);
			this.lblBirthDate.Name = "lblBirthDate";
			this.lblBirthDate.Size = new System.Drawing.Size(66, 17);
			this.lblBirthDate.TabIndex = 7;
			this.lblBirthDate.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(24, 112);
			this.lblTelephone.Name = "lblTelephone";
			this.lblTelephone.Size = new System.Drawing.Size(66, 17);
			this.lblTelephone.TabIndex = 8;
			this.lblTelephone.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(24, 144);
			this.lblMobile.Name = "lblMobile";
			this.lblMobile.Size = new System.Drawing.Size(66, 17);
			this.lblMobile.TabIndex = 9;
			this.lblMobile.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(24, 176);
			this.lblAddress.Name = "lblAddress";
			this.lblAddress.Size = new System.Drawing.Size(66, 17);
			this.lblAddress.TabIndex = 10;
			this.lblAddress.Text = "联系地址：";
			// 
			// txtName
			// 
			this.txtName.Location = new System.Drawing.Point(96, 43);
			this.txtName.MaxLength = 20;
			this.txtName.Name = "txtName";
			this.txtName.Size = new System.Drawing.Size(120, 21);
			this.txtName.TabIndex = 0;
			this.txtName.Text = "";
			// 
			// txtCardNumber
			// 
			this.txtCardNumber.Location = new System.Drawing.Point(96, 72);
			this.txtCardNumber.MaxLength = 15;
			this.txtCardNumber.Name = "txtCardNumber";
			this.txtCardNumber.Size = new System.Drawing.Size(120, 21);
			this.txtCardNumber.TabIndex = 3;
			this.txtCardNumber.Text = "";
			// 
			// txtTelephone
			// 
			this.txtTelephone.Location = new System.Drawing.Point(96, 104);
			this.txtTelephone.MaxLength = 15;
			this.txtTelephone.Name = "txtTelephone";
			this.txtTelephone.Size = new System.Drawing.Size(120, 21);
			this.txtTelephone.TabIndex = 5;
			this.txtTelephone.Text = "";
			// 
			// txtMobile
			// 
			this.txtMobile.Location = new System.Drawing.Point(96, 136);
			this.txtMobile.MaxLength = 15;
			this.txtMobile.Name = "txtMobile";
			this.txtMobile.Size = new System.Drawing.Size(120, 21);
			this.txtMobile.TabIndex = 6;
			this.txtMobile.Text = "";
			// 
			// txtAddress
			// 
			this.txtAddress.Location = new System.Drawing.Point(96, 168);
			this.txtAddress.MaxLength = 80;
			this.txtAddress.Name = "txtAddress";
			this.txtAddress.Size = new System.Drawing.Size(296, 21);
			this.txtAddress.TabIndex = 7;
			this.txtAddress.Text = "";
			// 
			// dtpBirthDate
			// 
			this.dtpBirthDate.CustomFormat = "yyyy-MM";
			this.dtpBirthDate.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.dtpBirthDate.Location = new System.Drawing.Point(304, 72);
			this.dtpBirthDate.Name = "dtpBirthDate";
			this.dtpBirthDate.ShowUpDown = true;
			this.dtpBirthDate.Size = new System.Drawing.Size(72, 21);
			this.dtpBirthDate.TabIndex = 4;
			// 
			// rdoMale
			// 
			this.rdoMale.Checked = true;
			this.rdoMale.Location = new System.Drawing.Point(304, 43);
			this.rdoMale.Name = "rdoMale";
			this.rdoMale.Size = new System.Drawing.Size(40, 24);
			this.rdoMale.TabIndex = 1;
			this.rdoMale.TabStop = true;
			this.rdoMale.Text = "男";
			// 
			// rdoFemale
			// 
			this.rdoFemale.Location = new System.Drawing.Point(360, 43);
			this.rdoFemale.Name = "rdoFemale";
			this.rdoFemale.Size = new System.Drawing.Size(40, 24);
			this.rdoFemale.TabIndex = 2;
			this.rdoFemale.Text = "女";
			// 
			// grpPatientInfo
			// 
			this.grpPatientInfo.BackColor = System.Drawing.Color.AliceBlue;
			this.grpPatientInfo.Controls.Add(this.panel1);
			this.grpPatientInfo.Controls.Add(this.pictureBox3);
			this.grpPatientInfo.Controls.Add(this.rdoFemale);
			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.lblMobile);
			this.grpPatientInfo.Controls.Add(this.lblAddress);
			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.dtpBirthDate);
			this.grpPatientInfo.Controls.Add(this.lblCardNumber);
			this.grpPatientInfo.Location = new System.Drawing.Point(8, 8);
			this.grpPatientInfo.Name = "grpPatientInfo";
			this.grpPatientInfo.Size = new System.Drawing.Size(435, 200);
			this.grpPatientInfo.TabIndex = 11;
			this.grpPatientInfo.TabStop = false;
			// 
			// pictureBox3
			// 
			this.pictureBox3.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox3.BackgroundImage")));
			this.pictureBox3.Location = new System.Drawing.Point(0, 25);
			this.pictureBox3.Name = "pictureBox3";
			this.pictureBox3.Size = new System.Drawing.Size(435, 8);
			this.pictureBox3.TabIndex = 12;
			this.pictureBox3.TabStop = false;
			// 
			// pictureBox2
			// 
			this.pictureBox2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox2.BackgroundImage")));
			this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Right;
			this.pictureBox2.Location = new System.Drawing.Point(315, 0);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(120, 25);
			this.pictureBox2.TabIndex = 1;
			this.pictureBox2.TabStop = false;
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Left;
			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(320, 25);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.pictureBox1);
			this.panel1.Controls.Add(this.pictureBox2);
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(435, 25);
			this.panel1.TabIndex = 13;
			// 
			// frmPatientNew
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.ClientSize = new System.Drawing.Size(454, 243);
			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 = "frmPatientNew";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "请注册病人信息";
			this.grpPatientInfo.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		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
			{
				//保存病人信息
				PatientsEx patientsEx=new PatientsEx();				
				
				PatientsEx.t_PatientRow patientRow=patientsEx.t_Patient.Newt_PatientRow();
				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();

				patientsEx.t_Patient.Rows.Add(patientRow);

				(new Patient()).AddPatientInfo(patientRow);
				MessageProcess.ShowInformation("保存病人信息成功!");
				WaitForInputNextPatient();
			}
			catch(Exception ex)
			{
				MessageProcess.ShowError(ex.Message);
			}
		}

		/// <summary>
		/// 将所有的控件设为初始状态，等待输入下一个病人的信息
		/// </summary>
		private void WaitForInputNextPatient()
		{
			txtName.Text="";
			rdoMale.Checked=true;
			txtCardNumber.Text="";
			dtpBirthDate.Value=DateTime.Today;
			txtTelephone.Text="";
			txtMobile.Text="";
			txtAddress.Text="";

			txtName.Focus();
		}

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