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

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

namespace HISClinic
{
	/// <summary>
	/// frmBillOfPrescriptionNew 的摘要说明。
	/// </summary>
	public class frmBillOfPrescriptionNew : System.Windows.Forms.Form
	{
		private DataView _dvPrescriptionMaster;
		private DataView _dvPrescriptionDetail;
 
		private System.Windows.Forms.GroupBox groupBox1;
		private HISClinic.Controls.PatientInfo patientInfo;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label lblSumTotalValue;
		private System.Windows.Forms.Label lblSumTotal;
		private System.Windows.Forms.DataGrid grdPrescriptionDetail;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.DataGrid grdPrescriptionMaster;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.PictureBox pictureBox3;
		private System.Windows.Forms.PictureBox pictureBox4;
		private System.Windows.Forms.PictureBox pictureBox5;
		private System.Windows.Forms.PictureBox pictureBox6;
		private System.Windows.Forms.PictureBox pictureBox7;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			SetgrdPrescriptionMaster();
			SetgrdPrescriptionDetailStyle();
		}

		/// <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(frmBillOfPrescriptionNew));
			this.groupBox1 = 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.patientInfo = new HISClinic.Controls.PatientInfo();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.pictureBox6 = new System.Windows.Forms.PictureBox();
			this.pictureBox5 = new System.Windows.Forms.PictureBox();
			this.pictureBox4 = new System.Windows.Forms.PictureBox();
			this.grdPrescriptionMaster = new System.Windows.Forms.DataGrid();
			this.lblSumTotalValue = new System.Windows.Forms.Label();
			this.lblSumTotal = new System.Windows.Forms.Label();
			this.grdPrescriptionDetail = new System.Windows.Forms.DataGrid();
			this.pictureBox7 = new System.Windows.Forms.PictureBox();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.grdPrescriptionMaster)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.grdPrescriptionDetail)).BeginInit();
			this.panel1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.panel1);
			this.groupBox1.Controls.Add(this.pictureBox3);
			this.groupBox1.Controls.Add(this.patientInfo);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(656, 88);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "病人信息";
			// 
			// 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(656, 6);
			this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox3.TabIndex = 8;
			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(536, 0);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(120, 25);
			this.pictureBox2.TabIndex = 7;
			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(536, 25);
			this.pictureBox1.TabIndex = 3;
			this.pictureBox1.TabStop = false;
			// 
			// patientInfo
			// 
			this.patientInfo.BackColor = System.Drawing.Color.AliceBlue;
			this.patientInfo.Location = new System.Drawing.Point(0, 32);
			this.patientInfo.Name = "patientInfo";
			this.patientInfo.Size = new System.Drawing.Size(656, 56);
			this.patientInfo.TabIndex = 1;
			this.patientInfo.GetPatientInfoCompleted += new HISClinic.Controls.OnGetPatientInfoCompleted(this.patientInfo_GetPatientInfoCompleted);
			// 
			// groupBox2
			// 
			this.groupBox2.BackColor = System.Drawing.Color.AliceBlue;
			this.groupBox2.Controls.Add(this.panel2);
			this.groupBox2.Controls.Add(this.pictureBox6);
			this.groupBox2.Controls.Add(this.grdPrescriptionMaster);
			this.groupBox2.Controls.Add(this.lblSumTotalValue);
			this.groupBox2.Controls.Add(this.lblSumTotal);
			this.groupBox2.Controls.Add(this.grdPrescriptionDetail);
			this.groupBox2.Controls.Add(this.pictureBox7);
			this.groupBox2.Location = new System.Drawing.Point(8, 104);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(656, 290);
			this.groupBox2.TabIndex = 2;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "处方内容";
			// 
			// pictureBox6
			// 
			this.pictureBox6.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox6.BackgroundImage")));
			this.pictureBox6.Location = new System.Drawing.Point(0, 25);
			this.pictureBox6.Name = "pictureBox6";
			this.pictureBox6.Size = new System.Drawing.Size(656, 6);
			this.pictureBox6.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox6.TabIndex = 9;
			this.pictureBox6.TabStop = false;
			// 
			// pictureBox5
			// 
			this.pictureBox5.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox5.BackgroundImage")));
			this.pictureBox5.Dock = System.Windows.Forms.DockStyle.Right;
			this.pictureBox5.Location = new System.Drawing.Point(536, 0);
			this.pictureBox5.Name = "pictureBox5";
			this.pictureBox5.Size = new System.Drawing.Size(120, 25);
			this.pictureBox5.TabIndex = 8;
			this.pictureBox5.TabStop = false;
			// 
			// pictureBox4
			// 
			this.pictureBox4.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.pictureBox4.Dock = System.Windows.Forms.DockStyle.Left;
			this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
			this.pictureBox4.Location = new System.Drawing.Point(0, 0);
			this.pictureBox4.Name = "pictureBox4";
			this.pictureBox4.Size = new System.Drawing.Size(536, 25);
			this.pictureBox4.TabIndex = 6;
			this.pictureBox4.TabStop = false;
			// 
			// grdPrescriptionMaster
			// 
			this.grdPrescriptionMaster.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.grdPrescriptionMaster.CaptionVisible = false;
			this.grdPrescriptionMaster.DataMember = "";
			this.grdPrescriptionMaster.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.grdPrescriptionMaster.Location = new System.Drawing.Point(2, 40);
			this.grdPrescriptionMaster.Name = "grdPrescriptionMaster";
			this.grdPrescriptionMaster.ReadOnly = true;
			this.grdPrescriptionMaster.Size = new System.Drawing.Size(170, 227);
			this.grdPrescriptionMaster.TabIndex = 0;
			this.grdPrescriptionMaster.MouseDown += new System.Windows.Forms.MouseEventHandler(this.grdPrescriptionMaster_MouseDown);
			this.grdPrescriptionMaster.Click += new System.EventHandler(this.grdPrescriptionMaster_Click);
			this.grdPrescriptionMaster.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.grdPrescriptionMaster_KeyPress);
			this.grdPrescriptionMaster.CurrentCellChanged += new System.EventHandler(this.grdPrescriptionMaster_CurrentCellChanged);
			// 
			// lblSumTotalValue
			// 
			this.lblSumTotalValue.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(186)), ((System.Byte)(209)), ((System.Byte)(247)));
			this.lblSumTotalValue.Location = new System.Drawing.Point(512, 272);
			this.lblSumTotalValue.Name = "lblSumTotalValue";
			this.lblSumTotalValue.Size = new System.Drawing.Size(112, 16);
			this.lblSumTotalValue.TabIndex = 5;
			this.lblSumTotalValue.Text = "0.00";
			this.lblSumTotalValue.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lblSumTotal
			// 
			this.lblSumTotal.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(186)), ((System.Byte)(209)), ((System.Byte)(247)));
			this.lblSumTotal.Location = new System.Drawing.Point(464, 272);
			this.lblSumTotal.Name = "lblSumTotal";
			this.lblSumTotal.Size = new System.Drawing.Size(48, 16);
			this.lblSumTotal.TabIndex = 4;
			this.lblSumTotal.Text = "总计：";
			// 
			// grdPrescriptionDetail
			// 
			this.grdPrescriptionDetail.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.grdPrescriptionDetail.CaptionVisible = false;
			this.grdPrescriptionDetail.DataMember = "";
			this.grdPrescriptionDetail.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.grdPrescriptionDetail.Location = new System.Drawing.Point(179, 40);
			this.grdPrescriptionDetail.Name = "grdPrescriptionDetail";
			this.grdPrescriptionDetail.ReadOnly = true;
			this.grdPrescriptionDetail.Size = new System.Drawing.Size(474, 227);
			this.grdPrescriptionDetail.TabIndex = 1;
			// 
			// pictureBox7
			// 
			this.pictureBox7.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox7.BackgroundImage")));
			this.pictureBox7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox7.Location = new System.Drawing.Point(0, 267);
			this.pictureBox7.Name = "pictureBox7";
			this.pictureBox7.Size = new System.Drawing.Size(656, 23);
			this.pictureBox7.TabIndex = 10;
			this.pictureBox7.TabStop = false;
			// 
			// 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(528, 400);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(60, 23);
			this.btnSave.TabIndex = 5;
			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(600, 400);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(60, 23);
			this.btnCancel.TabIndex = 6;
			this.btnCancel.Text = "取 消";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// 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(656, 25);
			this.panel1.TabIndex = 9;
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.pictureBox4);
			this.panel2.Controls.Add(this.pictureBox5);
			this.panel2.Location = new System.Drawing.Point(0, 0);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(656, 25);
			this.panel2.TabIndex = 11;
			// 
			// frmBillOfPrescriptionNew
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.ClientSize = new System.Drawing.Size(674, 428);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.ForeColor = System.Drawing.SystemColors.Desktop;
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmBillOfPrescriptionNew";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "收费信息";
			this.Load += new System.EventHandler(this.frmBillOfPrescriptionNew_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.grdPrescriptionMaster)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.grdPrescriptionDetail)).EndInit();
			this.panel1.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

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

		/// <summary>
		/// 设置PrescriptionMaste DataGried的显示风格
		/// </summary>
		private void SetgrdPrescriptionMaster()
		{
			DataGridTableStyle tableStyle=new DataGridTableStyle();

			//增加一列用于显示选择的处方
			DataGridBoolColumnEx columnStyleSelectede=new DataGridBoolColumnEx();
			columnStyleSelectede.HeaderText="";
			columnStyleSelectede.MappingName="Selected";
			columnStyleSelectede.Width=25;					
			tableStyle.GridColumnStyles.Add(columnStyleSelectede);	

			//增加一列用于显示开处方的时间
			DataGridTextBoxColumnEx columnStyleMakeDate=new DataGridTextBoxColumnEx();
			
			columnStyleMakeDate.HeaderText="日期";
			columnStyleMakeDate.MappingName=PrescriptionsEx.t_PrescriptionMasterDataTableEx.FIELD_MakeDate;
			columnStyleMakeDate.Width=70;				
			tableStyle.GridColumnStyles.Add(columnStyleMakeDate);

			//增加一列用于显示单个处方的总计
			DataGridDecimalColumn columnStyleSumTotal=new DataGridDecimalColumn();
			
			columnStyleSumTotal.HeaderText="总计";
			columnStyleSumTotal.Alignment=HorizontalAlignment.Right;
			columnStyleSumTotal.MappingName=PrescriptionsEx.t_PrescriptionMasterDataTableEx.FIELD_SumTotal;
			columnStyleSumTotal.Width=grdPrescriptionMaster.Width - columnStyleMakeDate.Width - columnStyleSelectede.Width-columnStyleSumTotal.Width- 24;				
			tableStyle.GridColumnStyles.Add(columnStyleSumTotal);

			tableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			tableStyle.MappingName = PrescriptionsEx .t_PrescriptionMasterDataTableEx.TableName_PrescriptionMaster;
			tableStyle.RowHeadersVisible = false;						

			tableStyle.AlternatingBackColor = System.Drawing.Color.LightGray;
			tableStyle.AllowSorting=false;		
			
			grdPrescriptionMaster.TableStyles.Add(tableStyle);	
			grdPrescriptionMaster.ReadOnly=true;
		}

		/// <summary>
		/// 设置grdPrescriptionDetail的显示风格
		/// </summary>
		private void SetgrdPrescriptionDetailStyle()
		{
			DataGridTableStyle tableStyle=new DataGridTableStyle();

			//增加一列用于显示LineNumber
			DataGridColumnStyle columnStyleLineNumber=new DataGridTextBoxColumnEx();
			columnStyleLineNumber.HeaderText="序";
			columnStyleLineNumber.Alignment=HorizontalAlignment.Center;
			columnStyleLineNumber.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_LineNumber;
			columnStyleLineNumber.Width=30;						
			tableStyle.GridColumnStyles.Add(columnStyleLineNumber);	

			//增加一列用于显示MedicineName
			DataGridTextBoxColumnEx columnStyleMedicineName=new DataGridTextBoxColumnEx();			
			columnStyleMedicineName.HeaderText="药品名";
			columnStyleMedicineName.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_MedicineName;			
			columnStyleMedicineName.Width=125;				
			tableStyle.GridColumnStyles.Add(columnStyleMedicineName);

			//增加一列用于显示MedicineSpec
			DataGridTextBoxColumnEx columnStyleMedicineSpec=new DataGridTextBoxColumnEx();			
			columnStyleMedicineSpec.HeaderText="规格";
			columnStyleMedicineSpec.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_MedicineSpec;			
			columnStyleMedicineSpec.Width=100;
			tableStyle.GridColumnStyles.Add(columnStyleMedicineSpec);

			//增加一列用于显示Quantity
			DataGridTextBoxColumnEx columnStyleQuantity=new DataGridTextBoxColumnEx();			
			columnStyleQuantity.HeaderText="数量";
			columnStyleQuantity.Alignment=HorizontalAlignment.Right;
			columnStyleQuantity.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_Quantity;			
			columnStyleQuantity.Width=40;
			tableStyle.GridColumnStyles.Add(columnStyleQuantity);

			//增加一列用于显示UnitName
			DataGridTextBoxColumnEx columnStyleUnitName=new DataGridTextBoxColumnEx();			
			columnStyleUnitName.HeaderText="单位";
			columnStyleUnitName.Alignment=HorizontalAlignment.Center;
			columnStyleUnitName.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_UnitName;			
			columnStyleUnitName.Width=30;
			tableStyle.GridColumnStyles.Add(columnStyleUnitName); 

			//增加一列用于显示UnitPrice
			DataGridDecimalColumn columnStyleUnitPrice=new DataGridDecimalColumn();
			
			columnStyleUnitPrice.HeaderText="单价";
			columnStyleUnitPrice.Alignment=HorizontalAlignment.Right;
			columnStyleUnitPrice.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_UnitPrice;			
			columnStyleUnitPrice.Width=55;
			tableStyle.GridColumnStyles.Add(columnStyleUnitPrice);

			//增加一列用于显示SubTotal
			DataGridDecimalColumn columnStyleSubTotal=new DataGridDecimalColumn();
			
			columnStyleSubTotal.HeaderText="小计";
			columnStyleSubTotal.Alignment=HorizontalAlignment.Right;
			columnStyleSubTotal.MappingName=PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_SubTotal;			
			columnStyleSubTotal.Width=60;
			tableStyle.GridColumnStyles.Add(columnStyleSubTotal);

			tableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			tableStyle.MappingName = PrescriptionsEx.t_PrescriptionDetailDataTableEx.TableName_PrescriptionDetail;
			tableStyle.RowHeadersVisible = false;			
			tableStyle.AllowSorting=false;	
        
			tableStyle.AlternatingBackColor = System.Drawing.Color.LightGray;			
		
			grdPrescriptionDetail.TableStyles.Add(tableStyle);	
			grdPrescriptionDetail.ReadOnly=true;			
		}

		private void patientInfo_GetPatientInfoCompleted()
		{
			PrescriptionsEx prescriptionsEx=(new Prescription()).FindUnpaidPrescriptions(patientInfo.PatientRow.PatientID);

			if(prescriptionsEx !=null)
			{
				Prescription.CalculateSumTotal(prescriptionsEx);

				DataColumn col=new DataColumn("Selected",Type.GetType("System.Boolean"));
				col.DefaultValue=false;
				prescriptionsEx.t_PrescriptionMaster.Columns.Add(col);
				
				_dvPrescriptionMaster=new  DataView(prescriptionsEx.t_PrescriptionMaster,"", PrescriptionsEx.t_PrescriptionMasterDataTableEx.FIELD_MakeDate + " DESC",DataViewRowState.CurrentRows);
				_dvPrescriptionDetail=new DataView(prescriptionsEx.t_PrescriptionDetail,"","",DataViewRowState.CurrentRows);

				FilterPrescriptionDetail(0);
				grdPrescriptionMaster.SetDataBinding(_dvPrescriptionMaster,"");
				grdPrescriptionDetail.SetDataBinding(_dvPrescriptionDetail,"");
				DisplaySelectedPrescriptionMasterSumTotol();
			}
		}

		private void FilterPrescriptionDetail(int RowIndex)
		{
			if(_dvPrescriptionMaster != null )
			{
				if(_dvPrescriptionMaster.Count>0)
				{
					_dvPrescriptionDetail.RowFilter = PrescriptionsEx.t_PrescriptionDetailDataTableEx.FIELD_RxID +" = " +_dvPrescriptionMaster[RowIndex][PrescriptionsEx.t_PrescriptionMasterDataTableEx.FIELD_RxID].ToString();
				}
			}
		}

		private void grdPrescriptionMaster_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{			
			if(_dvPrescriptionMaster !=null )
			{
				if(_dvPrescriptionMaster.Count>0)
				{
					Rectangle rect=grdPrescriptionMaster.GetCellBounds(0,0);			
					int DistanceFrom0Row=e.Y-rect.Y;
					int RowIndex=DistanceFrom0Row /(rect.Height+1);
		
					if( RowIndex < _dvPrescriptionMaster.Count && RowIndex == grdPrescriptionMaster.CurrentCell.RowNumber)
					{
						SelectPrescriptionMaster(RowIndex);
					}
				}
			}
		}

		private void SelectPrescriptionMaster(int RowIndex)
		{
			_dvPrescriptionMaster[RowIndex].BeginEdit();
			_dvPrescriptionMaster[RowIndex]["Selected"]=!Convert.ToBoolean(_dvPrescriptionMaster[RowIndex]["Selected"]);
			_dvPrescriptionMaster[RowIndex].EndEdit();

			DisplaySelectedPrescriptionMasterSumTotol();
		}

		private void grdPrescriptionMaster_CurrentCellChanged(object sender, System.EventArgs e)
		{
			FilterPrescriptionDetail(grdPrescriptionMaster.CurrentCell.RowNumber);
		}

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

		private void grdPrescriptionMaster_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(e.KeyChar == (char)Keys.Space || e.KeyChar==(char)Keys.Enter)
			{
				if(_dvPrescriptionMaster != null)
				{
					if(_dvPrescriptionMaster.Count>0)
					{
						SelectPrescriptionMaster(grdPrescriptionMaster.CurrentCell.RowNumber);
					}
				}
				e.Handled=true;
				return;
			}
		}

		private void DisplaySelectedPrescriptionMasterSumTotol()
		{
			decimal SumTotal=0;
			PrescriptionsEx.t_PrescriptionMasterRow[] masterRow=(PrescriptionsEx.t_PrescriptionMasterRow[])_dvPrescriptionMaster.Table.Select("Selected=1");
			foreach(PrescriptionsEx.t_PrescriptionMasterRow row in masterRow )
			{
				SumTotal+=row.SumTotal;
			}

			lblSumTotalValue.Text=SumTotal.ToString("0.00");

			btnSave.Enabled=SumTotal>0;
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			try
			{
				UsersEx usersEx=(UsersEx)CacheDataManager.GetCacheData(UsersEx.t_UserDataTableEx.TableName_User);
				UsersEx.t_UserRow userRow=(UsersEx.t_UserRow)usersEx.t_User.Rows[0];				

				PrescriptionsEx.t_PrescriptionMasterRow[] prescriptionMasterRows=(PrescriptionsEx.t_PrescriptionMasterRow[])_dvPrescriptionMaster.Table.Select("Selected=1");
				BillOfPrescriptionsEx billOfPrescriptionsEx=new BillOfPrescriptionsEx();
				BillOfPrescriptionsEx.t_BillOfPrescritpionRow billOfPrescriptionRow=billOfPrescriptionsEx.t_BillOfPrescritpion.Newt_BillOfPrescritpionRow();
				billOfPrescriptionRow.BeginEdit();
				billOfPrescriptionRow[BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_CashierID]=userRow.UserID;
				billOfPrescriptionRow[BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_CashierName]=userRow.Name;				
				billOfPrescriptionRow[BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_PaymentAmount]=Convert.ToDecimal(lblSumTotalValue.Text);
				billOfPrescriptionRow.EndEdit();
				billOfPrescriptionsEx.t_BillOfPrescritpion.Addt_BillOfPrescritpionRow(billOfPrescriptionRow);

				(new BillOfPrescription()).AddNewBillOfPrescription(prescriptionMasterRows,billOfPrescriptionRow);

				patientInfo.WaitForFindNextPatient();
				_dvPrescriptionMaster =null;
				_dvPrescriptionDetail=null;
				grdPrescriptionMaster.SetDataBinding(null,"");
				grdPrescriptionDetail.SetDataBinding(null,""); 
				lblSumTotalValue.Text="0.00";
				MessageProcess.ShowInformation("保存收费信息成功!");
				btnSave.Enabled=false;
			}
			catch(Exception ex)
			{
				MessageProcess.ShowError("保存收费记录出错！\n原因："+ex.Message);
			}
		}
	}
}
