using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Core.Mes.ClientFrameWork;
using Core.Mes.IBaseInterface;
//using Core.Mes.ServerFrameWork;

using System.Data;

using Infragistics.Win.UltraWinTree;

namespace Core.Mes.ClientPurviewManager
{
	/// <summary>
	/// FrmSelUserID 的摘要说明。
	/// </summary>
	public class FrmSelUserID : System.Windows.Forms.Form
	{
		private Infragistics.Win.UltraWinListBar.UltraListBar ultraListBar1;
		private Infragistics.Win.UltraWinEditors.UltraTextEditor ultraTextEditor1;
		private Infragistics.Win.Misc.UltraButton btnOK;
		private Infragistics.Win.Misc.UltraButton btnCancle;
        private Core.Mes.ClientPurviewManager.UcDepartment ucDepartment1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FrmSelUserID()
		{
			//
			// 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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmSelUserID));
            Infragistics.Win.UltraWinListBar.Group group1 = new Infragistics.Win.UltraWinListBar.Group(true);
            Infragistics.Win.UltraWinListBar.Group group2 = new Infragistics.Win.UltraWinListBar.Group();
            Infragistics.Win.UltraWinListBar.Group group3 = new Infragistics.Win.UltraWinListBar.Group();
            Infragistics.Win.UltraWinListBar.Group group4 = new Infragistics.Win.UltraWinListBar.Group();
            Infragistics.Win.UltraWinListBar.Group group5 = new Infragistics.Win.UltraWinListBar.Group();
            Infragistics.Win.UltraWinListBar.Group group6 = new Infragistics.Win.UltraWinListBar.Group();
            Infragistics.Win.UltraWinListBar.Group group7 = new Infragistics.Win.UltraWinListBar.Group();
            this.ultraListBar1 = new Infragistics.Win.UltraWinListBar.UltraListBar();
            this.ultraTextEditor1 = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
            this.btnOK = new Infragistics.Win.Misc.UltraButton();
            this.btnCancle = new Infragistics.Win.Misc.UltraButton();
            this.ucDepartment1 = new Core.Mes.ClientPurviewManager.UcDepartment();
            ((System.ComponentModel.ISupportInitialize)(this.ultraTextEditor1)).BeginInit();
            this.SuspendLayout();
            // 
            // ultraListBar1
            // 
            this.ultraListBar1.DefaultIcon = ((System.Drawing.Icon)(resources.GetObject("ultraListBar1.DefaultIcon")));
            this.ultraListBar1.DefaultStyle = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            this.ultraListBar1.FlatMode = true;
            this.ultraListBar1.GroupHeadersVisible = false;
            group1.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            group1.Text = "员工姓名";
            group2.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            group3.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            group4.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            group5.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            group6.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            group7.Style = Infragistics.Win.UltraWinListBar.Style.SmallIcons;
            this.ultraListBar1.Groups.Add(group1);
            this.ultraListBar1.Groups.Add(group2);
            this.ultraListBar1.Groups.Add(group3);
            this.ultraListBar1.Groups.Add(group4);
            this.ultraListBar1.Groups.Add(group5);
            this.ultraListBar1.Groups.Add(group6);
            this.ultraListBar1.Groups.Add(group7);
            this.ultraListBar1.Location = new System.Drawing.Point(196, 1);
            this.ultraListBar1.Name = "ultraListBar1";
            this.ultraListBar1.Size = new System.Drawing.Size(164, 475);
            this.ultraListBar1.ItemSelected += new Infragistics.Win.UltraWinListBar.ItemEventHandler(this.ultraListBar1_ItemSelected);
            // 
            // ultraTextEditor1
            // 
            this.ultraTextEditor1.AutoSize = true;
            this.ultraTextEditor1.Location = new System.Drawing.Point(219, 490);
            this.ultraTextEditor1.Name = "ultraTextEditor1";
            this.ultraTextEditor1.Size = new System.Drawing.Size(120, 21);
            this.ultraTextEditor1.TabIndex = 1;
            // 
            // btnOK
            // 
            this.btnOK.Location = new System.Drawing.Point(205, 524);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(64, 26);
            this.btnOK.TabIndex = 2;
            this.btnOK.Text = "确定";
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnCancle
            // 
            this.btnCancle.Location = new System.Drawing.Point(285, 524);
            this.btnCancle.Name = "btnCancle";
            this.btnCancle.Size = new System.Drawing.Size(64, 26);
            this.btnCancle.TabIndex = 3;
            this.btnCancle.Text = "取消";
            this.btnCancle.Click += new System.EventHandler(this.btnCancle_Click);
            // 
            // ucDepartment1
            // 
            this.ucDepartment1.Dock = System.Windows.Forms.DockStyle.Left;
            this.ucDepartment1.Location = new System.Drawing.Point(0, 0);
            this.ucDepartment1.Name = "ucDepartment1";
            this.ucDepartment1.Size = new System.Drawing.Size(192, 564);
            this.ucDepartment1.TabIndex = 0;
            // 
            // FrmSelUserID
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(360, 564);
            this.Controls.Add(this.ucDepartment1);
            this.Controls.Add(this.btnCancle);
            this.Controls.Add(this.btnOK);
            this.Controls.Add(this.ultraTextEditor1);
            this.Controls.Add(this.ultraListBar1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmSelUserID";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "员工姓名";
            this.Load += new System.EventHandler(this.FrmSelUserID_Load);
            ((System.ComponentModel.ISupportInitialize)(this.ultraTextEditor1)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion
		#region "	Valiable	"
		public string _currSelectID;
		public bool _changFlag = false;
		
		public DataSet _userData;

		public DataSet _departmentData;

		#endregion

		private void ultraListBar1_ItemSelected(object sender, Infragistics.Win.UltraWinListBar.ItemEventArgs e)
		{
			this.ultraTextEditor1.Text = e.Item.Text ;
			this.ultraTextEditor1.Tag = e.Item.Key ;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			_changFlag = false;
			if (this.ultraTextEditor1.Tag != null)
			{
				if (this._currSelectID == this.ultraTextEditor1.Tag .ToString())
				{
					_changFlag = false;
				}
				else
				{
					_changFlag = true;
					_currSelectID = this.ultraTextEditor1.Tag .ToString();
				}
			}
			this.Close();
		}

		private void btnCancle_Click(object sender, System.EventArgs e)
		{
			_changFlag = false;
			this.Close();
		}
		private void ChangDepart(string strWhere)
		{
			try
			{
				string strFilter = GetRefreshWhereUser(strWhere);
				this._userData.Tables[0].DefaultView.RowFilter = strFilter;
                this._userData.Tables[0].DefaultView.Sort = "NAME";
				
				this.ultraListBar1.Groups[0].Items.Clear();
				foreach (DataRowView myDRV in this._userData.Tables[0].DefaultView)
				{
					try
					{
						this.ultraListBar1.Groups[0].Items.Add((string)myDRV["USERID"],(string)myDRV["NAME"]);
					}
					catch{}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				this._userData.Tables[0].DefaultView.RowFilter = "1=2";
			}
		}
		private string GetRefreshWhereUser(string strWhere)
		{
			string sqlstr = "";
			
			ArrayList al = this.GetUserID(strWhere);
			if (al == null || al.Count == 0)
			{
				sqlstr = "1=2";
			}
			else
			{
				sqlstr += " USERID in ('" + al[0].ToString() + "'";
				for (int i = 1 ; i < al.Count ; i++)
				{
					sqlstr += ", '" + al[i].ToString() + "' ";
				}
				sqlstr += ")";
				
			}
			
			return sqlstr;
		}
		private ArrayList GetUserID(string strWhere)
		{
			ArrayList al = new ArrayList();
			DataTable dt = null;
			if (this._userData == null)
				dt = null;
			else
				dt = this._userData.Tables[0];
			
			if (dt != null)
			{
				
				string strFilter = "DEPARTMENTID in (" + strWhere + ")";
					
				dt.DefaultView.RowFilter = strFilter;					
			

				for(int i =0; i < dt.DefaultView.Count ; i++)
				{
					al.Add(dt.DefaultView[i]["USERID"].ToString());
				}

				return al;
			}
			else
				return null;
		}

		private void FrmSelUserID_Load(object sender, System.EventArgs e)
		{
			this.ucDepartment1._departmentData = this._departmentData;
			this.ucDepartment1.RefreshTree();
            this.ucDepartment1.ultraTree1.ExpandAll();

			this.ucDepartment1.ultraTree1.AfterSelect += new Infragistics.Win.UltraWinTree.AfterNodeSelectEventHandler(ultraTree1_AfterSelect);
            if (ucDepartment1.ultraTree1.Nodes.Count > 0) ucDepartment1.ultraTree1.Nodes[0].Selected = true;
		}
		private void ultraTree1_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e)
		{
			if (e.NewSelections == null || e.NewSelections.Count == 0) 
			{}
			else
			{	
				string str = "'" + e.NewSelections[0].Key + "'";
				str = GetSelectedID(ref str, e.NewSelections[0]);
				
				ChangDepart(str);
			}
		}
		private string GetSelectedID(ref string str, UltraTreeNode utn)
		{
			try
			{
				foreach(UltraTreeNode ut in utn.Nodes )
				{
					str += ",'" + ut.Key + "'";

					if (ut.Nodes.Count > 0)
						GetSelectedID(ref str, ut);
				}	
				return str;
			}
			catch
			{
				return "";
			}
		}
	}
}
