using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ljs;
using JXC_MIS.MyClass;
using JXC_MIS.DataTier;
using JXC_MIS.Interface;

namespace JXC_MIS.UserTier.CommonForm
{
	/// <summary>
	/// frmInput 的摘要说明。
	/// </summary>
	public class frmInput : System.Windows.Forms.Form
	{
		ClsReceiptMethod newReceipt=new ClsReceiptMethod();
		ClsCellAttrib newCellAttrib=new ClsCellAttrib();
		ClsReceiptAttrib newR_Attrib=new ClsReceiptAttrib();
		ClsDataBase newDB=new ClsDataBase() ;
		ClsDataBaseAccess newADO=new ClsDataBaseAccess();
		ClsInitData InitData=new ClsInitData();
		protected string m_RefertoReturnCols  ;		       //参照返回列值
		protected string m_RefertoInputCols  ;			       //目标列对应列
		protected string m_MouseEnterPlace  ;			       //鼠标单击时所在位置 ,主表位置"Master"	明细表位置 "Detail"
		private IReferto  m_IRefertoForm;

		private ToolTip nToolTip=new ToolTip();  //控件提示
		protected TextBox TxtTemp=new TextBox() ;
		private ljs.MyGrid grd_Input;
		private TextBox [] ArrCell=null;
		private string m_Name ="名称" ;			//               名称

		private string m_IDValue ;			    //               设置被修改的唯一标识的值
		private bool m_EditFlag=false ;			//               标志添加还是修改状态
		private string m_TitleFormat="合计大写金额||小写|$|^;部门||业务员|$|客户代码|>;北京;;1;|^;客户|$|联系人|>|^;地址|$|电话||手机|$|合计大写金额||小写|$|^;部门||业务员|$|客户代码|>;北京;;1;|^;客户|$|联系人|>|^;地址|$|电话||手机|$|合计大写金额||小写|$|^;部门||业务员|$|客户代码|>;北京;;1;|^;客户|$|联系人|>|^;地址|$|电话||手机|>";	//                   新单据标题内容标题
	
		private string m_ColWidth ="70,150,50,150" ;	    //                  列宽
//		private string m_TableName ="I_Department" ;			//                   名称
//		private string m_FieldsName ="Code,Name" ;			//                   名称
		/// <summary>
		/// 设名称
		/// </summary>
		public string RecordName
		{ 
			get {return m_Name;} 
			set { m_Name=value;this.Text =m_Name+" 编辑";} 
		} 
		/// <summary>
		/// 设置被修改的唯一标识的值
		/// </summary>
		public string IDValue
		{ 
			get {return m_IDValue;} 
			set { m_IDValue=value;} 
		} 

		
		/// <summary>
		///  标志添加还是修改状态 true修改 false插入
		/// </summary>
		public bool EditFlag
		{ 
			get {return m_EditFlag;} 
			set { m_EditFlag=value;} 
		} 

