using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data ;
using JXC_MIS.MyClass;
using JXC_MIS.DataTier  ;
using JXC_MIS.Interface;
namespace JXC_MIS.UserTier.CommonForm
{
	/// <summary>
	/// 参照类型，网格列表参照=0,日期参照=1,ListBox列表参照=2,树型列表参照=3,树型文件路径参照=4,文本参照=5
	/// </summary>
	public enum _RefertoTypeFlags{ListView=0,Date=1,ListBox=2,Tree=3,FilePath=4,Text=5,Grid=10}

	/// <summary>
	/// 参照输入窗口，包括日期、下拉、列表、网格、树
	/// </summary>
	public class frmReferto : System.Windows.Forms.Form,JXC_MIS.Interface.IReferto
	{
		//窗口级变量
		ClsDataBaseAccess AdoDB=new ClsDataBaseAccess();
		ClsDataBase newDb=new ClsDataBase();
		ClsInitData InitData=new ClsInitData();

		//返回值
		System.Collections.Hashtable _GetReturnHashtable;

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

		private string  _DefaultValue;
//		private string _ReturnCols  ;		          //参照返回列值
//		private string _InputCols  ;			       //目标列对应列


		#region 字段属性
		/// <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>
//		/// 返回参照对应列“ 1:2:3:4:5”
//		/// </summary>
//		public string ReturnCols
//		{ 
//			get {return _ReturnCols;} 
//			set { _ReturnCols=value;} 
//		} 
//
//		/// <summary>
//		/// 目标对应输入的列 “ 1:2:3:4:5”
//		/// </summary>
//		public string InputCols
//		{ 
//			get {return _InputCols;} 
//			set { _InputCols=value;} 
//		} 

		
		/// <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 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

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

		}


		#region Windows 窗体设计器生成的代码

