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

namespace JXC_MIS.UserTier.CommonForm
{
	public class frmRefertoList : JXC_MIS.UserTier.BaseForm.frmTreeFlexGrid,JXC_MIS.Interface.IReferto
	{
		private System.Windows.Forms.Button cmd_OK;
		private System.Windows.Forms.CheckBox chk_AllSelect;
		private System.Windows.Forms.ComboBox cbo_FliterValue;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cbo_FilterName;
		private System.ComponentModel.IContainer components = null;
		//返回值
		System.Collections.Hashtable _GetReturnHashtable=null;

		//字段
		private System.Data.CommandType _CommandType;
		private int _RefertoType = 0 ;
		private string  _DataSourceKey;
		private string  _DataSource;
		private string  _FieldSource;
		private string  _FilterSource;
		private string  _DefaultValue	;
		private string  _OtherFilter;   //参照其他过滤条件

		private System.Windows.Forms.MenuItem mnu_ColWidthSetup;
		private System.Data.DataTable dt =new System.Data.DataTable();
		protected ClsReceiptAttrib newR_Attrib=new ClsReceiptAttrib();
		public frmRefertoList()
		{
			// 该调用是 Windows 窗体设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitializeComponent 调用后添加任何初始化
		}
		#region 字段属性

		/// <summary>
		/// 获得数据表
		/// </summary>
		public System.Data.DataTable GetDataTable 
		{
			get {return dt ;} 
		}



		//		/// <summary>
		//		/// 获取参照返回值
		//		/// </summary>
		//		/// <returns></returns>
		//		public System.Collections.Hashtable GetReturnHashtable
		//		{
		//			get {return _GetReturnHashtable;}
		//		}
		//
		//		/// <summary>
		//		/// 获取参照返回值DataTable
		//		/// </summary>
		//		/// <returns></returns>
		//		public System.Data.DataTable GetReturnDataTable
		//		{
		//			get {return dt;}
		//		}


		/// <summary>
		/// 参照类型，网格列表参照=0,日期参照=1,ListBox列表参照=2,树型列表参照=3,树型文件路径参照=4。
		/// 也可以用_RefertoTypeFlag顾名思义
		/// </summary>
		public int RefertoType 
		{
			set { _RefertoType=value ;} 
			get {return _RefertoType ;} 
		}

		/// <summary>
		/// 参照类型，也可以用_RefertoType属性直接设置数字值
		/// </summary>
		public _RefertoTypeFlags RefertoTypeFlag
		{
			set { this._RefertoType = (int)value ;}
			get {return (_RefertoTypeFlags)_RefertoType ;} 		
		}
		
		/// <summary>
		/// 数据字段唯一标识
		/// </summary>
		public string DataSourceKey 
		{
			set { _DataSourceKey=value ;} 
			get {return _DataSourceKey ;} 
		}
		/// <summary>
		/// 数据源，Select SQL、表名、存储
		/// </summary>
		public string DataSource 
		{
			set { _DataSource=value ;} 
			get {return _DataSource ;} 
		}

		/// <summary>
		/// 数据源命令类型
		/// </summary>
		public System.Data.CommandType CommandType 
		{
			set { _CommandType=value ;} 
			get {return _CommandType ;} 
		}

		/// <summary>
		/// 过滤字段名
		/// </summary>
		public string FieldSource 
		{
			set { _FieldSource=value ;} 
			get {return _FieldSource ;} 
		}