		/// <summary>
		/// 设置标题
		/// </summary>
		public string TitleFormat
		{ 
			get {return m_TitleFormat;} 
			set { m_TitleFormat=value;} 
		} 
		/// <summary>
		/// 设置列宽
		/// </summary>
		public string ColWidth
		{ 
			get {return m_ColWidth;} 
			set { m_ColWidth=value;} 
		} 

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button cmd_OK;
		private System.Windows.Forms.Button cmd_Cancel;
		private System.Windows.Forms.Label lblBorder;
		private System.Windows.Forms.Button cmd_Print;
		private System.Windows.Forms.Label lbl_Title;
		protected System.Windows.Forms.Button btnDown;
		private System.Windows.Forms.ComboBox cbo_ID;
		public frmInput()
		{
			//
			// 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(frmInput));
			this.grd_Input = new ljs.MyGrid();
			this.cmd_OK = new System.Windows.Forms.Button();
			this.cmd_Cancel = new System.Windows.Forms.Button();
			this.lblBorder = new System.Windows.Forms.Label();
			this.cmd_Print = new System.Windows.Forms.Button();
			this.lbl_Title = new System.Windows.Forms.Label();
			this.cbo_ID = new System.Windows.Forms.ComboBox();
			this.btnDown = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// grd_Input
			// 
			this.grd_Input.BackColor = System.Drawing.Color.White;
			this.grd_Input.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("grd_Input.BackgroundImage")));
			this.grd_Input.BorderStyle = false;
			this.grd_Input.BorderWidth = 0;
			this.grd_Input.CellBackColor = System.Drawing.Color.White;
			this.grd_Input.CellColLength = -1;
			this.grd_Input.CellFontSize = 9F;
			this.grd_Input.CellForeColor = System.Drawing.Color.Black;
			this.grd_Input.CellHeight = 0;
			this.grd_Input.CellRowLength = -1;
			this.grd_Input.CellTag = null;
			this.grd_Input.Col = 0;
			this.grd_Input.Cols = 6;
			this.grd_Input.EnterToTab = true;
			this.grd_Input.GridStyle = true;
			this.grd_Input.Location = new System.Drawing.Point(14, 34);
			this.grd_Input.Name = "grd_Input";
			this.grd_Input.ReadCellBackColor = System.Drawing.Color.White;
			this.grd_Input.ReadCellForeColor = System.Drawing.Color.Black;
			this.grd_Input.Row = 0;
			this.grd_Input.RowHeightMin = 20;
			this.grd_Input.Rows = 2;
			this.grd_Input.SetCellWidth = "100,100,100,100,100,100,100,100";
			this.grd_Input.Size = new System.Drawing.Size(394, 112);
			this.grd_Input.TabIndex = 45;
			this.grd_Input.Cell_Enter += new ljs.GridCell_Enter(this.grd_Input_Cell_Enter);
			// 
			// cmd_OK
			// 
			this.cmd_OK.BackColor = System.Drawing.SystemColors.Control;
			this.cmd_OK.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmd_OK.Location = new System.Drawing.Point(242, 160);
			this.cmd_OK.Name = "cmd_OK";
			this.cmd_OK.Size = new System.Drawing.Size(72, 26);
			this.cmd_OK.TabIndex = 46;
			this.cmd_OK.Text = "确定";
			this.cmd_OK.Click += new System.EventHandler(this.cmd_OK_Click);
			// 
			// cmd_Cancel
			// 
			this.cmd_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmd_Cancel.Location = new System.Drawing.Point(342, 160);
			this.cmd_Cancel.Name = "cmd_Cancel";
			this.cmd_Cancel.Size = new System.Drawing.Size(68, 26);
			this.cmd_Cancel.TabIndex = 47;
			this.cmd_Cancel.Text = "取消";
			this.cmd_Cancel.Click += new System.EventHandler(this.cmd_Cancel_Click);
			// 
			// lblBorder
			// 
			this.lblBorder.BackColor = System.Drawing.Color.Transparent;
			this.lblBorder.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lblBorder.Location = new System.Drawing.Point(320, 64);
			this.lblBorder.Name = "lblBorder";
			this.lblBorder.Size = new System.Drawing.Size(196, 26);
			this.lblBorder.TabIndex = 48;
			// 
			// cmd_Print
			// 
			this.cmd_Print.BackColor = System.Drawing.SystemColors.Control;
			this.cmd_Print.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmd_Print.Location = new System.Drawing.Point(14, 160);
			this.cmd_Print.Name = "cmd_Print";
			this.cmd_Print.Size = new System.Drawing.Size(72, 26);
			this.cmd_Print.TabIndex = 49;
			this.cmd_Print.Text = "打印";
			// 
			// lbl_Title
			// 
			this.lbl_Title.BackColor = System.Drawing.Color.Transparent;
			this.lbl_Title.Font = new System.Drawing.Font("宋体", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lbl_Title.Location = new System.Drawing.Point(20, 4);
			this.lbl_Title.Name = "lbl_Title";
			this.lbl_Title.Size = new System.Drawing.Size(382, 24);
			this.lbl_Title.TabIndex = 50;
			this.lbl_Title.Text = "信息设置";
			this.lbl_Title.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// cbo_ID
			// 
			this.cbo_ID.Location = new System.Drawing.Point(106, 158);
			this.cbo_ID.Name = "cbo_ID";
			this.cbo_ID.Size = new System.Drawing.Size(106, 20);
			this.cbo_ID.TabIndex = 51;
			this.cbo_ID.Visible = false;
			// 
			// btnDown
			// 
			this.btnDown.BackColor = System.Drawing.Color.Transparent;
			this.btnDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnDown.BackgroundImage")));
			this.btnDown.Font = new System.Drawing.Font("Marlett", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(2)));
			this.btnDown.Location = new System.Drawing.Point(2, 190);
			this.btnDown.Name = "btnDown";
			this.btnDown.Size = new System.Drawing.Size(16, 18);
			this.btnDown.TabIndex = 62;
			this.btnDown.Visible = false;
			this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
			// 
			// frmInput
			// 
			this.AcceptButton = this.cmd_OK;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.CancelButton = this.cmd_Cancel;
			this.ClientSize = new System.Drawing.Size(420, 203);
			this.Controls.Add(this.btnDown);
			this.Controls.Add(this.cbo_ID);
			this.Controls.Add(this.lbl_Title);
			this.Controls.Add(this.cmd_Print);
			this.Controls.Add(this.cmd_Cancel);
			this.Controls.Add(this.cmd_OK);
			this.Controls.Add(this.grd_Input);
			this.Controls.Add(this.lblBorder);
			this.MaximizeBox = false;
			this.Name = "frmInput";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "frmInput";
			this.Load += new System.EventHandler(this.frmInput_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmInput_Load(object sender, System.EventArgs e)
		{
			newR_Attrib.LoadData(m_Name) ;
			cbo_ID.Text=m_IDValue;
			newReceipt.SetInterface(grd_Input,newR_Attrib.PageTopFormat,newR_Attrib.PageTopColWidth,true,true);
			grd_Input.SetColBackColor(Color.LightGray  ,true);
			if (m_EditFlag==true) 
			{
				RecordLoad();
				lbl_Title.Text =m_Name+"信息设置[修改]";
			}
			else
			{
				lbl_Title.Text =m_Name+"信息设置[插入]";
			}
			this.Width=grd_Input.Width+this.grd_Input.Left*2+10;
			this.Height =grd_Input.Top +grd_Input.Height +cmd_OK.Height *4;

			this.lblBorder.Top =this.grd_Input.Top -1;
			this.lblBorder.Left =this.grd_Input.Left -1;
			this.lblBorder.Height =this.grd_Input.Height +2;
			this.lblBorder.Width =this.grd_Input.Width +2;
			this.lblBorder.SendToBack();
			lbl_Title.Width=this.lblBorder.Width;
			lbl_Title.Left=this.lblBorder.Left;
			this.cmd_OK.Top =this.lblBorder.Height +this.cmd_OK.Height+this.lblBorder.Top ;
			this.cmd_Cancel.Top =this.cmd_OK.Top ;
			this.cmd_Print.Top =this.cmd_OK.Top ;
			this.cmd_Cancel.Left  =this.Width-cmd_Cancel.Width-this.grd_Input.Left*2 ;
			this.cmd_OK.Left  =this.cmd_Cancel.Left-cmd_Cancel.Width-10 ;	

		}




		/// <summary>
		/// 记录修改
		/// </summary>
		public void RecordModify() 
		{
//			ToolEnabled(true,false);
			m_EditFlag=true;

		}
	
		/// <summary>
		///  记录取消放弃
		/// </summary>
		public void RecordCancel() 
		{
//			ToolEnabled(false,false);
		}
		/// <summary>
		///  记录刷新
		/// </summary>
		public void RecordRefresh() 
		{
//			ToolEnabled(false,false);
//			InitReceiptData(cbo_ID.Text);
		}
		/// <summary>
		///  记录查询
		/// </summary>
		public void RecordFind() 
		{
			frmReceiptFind f=new frmReceiptFind();
			f.ShowDialog() ;
		}
		/// <summary>
		///  记录加载
		/// </summary>
		public void RecordLoad() 
		{
			string strSQL="";
			string strIdName=newR_Attrib.KeyFieldName;
			string strTableName=newR_Attrib.MasterTableName;
			strSQL=newR_Attrib.MasterFeildName+" From "+strTableName;
			strSQL="Select "+strSQL+" Where "+strIdName+"='"+m_IDValue+"'";
		    InitData.RecordToGrid(grd_Input,strSQL);
		}
		/// <summary>
		/// 将界面中指定网格单元格对象添加到控件数组中。
		/// </summary>
		private void AddToArray(ljs.MyGrid p_Grid)
		{
			int rows;
			int cols;
			int p_Index=0;
				rows=p_Grid.Rows ;
				cols=p_Grid.Cols;
				ArrCell=new TextBox[rows*cols];
				for (int r=0;r<rows;r++)
				{
					for (int c=0;c<cols;c++)
					{
						//将单元格对象添加到控件数组中
						ArrCell[p_Index]=p_Grid.GridCell(r,c);
						p_Index++;
					}
				}
		}

		/// <summary>
		/// 记录保存
		/// </summary>
		public void RecordSave() 
		{
			AddToArray(grd_Input);
			string [,] TempArrCell=null;
			string sReceiptNo;
			newADO.BeginTrans(); 

			if (m_EditFlag==true)
			{
				sReceiptNo=cbo_ID.Text;
				newADO.ExeSql("Delete From "+newR_Attrib.MasterTableName+" Where "+newR_Attrib.KeyFieldName+"='"+cbo_ID.Text +"'"); 
			}
			else
			{
				cbo_ID.Text =newReceipt.GetRecordNo(newR_Attrib.MasterTableName,newR_Attrib.KeyFieldName,newR_Attrib.FirstChar);
				sReceiptNo=cbo_ID.Text;
				grd_Input.GridCell(0,1).Text=cbo_ID.Text ;
			}

			int j=ArrCell.GetUpperBound(0)+1;
			//数据保存
			TempArrCell=new string[j/2,2];
			for (int i=0;i<j;i++)
			{
				if (i%2!=0)
				{
					TempArrCell[i/2,0]=ArrCell[i].Text ;
					TempArrCell[i/2,1]=ArrCell[i].Tag.ToString();
				}
			}
			string sSQL=newReceipt.GetInsertSQL(newR_Attrib.MasterTableName,newR_Attrib.MasterFeildName,TempArrCell);
			if (newADO.ExeSql(sSQL)<0)
			{
				newADO.Rollback() ;
				return ;
			}

			
			newADO.Commit(); 
			TempArrCell=null;
//			ToolEnabled(false,false);
//			//单据编号列表初始
//			InitReceiptList();
		}
		/// <summary>
		/// 设置参照按钮
		/// </summary>
		private void CellMouseDown(TextBox sender)
		{
			try
			{
//				m_ColExpress=null;
				newCellAttrib.FormatString =sender.Tag.ToString();
//				m_ColExpress=newCellAttrib.ColExpress ;
//				m_ShowFormat=newCellAttrib.ShowFormat ;
				if (newCellAttrib.RefertoType=="" || newCellAttrib.RefertoType==null)
				{
					btnDown.Visible =false;
				}
				else
				{
					btnDown.Top  =sender.Top+sender.Parent.Top+1 ;
					btnDown.Left  =sender.Left+sender.Parent.Left +sender.Width -btnDown.Width  ;
					btnDown.Height   =sender.Height-1 ;
					btnDown.Visible =true;
					btnDown.BringToFront() ;
				}
			}
			catch
			{
				return;
			}
		}


		/// <summary>
		/// 显示参照
		/// </summary>
		protected virtual void ShowReferto(int p_RefertoType)
		{  
			IReferto f= new frmReferto();

			f.DataSource =newCellAttrib.RefertoDataSource ;

			f.Location = new Point(this.btnDown.Left + this.Left-TxtTemp.Width ,this.btnDown.Height*2 +this.btnDown.Top + this.Top);
			f.DefaultValue=TxtTemp.Text ;
			f.RefertoType =p_RefertoType ;
			m_RefertoReturnCols=newCellAttrib.RefertoReturnCols   ;		       //参照返回列值
			m_RefertoInputCols=newCellAttrib.RefertoInputCols   ;			       //目标列对应列

			f.ShowDialog();
			System.Collections.Hashtable ReturnValues= f.GetReturnHashtable();
			if (ReturnValues==null  || ReturnValues.Count==0) return;
			TxtTemp.Text =ReturnValues[0].ToString();

			if (ReturnValues!=null && m_RefertoReturnCols!="")
			{
				string[] ArrTemp=null;
				string[] ArrTemp1=null;
				ArrTemp=m_RefertoReturnCols.Split(':');
				ArrTemp1=m_RefertoInputCols.Split(':');
				int a=ArrTemp.Length ;
				int b=ArrTemp1.Length ;
				if (a==b && a!=0)
				{
					for (int r=0;r<a;r++)
					{
							ArrCell[int.Parse(ArrTemp1[r])].Text=ReturnValues[int.Parse(ArrTemp[r])].ToString ();
					}
				}
			}
			this.TxtTemp.Focus();
			f=null;
		}
		private void LoadReferto()
		{
			try
			{
				int T=0;
				newCellAttrib.FormatString =TxtTemp.Tag.ToString();
				T=int.Parse(newCellAttrib.RefertoType);
					ShowReferto(T)	;
			}
			catch(System.Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
				return;
			}
		
		}
		protected virtual void btnDown_Click(object sender, System.EventArgs e)
		{
			LoadReferto();
		}

		private void cmd_OK_Click(object sender, System.EventArgs e)
		{
			RecordSave() ;
		}

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

		private void grd_Input_Cell_Enter(object sender, System.EventArgs e)
		{
			try
			{
				TextBox control = (TextBox) sender ; 
				TxtTemp =control;
				CellMouseDown(control);
				nToolTip.SetToolTip(control,"索引位置："+control.TabIndex.ToString());
			}
			catch
			{
				return;
			}
		}












	}
}
