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;
namespace JXC_MIS.UserTier.CommonForm
{
	/// <summary>
	/// frmReceiptFind 的摘要说明。
	/// </summary>
	public class frmReceiptFind : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.TextBox txt_SQL;
		private System.Windows.Forms.ComboBox cbo_Where;
		private System.Windows.Forms.ComboBox cbo_Value;
		private System.Windows.Forms.ComboBox cbo_AndOr;
		private System.Windows.Forms.ListView lv_List;
		private System.Windows.Forms.Button cmd_Add;
		private System.Windows.Forms.Button cmd_Query;
		private System.Windows.Forms.TextBox txt_SQL1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Form setFormSource;
		private System.Windows.Forms.ComboBox cbo_FieldName;		    //                   表表名
		private string m_TableName="" ;
		private string m_FeildName=""  ;								//                   表字段名称
		private string m_FeildCaption;
		private string m_Feilds;
		private string m_ReceiptNo="";

//		private string m_FeildType;
		string strSQL ="";
		bool bAndOr=false;
//		string sFeildNameCh="";
//		string sFeildNameEN="";
//		string sFeildType="";
//
//		string sWhereCh="";
//		string sWhereEN="";
//		string sAndOrCh="";
//		string sAndOrEN="";

		ClsReceiptMethod newReceipt=new ClsReceiptMethod();
		ClsReceiptAttrib newR_Attrib=new ClsReceiptAttrib();
		ClsDataBase newDB=new ClsDataBase() ;
		ClsDataBaseAccess newADO=new ClsDataBaseAccess();
		private System.Windows.Forms.Button cmd_Exit;
		private System.Windows.Forms.Button cmd_Clear;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		ClsInitData InitData=new ClsInitData();
		/// <summary>
		/// 设置表名
		/// </summary>
		public string SetTableName
		{ 
			get {return m_TableName;} 
			set { m_TableName=value;} 
		} 

		/// <summary>
		/// 设置表字段名称
		/// </summary>
		public string SetFeildName
		{ 
			get {return m_FeildName;} 
			set { m_FeildName=value;} 
		} 
		/// <summary>
		/// 设置表字段标题
		/// </summary>
		public string SetFeilds
		{ 
			get {return m_Feilds;} 
			set { m_Feilds=value;} 
		} 

		public Form SetFormSource 
		{
			set { setFormSource=value ;} 
			get {return setFormSource ;} 
		}
		public string  GetReceiptNo
		{
			get {return m_ReceiptNo ;} 
		}
		