		private System.Windows.Forms.MonthCalendar cal_Date;
		private System.Windows.Forms.ListView lv_List;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.Button cmd_Cancel;
		private System.Windows.Forms.Button cmd_Ok;
		private System.Windows.Forms.TextBox txt_Search;
		private System.Windows.Forms.TreeView tv_Tree;
		private System.Windows.Forms.ListBox lst_List;//0=list 1=Date

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmReferto));
			this.cal_Date = new System.Windows.Forms.MonthCalendar();
			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.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.txt_Search = new System.Windows.Forms.TextBox();
			this.cmd_Cancel = new System.Windows.Forms.Button();
			this.cmd_Ok = new System.Windows.Forms.Button();
			this.tv_Tree = new System.Windows.Forms.TreeView();
			this.lst_List = new System.Windows.Forms.ListBox();
			this.txt_Text = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// cal_Date
			// 
			this.cal_Date.Location = new System.Drawing.Point(1, 26);
			this.cal_Date.Name = "cal_Date";
			this.cal_Date.TabIndex = 4;
			this.cal_Date.Visible = false;
			this.cal_Date.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.cal_Date_DateSelected);
			this.cal_Date.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.cal_Date_DateChanged);
			// 
			// lv_List
			// 
			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.columnHeader4});
			this.lv_List.FullRowSelect = true;
			this.lv_List.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lv_List.HideSelection = false;
			this.lv_List.HoverSelection = true;
			this.lv_List.Location = new System.Drawing.Point(1, 26);
			this.lv_List.Name = "lv_List";
			this.lv_List.Size = new System.Drawing.Size(273, 148);
			this.lv_List.TabIndex = 1;
			this.lv_List.View = System.Windows.Forms.View.Details;
			this.lv_List.Click += new System.EventHandler(this.lv_List_Click);
			this.lv_List.DoubleClick += new System.EventHandler(this.lv_List_DoubleClick);
			this.lv_List.KeyUp += new System.Windows.Forms.KeyEventHandler(this.lv_List_KeyUp);
			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 = "规格";
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "数量";
			this.columnHeader4.Width = 40;
			// 
			// txt_Search
			// 
			this.txt_Search.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txt_Search.Location = new System.Drawing.Point(1, 2);
			this.txt_Search.Multiline = true;
			this.txt_Search.Name = "txt_Search";
			this.txt_Search.Size = new System.Drawing.Size(179, 21);
			this.txt_Search.TabIndex = 5;
			this.txt_Search.Text = "";
			this.txt_Search.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_Search_KeyDown);
			this.txt_Search.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_Search_KeyPress);
			// 
			// cmd_Cancel
			// 
			this.cmd_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmd_Cancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.cmd_Cancel.Location = new System.Drawing.Point(230, 2);
			this.cmd_Cancel.Name = "cmd_Cancel";
			this.cmd_Cancel.Size = new System.Drawing.Size(44, 22);
			this.cmd_Cancel.TabIndex = 7;
			this.cmd_Cancel.Text = "取消";
			this.cmd_Cancel.Click += new System.EventHandler(this.cmd_Cancel_Click);
			// 
			// cmd_Ok
			// 
			this.cmd_Ok.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmd_Ok.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.cmd_Ok.Location = new System.Drawing.Point(180, 2);
			this.cmd_Ok.Name = "cmd_Ok";
			this.cmd_Ok.Size = new System.Drawing.Size(50, 22);
			this.cmd_Ok.TabIndex = 6;
			this.cmd_Ok.Text = "确定";
			this.cmd_Ok.Click += new System.EventHandler(this.cmd_Ok_Click);
			// 
			// tv_Tree
			// 
			this.tv_Tree.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tv_Tree.ImageIndex = -1;
			this.tv_Tree.Location = new System.Drawing.Point(1, 26);
			this.tv_Tree.Name = "tv_Tree";
			this.tv_Tree.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
																				new System.Windows.Forms.TreeNode("物资产品", new System.Windows.Forms.TreeNode[] {
																																								  new System.Windows.Forms.TreeNode("食品", new System.Windows.Forms.TreeNode[] {
																																																												  new System.Windows.Forms.TreeNode("节点6", new System.Windows.Forms.TreeNode[] {
																																																																																   new System.Windows.Forms.TreeNode("节点7", new System.Windows.Forms.TreeNode[] {
																																																																																																					new System.Windows.Forms.TreeNode("节点8", new System.Windows.Forms.TreeNode[] {
																																																																																																																									 new System.Windows.Forms.TreeNode("节点9")})})})}),
																																								  new System.Windows.Forms.TreeNode("日用"),
																																								  new System.Windows.Forms.TreeNode("家电"),
																																								  new System.Windows.Forms.TreeNode("服装")})});
			this.tv_Tree.SelectedImageIndex = -1;
			this.tv_Tree.Size = new System.Drawing.Size(273, 148);
			this.tv_Tree.TabIndex = 2;
			this.tv_Tree.Visible = false;
			this.tv_Tree.Click += new System.EventHandler(this.tv_Tree_Click);
			this.tv_Tree.DoubleClick += new System.EventHandler(this.tv_Tree_DoubleClick);
			// 
			// lst_List
			// 
			this.lst_List.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lst_List.ItemHeight = 12;
			this.lst_List.Location = new System.Drawing.Point(1, 26);
			this.lst_List.Name = "lst_List";
			this.lst_List.Size = new System.Drawing.Size(273, 146);
			this.lst_List.TabIndex = 0;
			this.lst_List.Visible = false;
			this.lst_List.DoubleClick += new System.EventHandler(this.lst_List_DoubleClick);
			this.lst_List.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lst_List_MouseUp);
			this.lst_List.SelectedIndexChanged += new System.EventHandler(this.lst_List_SelectedIndexChanged);
			// 
			// txt_Text
			// 
			this.txt_Text.Location = new System.Drawing.Point(1, 26);
			this.txt_Text.Multiline = true;
			this.txt_Text.Name = "txt_Text";
			this.txt_Text.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txt_Text.Size = new System.Drawing.Size(273, 156);
			this.txt_Text.TabIndex = 3;
			this.txt_Text.Text = "";
			this.txt_Text.Visible = false;
			this.txt_Text.TextChanged += new System.EventHandler(this.txt_Text_TextChanged);
			// 
			// frmReferto
			// 
			this.AcceptButton = this.cmd_Ok;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.CancelButton = this.cmd_Cancel;
			this.ClientSize = new System.Drawing.Size(276, 181);
			this.Controls.Add(this.txt_Text);
			this.Controls.Add(this.txt_Search);
			this.Controls.Add(this.cmd_Ok);
			this.Controls.Add(this.cmd_Cancel);
			this.Controls.Add(this.lst_List);
			this.Controls.Add(this.tv_Tree);
			this.Controls.Add(this.lv_List);
			this.Controls.Add(this.cal_Date);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MinimizeBox = false;
			this.Name = "frmReferto";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "参照数据信息";
			this.Resize += new System.EventHandler(this.frmReferto_Resize);
			this.Load += new System.EventHandler(this.frmReferto_Load);
			this.ResumeLayout(false);

		}
		#endregion


		

		public string GetRefertoValues(int p_index)
		{
			string strReturn = "";
			if (p_index < this._GetReturnHashtable.Keys.Count)
			{
				strReturn = this._GetReturnHashtable[p_index].ToString();
			}
			return strReturn; 
		}

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

		/// <summary>
		/// 获取参照返回值DataTable
		/// </summary>
		/// <returns></returns>
		public System.Data.DataTable GetReturnDataTable()
		{
			return null;
		}
		/// <summary>
		/// 设置参照返回值
		/// </summary>
		/// <returns></returns>
		private void SetRefertoValues()
		{
			_GetReturnHashtable = new Hashtable();

//			_GetReturnHashtable.Clear();

			switch(_RefertoType)
			{
				case 0:	//网格列表参照
					try
					{
						if (lv_List.SelectedItems  != null)
						{
							for(int i = 0 ; i < lv_List.SelectedItems[0].SubItems.Count ; i++ )
							{
								_GetReturnHashtable.Add(i,lv_List.SelectedItems[0].SubItems[i].Text);					
							}
						}
					}
					catch
					{
						_GetReturnHashtable.Add(0,txt_Search.Text);
					}
					break;
			
				case 1:	//日期参照
					_GetReturnHashtable.Add(0,txt_Search.Text);
					break;

				case 2:	//ListBox列表参照
					_GetReturnHashtable.Add(0,txt_Search.Text);
					break;

				case 3:	//树型列表参照
					_GetReturnHashtable.Add(0,txt_Search.Text);
					break;

				case 4:	//树型文件路径参照
					_GetReturnHashtable.Add(0,txt_Search.Text);
					break;
				case 5:	//文本参照
					_GetReturnHashtable.Add(0,txt_Search.Text);
					break;


			}//End Switch
		}

		private void frmReferto_Load(object sender, System.EventArgs e)
		{
			txt_Search.Text = this.DefaultValue;
			InitRefertoType();
		}

		/// <summary>
		/// 初始参照类型 	网格列表参照=0,日期参照=1,ListBox列表参照=2,树型列表参照=3,树型文件路径参照=4
		/// </summary>
		private void InitRefertoType()
		{
			string sSql=_DataSource;
			string sReplace="";
			lv_List.Visible =false;
			cal_Date.Visible =false;
			tv_Tree.Visible =false;
			lst_List.Visible =false;
			txt_Text.Visible =false;
			if (_OtherFilter!="")	sSql=sSql+" "+_OtherFilter;  //参照其他过滤条件

			//  日期参照
			if (_RefertoType==1)
			{
				cal_Date.Visible =true;
				DateTime todayDate = DateTime.Today;
				cal_Date.TodayDate = todayDate;
				//				cal_Date.SelectedDate = cal_Date.TodayDate;
				txt_Search.Text =cal_Date.TodayDate.ToShortDateString ();
				cal_Date.TodayDate = todayDate;
				this.Width=this.cal_Date.Width+12 ;
				this.cal_Date.Left =0;
				this.Height =this.cal_Date.Height+this.cal_Date.Top+30 ;

				cal_Date.Focus();
			}


			//网格列表参照
			if (_RefertoType==0)
			{
				lv_List.Visible =true;
				sReplace=GetReplace(sSql);
				if (sReplace!="") sSql=sSql.Replace(sReplace,txt_Search.Text) ;

				InitData.InitListView (lv_List,newDb.GetDataArray(sSql,""));
				lv_List.Focus();
			}

			// ListBox列表参照

			if (_RefertoType==2)
			{
				lst_List.Visible =true;
				sReplace=GetReplace(sSql);
				if (sReplace!="") sSql=sSql.Replace(sReplace,txt_Search.Text) ;
				InitData.InitListBox (lst_List,newDb.GetDataArray(AdoDB.GetDataSet(sSql)));
				lst_List.Focus();

			}
			// 树型列表参照

			if (_RefertoType==3)
			{
				tv_Tree.Visible =true;
				InitData.InitTreeView (tv_Tree,_DataSource);
				this.tv_Tree.Focus();

			}
			// 树型文件路径参照

			if (_RefertoType==4)
			{
				tv_Tree.Visible =true;
				InitData.InitDriveTree(tv_Tree);
				tv_Tree.Focus();

			}
	
			// 文本参照

			if (_RefertoType==5)
			{
				txt_Text.Visible =true;
				txt_Text.Text=txt_Search.Text;
				txt_Text.Focus();
			}
		}
		/// <summary>
		/// 获得字符串中存在有"@"字符后面到"'"或"空格"位置的字符串
		/// </summary>
		/// <param name="p_sSql"></param>
		private string GetReplace(string p_sSql)
		{
//			_OtherFilter
			string replace="";
			int iStart;
			int iEnd=0;
			int i=0;
			iStart=p_sSql.IndexOf("@");
			if (iStart!=-1)
			{
				while (p_sSql.Substring(iStart+i,1)!="'")
				{
					if (p_sSql.Substring(iStart+i,1)==" " || p_sSql.Substring(iStart+i,1)=="%") break;
					replace=replace+p_sSql.Substring(iStart+i,1);
					i++;
				}
			}
			iEnd=iStart+i;
			return replace;
		}

		private void cmd_Cancel_Click(object sender, System.EventArgs e)
		{
//			_GetReturnHashtable.Add(0,_DefaultValue);	
			_GetReturnHashtable=null;	
			this.Close(); 

		}

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

		private void lv_List_Click(object sender, System.EventArgs e)
		{
			txt_Search.Text =lv_List.SelectedItems[0].SubItems[0].Text ; 
//			lv_List.SelectedItems[0].Remove();
		}

		private void cal_Date_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
		{
			txt_Search.Text =e.End.Date.ToShortDateString()  ;
			SetRefertoValues();
			this.Close(); 
		}

		private void cal_Date_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
		{
			txt_Search.Text =e.End.Date.ToShortDateString()  ;
	
		}


		private void lv_List_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode ==Keys.Down || e.KeyCode ==Keys.Up )
			{
			}

		}

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

		private void tv_Tree_Click(object sender, System.EventArgs e)
		{
			TreeView control = ( TreeView ) sender ; 
			try
			{
				TreeNode tn = new TreeNode();
				tn = control.SelectedNode;
				txt_Search.Text =tn.Text ;
			}
			catch
			{
			  return;
			}
		}

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

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

		private void lst_List_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			ListBox control = ( ListBox ) sender ; 
			txt_Search.Text =control.Text ;
		}

		private void txt_Text_TextChanged(object sender, System.EventArgs e)
		{
				txt_Search.Text =txt_Text.Text ;
		}

		private void frmReferto_Resize(object sender, System.EventArgs e)
		{
			if (cal_Date.Visible ==true)
			{
				this.Width=this.cal_Date.Width+12 ;
				this.cal_Date.Left =0;
				this.Height =this.cal_Date.Height+this.cal_Date.Top+30 ;
			    return;
			}
	
			lv_List.Width  =this.Width -4;
			tv_Tree.Width  =lv_List.Width ;
			lst_List.Width  =lv_List.Width ;
			txt_Text.Width  =lv_List.Width ;
			lv_List.Height  =this.Height -lv_List.Top*2;
			tv_Tree.Height  =lv_List.Height;
			lst_List.Height  =lv_List.Height;
			txt_Text.Height  =lv_List.Height;
//			lv_List.Left   =(this.Width  -lv_List.Width)/2 ;
//			tv_Tree.Left  =lv_List.Left;
//			lst_List.Left  =lv_List.Left;
//			txt_Text.Left  =lv_List.Left;

		}

		private void lst_List_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			ListBox control = ( ListBox ) sender ; 
			txt_Search.Text =control.Text ;

		}

		private void lv_List_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			ListView control = ( ListView ) sender ; 
			if (control.SelectedItems.Count<=0) return; 
			txt_Search.Text =control.SelectedItems[0].SubItems[0].Text ; 

		}

		private void txt_Search_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{

		}

		private void txt_Search_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode ==Keys.Down)
			{
				InitRefertoType();
			}

		}


	}//End Class
}//End NameSpace