		/// <summary>
		/// 过滤条件
		/// </summary>
		public string FilterSource 
		{
			set { _FilterSource=value ;} 
			get {return _FilterSource ;} 
		}
		/// <summary>
		/// 其他过滤条件
		/// </summary>
		public string OtherFilter 
		{
			set { _OtherFilter=value ;} 
			get {return _OtherFilter ;} 
		}
		/// <summary>
		/// 初始默认值
		/// </summary>
		public string DefaultValue 
		{
			set { _DefaultValue = value ;} 
			get {return _DefaultValue ;} 
		}
		#endregion
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region 设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.cmd_OK = new System.Windows.Forms.Button();
			this.chk_AllSelect = new System.Windows.Forms.CheckBox();
			this.cbo_FliterValue = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.cbo_FilterName = new System.Windows.Forms.ComboBox();
			this.mnu_ColWidthSetup = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(740, 446);
			// 
			// spt_H
			// 
			this.spt_H.Location = new System.Drawing.Point(144, 198);
			this.spt_H.Name = "spt_H";
			this.spt_H.Size = new System.Drawing.Size(596, 4);
			// 
			// spt_V
			// 
			this.spt_V.Location = new System.Drawing.Point(140, 198);
			this.spt_V.Name = "spt_V";
			this.spt_V.Size = new System.Drawing.Size(4, 248);
			// 
			// tv_Tree
			// 
			this.tv_Tree.Name = "tv_Tree";
			this.tv_Tree.Size = new System.Drawing.Size(140, 446);
			// 
			// cbo_SetupName
			// 
			this.cbo_SetupName.Name = "cbo_SetupName";
			this.cbo_SetupName.Size = new System.Drawing.Size(124, 20);
			// 
			// t_Find
			// 
			this.t_Find.Visible = false;
			// 
			// pnl_Filter
			// 
			this.pnl_Filter.Name = "pnl_Filter";
			// 
			// cmd_Cancel
			// 
			this.cmd_Cancel.Name = "cmd_Cancel";
			// 
			// cmd_ExeQuery
			// 
			this.cmd_ExeQuery.Name = "cmd_ExeQuery";
			// 
			// grd_Filter
			// 
			this.grd_Filter.Name = "grd_Filter";
			// 
			// sbr_StatusBar
			// 
			this.sbr_StatusBar.Name = "sbr_StatusBar";
			this.sbr_StatusBar.Size = new System.Drawing.Size(740, 25);
			// 
			// tbr_Toolbar
			// 
			this.tbr_Toolbar.Name = "tbr_Toolbar";
			this.tbr_Toolbar.Size = new System.Drawing.Size(740, 28);
			// 
			// grd_Top
			// 
			this.grd_Top.Cols = 21;
			this.grd_Top.Location = new System.Drawing.Point(140, 0);
			this.grd_Top.Name = "grd_Top";
			this.grd_Top.Rows = 675;
			this.grd_Top.Size = new System.Drawing.Size(600, 198);
			this.grd_Top.Row_DoubleClick += new ljs.FlexGridRow_DoubleClick(this.grd_Top_Row_DoubleClick);
			// 
			// grd_Bottom
			// 
			this.grd_Bottom.Cols = 21;
			this.grd_Bottom.Location = new System.Drawing.Point(144, 202);
			this.grd_Bottom.Name = "grd_Bottom";
			this.grd_Bottom.Rows = 675;
			this.grd_Bottom.Size = new System.Drawing.Size(594, 244);
			this.grd_Bottom.Row_DoubleClick += new ljs.FlexGridRow_DoubleClick(this.grd_Bottom_Row_DoubleClick);
			// 
			// ctm_Menu
			// 
			this.ctm_Menu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnu_ColWidthSetup});
			// 
			// img_ImageList
			// 
			this.img_ImageList.ImageStream = null;
			// 
			// cmd_OK
			// 
			this.cmd_OK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.cmd_OK.Location = new System.Drawing.Point(664, 4);
			this.cmd_OK.Name = "cmd_OK";
			this.cmd_OK.Size = new System.Drawing.Size(74, 20);
			this.cmd_OK.TabIndex = 27;
			this.cmd_OK.Text = "确定";
			this.cmd_OK.Click += new System.EventHandler(this.cmd_OK_Click);
			// 
			// chk_AllSelect
			// 
			this.chk_AllSelect.Location = new System.Drawing.Point(612, 2);
			this.chk_AllSelect.Name = "chk_AllSelect";
			this.chk_AllSelect.Size = new System.Drawing.Size(50, 24);
			this.chk_AllSelect.TabIndex = 25;
			this.chk_AllSelect.Text = "全选";
			this.chk_AllSelect.CheckedChanged += new System.EventHandler(this.chk_AllSelect_CheckedChanged);
			// 
			// cbo_FliterValue
			// 
			this.cbo_FliterValue.Items.AddRange(new object[] {
																 "<全部>"});
			this.cbo_FliterValue.Location = new System.Drawing.Point(326, 4);
			this.cbo_FliterValue.Name = "cbo_FliterValue";
			this.cbo_FliterValue.Size = new System.Drawing.Size(282, 20);
			this.cbo_FliterValue.TabIndex = 24;
			this.cbo_FliterValue.Text = "<输入查询值>";
			this.cbo_FliterValue.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.cbo_FliterValue_KeyPress);
			this.cbo_FliterValue.SelectedIndexChanged += new System.EventHandler(this.cbo_FliterValue_SelectedIndexChanged);
			this.cbo_FliterValue.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.cbo_FliterValue_DrawItem);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(254, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(246, 16);
			this.label1.TabIndex = 23;
			this.label1.Text = "查询内容：                值：";
			// 
			// cbo_FilterName
			// 
			this.cbo_FilterName.Items.AddRange(new object[] {
																"国标型号",
																"国标代码"});
			this.cbo_FilterName.Location = new System.Drawing.Point(326, 4);
			this.cbo_FilterName.Name = "cbo_FilterName";
			this.cbo_FilterName.Size = new System.Drawing.Size(84, 20);
			this.cbo_FilterName.TabIndex = 28;
			this.cbo_FilterName.Text = "国标型号";
			// 
			// mnu_ColWidthSetup
			// 
			this.mnu_ColWidthSetup.Index = 0;
			this.mnu_ColWidthSetup.Text = "列宽设置";
			this.mnu_ColWidthSetup.Click += new System.EventHandler(this.mnu_ColWidthSetup_Click);
			// 
			// frmRefertoList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(740, 499);
			this.Controls.Add(this.cbo_FliterValue);
			this.Controls.Add(this.cbo_FilterName);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.cmd_OK);
			this.Controls.Add(this.chk_AllSelect);
			this.Name = "frmRefertoList";
			this.Load += new System.EventHandler(this.frmRefertoList_Load);
			this.Controls.SetChildIndex(this.pnl_Filter, 0);
			this.Controls.SetChildIndex(this.tbr_Toolbar, 0);
			this.Controls.SetChildIndex(this.sbr_StatusBar, 0);
			this.Controls.SetChildIndex(this.panel1, 0);
			this.Controls.SetChildIndex(this.chk_AllSelect, 0);
			this.Controls.SetChildIndex(this.cmd_OK, 0);
			this.Controls.SetChildIndex(this.label1, 0);
			this.Controls.SetChildIndex(this.cbo_FilterName, 0);
			this.Controls.SetChildIndex(this.cbo_FliterValue, 0);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmRefertoList_Load(object sender, System.EventArgs e)
		{
			tv_Tree.Width =0;
			spt_H.Visible =true;
			this.grd_Top.Height =230;
			spt_H.BringToFront() ;
			this.ShowResize();
			this.grd_Bottom.Clear();
			this.grd_Bottom.Rows=2;
			ShowData(grd_Top);

			InitGridFace();
		}
		private void InitGridFace()
		{
			int j;
			j=this.grd_Top.Cols;
			grd_Bottom.Cols =j;
			for(int i=0; i<j;i++)
			{
				grd_Bottom.SetCellText(0,i,grd_Top.GetCellText(0,i) );
			}
			grd_Bottom.SetColWidth(grd_Top.GetColWidth());
		}
		private  void ShowData(ljs.MyFlexGrid p_FlexGrid)
		{
			string p_Filter;
			if (cbo_FliterValue.Text=="<全部>")
			{ 
				p_Filter="";
			} 
			else 
			{ 
				p_Filter=cbo_FliterValue.Text;
			}
			//添加行号
			p_FlexGrid.Clear();

			string strDataSource = _DataSource.ToLower();

			if (strDataSource.IndexOf("select")>=0 && strDataSource.IndexOf("from")>0)
			{
				//SQL		
				InitData.ShowGridData(p_FlexGrid,_DataSource);
			}
			else
			{
				//存储过程，必须有参数，新版支持
				InitData.ShowGridData(p_FlexGrid,_DataSource+ "  '"+p_Filter +"'");
			}


			//			p_FlexGrid.ColWidthAuto();
			p_FlexGrid.SetCellWidth =newR_Attrib.GetDetailColWidth("库存参照");
		}

		/// <summary>
		/// 获取参照返回值
		/// </summary>
		/// <returns></returns>
		public System.Collections.Hashtable GetReturnHashtable()
		{
			return _GetReturnHashtable;
		}

		/// <summary>
		/// 获取参照返回值DataTable
		/// </summary>
		/// <returns></returns>
		public System.Data.DataTable GetReturnDataTable()
		{
			return dt;
		}

		private void grd_Top_Row_DoubleClick(object sender, System.EventArgs e)
		{
			int j;
			if (grd_Top.GetCellText(grd_Top.Row,0)!="√")
			{
				j=this.grd_Top.Cols;
				grd_Top.SetCellText(grd_Top.Row,0,"√");
				for(int i=1; i<j;i++)
				{
					this.grd_Bottom.SetCellText(grd_Bottom.Rows-1,i,grd_Top.GetCellText(grd_Top.Row,i));
				}
				this.grd_Bottom.SetCellText(grd_Bottom.Rows-1,0,grd_Bottom.Row.ToString());
				this.grd_Bottom.RowInsert("",grd_Bottom.Rows);
			}
			else
			{
				grd_Top.SetCellText(grd_Top.Row,0,grd_Top.Row.ToString());
				j=this.grd_Bottom.Rows;
				for(int i=1; i<j;i++)
				{
					if (this.grd_Bottom.GetCellText(i,1)==grd_Top.GetCellText(grd_Top.Row,1))
					{
						this.grd_Bottom.RowRemove(i);
						this.grd_Bottom.SetRowNo();
					}
				}
			}

		}

		private void grd_Bottom_Row_DoubleClick(object sender, System.EventArgs e)
		{
			this.grd_Bottom.RowRemove(this.grd_Bottom.Row);
			this.grd_Bottom.SetRowNo();
		}

		private void chk_AllSelect_CheckedChanged(object sender, System.EventArgs e)
		{
			int j=this.grd_Top.Rows;
			if (chk_AllSelect.Checked==true)
			{
				grd_Bottom.Rows =j;
				int cs=grd_Top.Cols;

				for(int i=1; i<j;i++)
				{
					grd_Top.SetCellText(i,0,"√");
					for(int c=1; c<cs;c++)
					{
						this.grd_Bottom.SetCellText(i,c,grd_Top.GetCellText(i,c));
					}
					this.grd_Bottom.SetCellText(i,0,i.ToString());

				}
			}
			
			else
			{
				//添加行号
				for(int i=1; i<j;i++)
				{
					grd_Top.SetCellText(i,0,i.ToString());

				}
				grd_Bottom.Rows=2;
				grd_Bottom.Clear();
			
			}
		}

		private void cmd_OK_Click(object sender, System.EventArgs e)
		{
			//			System.Data.DataColumn newCol=new System.Data.DataColumn() ;
			int rs=grd_Bottom.Rows-1 ;
			int cs=grd_Bottom.Cols ;
			for (int c=0;c<cs;c++)
			{
				dt.Columns.Add(grd_Top.GetCellText(0,c)) ;
			}


			for (int r=1;r<rs;r++)
			{
				dt.Rows.Add(dt.NewRow());
				for (int c=0;c<cs;c++)
				{
					dt.Rows[dt.Rows.Count-1][c]=grd_Bottom.GetCellText(r,c);
				
				}
			
			}
			this.Close();

		}
		private void mnu_ColWidthSetup_Click(object sender, System.EventArgs e)
		{
			frmColWidthSetup f=new frmColWidthSetup();
			f.GridSource=this.grd_Top ;
			f.ReceiptName ="库存参照" ; 
			f.Show();
			grd_Top.SetCellWidth =newR_Attrib.GetDetailColWidth("库存参照");


		}
		private void cbo_FliterValue_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
		{
			
		}

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

		private void cbo_FliterValue_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (e.KeyChar==13)
			{
				ShowData(grd_Top);
			}
		}

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


	}
}