		public frmReceiptFind()
		{
			//
			// 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()
		{
			this.lv_List = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.txt_SQL = new System.Windows.Forms.TextBox();
			this.cmd_Add = new System.Windows.Forms.Button();
			this.cbo_FieldName = new System.Windows.Forms.ComboBox();
			this.cbo_Where = new System.Windows.Forms.ComboBox();
			this.cbo_Value = new System.Windows.Forms.ComboBox();
			this.cbo_AndOr = new System.Windows.Forms.ComboBox();
			this.cmd_Query = new System.Windows.Forms.Button();
			this.txt_SQL1 = new System.Windows.Forms.TextBox();
			this.cmd_Exit = new System.Windows.Forms.Button();
			this.cmd_Clear = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// lv_List
			// 
			this.lv_List.AllowColumnReorder = true;
			this.lv_List.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lv_List.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					  this.columnHeader1,
																					  this.columnHeader2,
																					  this.columnHeader3});
			this.lv_List.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.lv_List.FullRowSelect = true;
			this.lv_List.GridLines = true;
			this.lv_List.HoverSelection = true;
			this.lv_List.LabelEdit = true;
			this.lv_List.Location = new System.Drawing.Point(0, 255);
			this.lv_List.Name = "lv_List";
			this.lv_List.Size = new System.Drawing.Size(566, 164);
			this.lv_List.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.lv_List.TabIndex = 4;
			this.lv_List.View = System.Windows.Forms.View.Details;
			this.lv_List.DoubleClick += new System.EventHandler(this.lv_List_DoubleClick);
			this.lv_List.SelectedIndexChanged += new System.EventHandler(this.lv_List_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "单据编号";
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "日期";
			this.columnHeader2.Width = 92;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "制单人";
			// 
			// txt_SQL
			// 
			this.txt_SQL.Location = new System.Drawing.Point(4, 14);
			this.txt_SQL.Multiline = true;
			this.txt_SQL.Name = "txt_SQL";
			this.txt_SQL.Size = new System.Drawing.Size(558, 82);
			this.txt_SQL.TabIndex = 3;
			this.txt_SQL.Text = "";
			// 
			// cmd_Add
			// 
			this.cmd_Add.Location = new System.Drawing.Point(290, 16);
			this.cmd_Add.Name = "cmd_Add";
			this.cmd_Add.Size = new System.Drawing.Size(76, 22);
			this.cmd_Add.TabIndex = 8;
			this.cmd_Add.Text = "加入(&A)";
			this.cmd_Add.Click += new System.EventHandler(this.cmd_Add_Click);
			// 
			// cbo_FieldName
			// 
			this.cbo_FieldName.ForeColor = System.Drawing.Color.RoyalBlue;
			this.cbo_FieldName.Location = new System.Drawing.Point(6, 42);
			this.cbo_FieldName.Name = "cbo_FieldName";
			this.cbo_FieldName.Size = new System.Drawing.Size(182, 20);
			this.cbo_FieldName.TabIndex = 9;
			this.cbo_FieldName.Text = "<字段名称>";
			this.cbo_FieldName.SelectedValueChanged += new System.EventHandler(this.cbo_FieldName_SelectedValueChanged);
			this.cbo_FieldName.Click += new System.EventHandler(this.cbo_FieldName_Click);
			this.cbo_FieldName.SelectedIndexChanged += new System.EventHandler(this.cbo_FieldName_SelectedIndexChanged);
			// 
			// cbo_Where
			// 
			this.cbo_Where.Items.AddRange(new object[] {
														   "等于 |=",
														   "大于 |>",
														   "小于 |<",
														   "不等 |<>",
														   "包含 |Like",
														   "在..和..之间 |Between",
														   "小于等于 |<=",
														   "大于等于 |>="});
			this.cbo_Where.Location = new System.Drawing.Point(188, 42);
			this.cbo_Where.Name = "cbo_Where";
			this.cbo_Where.Size = new System.Drawing.Size(104, 20);
			this.cbo_Where.TabIndex = 10;
			this.cbo_Where.Text = "等于|=";
			// 
			// cbo_Value
			// 
			this.cbo_Value.Location = new System.Drawing.Point(294, 42);
			this.cbo_Value.Name = "cbo_Value";
			this.cbo_Value.Size = new System.Drawing.Size(266, 20);
			this.cbo_Value.TabIndex = 11;
			this.cbo_Value.Text = "<查询值>";
			// 
			// cbo_AndOr
			// 
			this.cbo_AndOr.Items.AddRange(new object[] {
														   "并且|AND",
														   "或者|OR"});
			this.cbo_AndOr.Location = new System.Drawing.Point(194, 18);
			this.cbo_AndOr.Name = "cbo_AndOr";
			this.cbo_AndOr.Size = new System.Drawing.Size(92, 20);
			this.cbo_AndOr.TabIndex = 12;
			// 
			// cmd_Query
			// 
			this.cmd_Query.Location = new System.Drawing.Point(370, 16);
			this.cmd_Query.Name = "cmd_Query";
			this.cmd_Query.Size = new System.Drawing.Size(82, 22);
			this.cmd_Query.TabIndex = 13;
			this.cmd_Query.Text = "执行查询(&F)";
			this.cmd_Query.Click += new System.EventHandler(this.cmd_Query_Click);
			// 
			// txt_SQL1
			// 
			this.txt_SQL1.Location = new System.Drawing.Point(4, 14);
			this.txt_SQL1.Multiline = true;
			this.txt_SQL1.Name = "txt_SQL1";
			this.txt_SQL1.Size = new System.Drawing.Size(558, 66);
			this.txt_SQL1.TabIndex = 14;
			this.txt_SQL1.Text = "";
			// 
			// cmd_Exit
			// 
			this.cmd_Exit.Location = new System.Drawing.Point(484, 14);
			this.cmd_Exit.Name = "cmd_Exit";
			this.cmd_Exit.TabIndex = 15;
			this.cmd_Exit.Text = "退出(&Q)";
			this.cmd_Exit.Click += new System.EventHandler(this.cmd_Exit_Click);
			// 
			// cmd_Clear
			// 
			this.cmd_Clear.Location = new System.Drawing.Point(8, 16);
			this.cmd_Clear.Name = "cmd_Clear";
			this.cmd_Clear.Size = new System.Drawing.Size(110, 23);
			this.cmd_Clear.TabIndex = 16;
			this.cmd_Clear.Text = "清楚过滤条件";
			this.cmd_Clear.Click += new System.EventHandler(this.cmd_Clear_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(126, 22);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(62, 14);
			this.label1.TabIndex = 17;
			this.label1.Text = "条件连接:";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txt_SQL);
			this.groupBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.groupBox1.Location = new System.Drawing.Point(0, 155);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(566, 100);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "查询过滤描述";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.txt_SQL1);
			this.groupBox2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.groupBox2.Location = new System.Drawing.Point(0, 69);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(566, 86);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "数据库查询语句(SQL)<高级查询>";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.cbo_Value);
			this.groupBox3.Controls.Add(this.cbo_AndOr);
			this.groupBox3.Controls.Add(this.cmd_Query);
			this.groupBox3.Controls.Add(this.label1);
			this.groupBox3.Controls.Add(this.cmd_Exit);
			this.groupBox3.Controls.Add(this.cmd_Add);
			this.groupBox3.Controls.Add(this.cmd_Clear);
			this.groupBox3.Controls.Add(this.cbo_FieldName);
			this.groupBox3.Controls.Add(this.cbo_Where);
			this.groupBox3.Location = new System.Drawing.Point(0, 0);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(566, 66);
			this.groupBox3.TabIndex = 0;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "过滤生成器";
			// 
			// frmReceiptFind
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(566, 419);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.lv_List);
			this.MaximizeBox = false;
			this.Name = "frmReceiptFind";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "单据查找";
			this.Load += new System.EventHandler(this.frmReceiptFind_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmReceiptFind_Load(object sender, System.EventArgs e)
		{
			string[] ArrTemp=null;
			string[] ArrTemp1=null;
			ArrTemp=m_Feilds.Split('|');
			InitData.InitComboBox(cbo_FieldName,ArrTemp);
			int j=ArrTemp.Length ;
			try
			{
				for (int i=0;i<j;i++)
				{
					ArrTemp1=ArrTemp[i].Split(';');
					m_FeildName=m_FeildName+ArrTemp1[1]+",";
					m_FeildCaption=m_FeildCaption+ArrTemp1[0]+",";

				}
				m_FeildName=m_FeildName.Substring(0,m_FeildName.Length-1);
				m_FeildCaption=m_FeildCaption.Substring(0,m_FeildCaption.Length-1);

				strSQL ="Select  "+m_FeildName +" From "+m_TableName;
				txt_SQL1.Text=strSQL;
			}
			catch
			{
			}

		}

		private void cmd_Add_Click(object sender, System.EventArgs e)
		{
			string sFeildNameCh="";
			string sFeildNameEN="";
			string sWhereCh="";
			string sWhereEN="";
			string sAndOrCh="";
			string sAndOrEN="";
			string sFeildType="";
			string[] ArrTemp=null; 
			if (bAndOr==true && cbo_AndOr.Text=="")
			{
				MessageBox.Show("请选择条件连接方式！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return;
			}
			if (cbo_FieldName.Text=="" || cbo_FieldName.Text=="<字段名称>")
			{
				MessageBox.Show("请选择查询的字段名称！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return;
			}
			else
			{ArrTemp=cbo_FieldName.Text.Split(';');}
			if (bAndOr==false && cbo_AndOr.Text=="")
			{
				txt_SQL1.Text =txt_SQL1.Text+" Where ";
			 }
			if (ArrTemp!=null && ArrTemp.Length >2)
			{
				sFeildNameCh=ArrTemp[0];
				sFeildNameEN=ArrTemp[1];
				sFeildType=ArrTemp[2];
				cbo_FieldName.Text=sFeildNameCh;
			}
			ArrTemp=null;
			if (cbo_Where.Text!="") {ArrTemp=cbo_Where.Text.Split('|');}
			if (ArrTemp!=null && ArrTemp.Length >1)
			{
				sWhereCh=ArrTemp[0].Trim();
				sWhereEN=ArrTemp[1].Trim();
			}
			ArrTemp=null;

			if (cbo_AndOr.Text!="") {ArrTemp=cbo_AndOr.Text.Split('|');}
			if (ArrTemp!=null && ArrTemp.Length >1)
			{
				sAndOrCh=ArrTemp[0].Trim();
				sAndOrEN=ArrTemp[1].Trim();
			}


			txt_SQL1.Text =txt_SQL1.Text+" "+sAndOrEN +" "+sFeildNameEN +sWhereEN +" '"+cbo_Value.Text +"'";

			txt_SQL.Text =txt_SQL.Text+ " "+sAndOrCh+" "+" ["+sFeildNameCh +"] "+sWhereCh +" '"+cbo_Value.Text +"'";


			cbo_FieldName.Text="";
			cbo_Value.Text="";
			cbo_AndOr.Text="";
			bAndOr=true;

		
		}
		private void QueryData()
		{
			try
			{
				System.Data.DataSet dst = new System.Data.DataSet();
				dst=newADO.GetDataSet(txt_SQL1.Text);
				if (dst==null){return;}

		
				InitData.InitListView(lv_List,newDB.GetDataArray(dst),m_FeildCaption);
			}
			catch(System.Exception ex)
			{
				string strErrInfo="错误提示：查询语句有错误！" + "\n" + ex.Message;
				System.Windows.Forms.MessageBox.Show(strErrInfo,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
			}
		}
		private void cmd_Exit_Click(object sender, System.EventArgs e)
		{
			m_ReceiptNo ="";
			this.Close(); 
		}

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

		}

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

		}

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

		}

		private void cmd_Clear_Click(object sender, System.EventArgs e)
		{
			txt_SQL1.Text =strSQL;
			txt_SQL.Text ="";
			bAndOr=false;
		}

		private void cmd_Query_Click(object sender, System.EventArgs e)
		{
			QueryData();
		}

		private void lv_List_DoubleClick(object sender, System.EventArgs e)
		{
			m_ReceiptNo =lv_List.SelectedItems[0].SubItems[0].Text ; 
			this.Close();

		}

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