using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Infragistics.Win.UltraWinTree;
using Core.Mes.IBaseInterface;

namespace Core.Mes.ClientPurviewManager
{
	/// <summary>
		/// UcDepartment 的摘要说明。
		/// </summary>
		public class UcDepartment : System.Windows.Forms.UserControl
		{
			public Infragistics.Win.UltraWinTree.UltraTree ultraTree1;
			/// <summary> 
			/// 必需的设计器变量。
			/// </summary>
			private System.ComponentModel.Container components = null;

			public UcDepartment()
			{
				// 该调用是 Windows.Forms 窗体设计器所必需的。
				InitializeComponent();

				// TODO: 在 InitializeComponent 调用后添加任何初始化

			}

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

			#region 组件设计器生成的代码
			/// <summary> 
			/// 设计器支持所需的方法 - 不要使用代码编辑器 
			/// 修改此方法的内容。
			/// </summary>
			private void InitializeComponent()
			{
                Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance();
                Infragistics.Win.UltraWinTree.UltraTreeColumnSet ultraTreeColumnSet1 = new Infragistics.Win.UltraWinTree.UltraTreeColumnSet();
                this.ultraTree1 = new Infragistics.Win.UltraWinTree.UltraTree();
                ((System.ComponentModel.ISupportInitialize)(this.ultraTree1)).BeginInit();
                this.SuspendLayout();
                // 
                // ultraTree1
                // 
                appearance1.BackColor = System.Drawing.Color.LightYellow;
                this.ultraTree1.Appearance = appearance1;
                this.ultraTree1.ColumnSettings.RootColumnSet = ultraTreeColumnSet1;
                this.ultraTree1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.ultraTree1.HideSelection = false;
                this.ultraTree1.Location = new System.Drawing.Point(0, 0);
                this.ultraTree1.Name = "ultraTree1";
                this.ultraTree1.Size = new System.Drawing.Size(272, 408);
                this.ultraTree1.TabIndex = 0;
                // 
                // UcDepartment
                // 
                this.Controls.Add(this.ultraTree1);
                this.Name = "UcDepartment";
                this.Size = new System.Drawing.Size(272, 408);
                this.Load += new System.EventHandler(this.UcDepartment_Load);
                ((System.ComponentModel.ISupportInitialize)(this.ultraTree1)).EndInit();
                this.ResumeLayout(false);

			}
			#endregion

			public DataSet _departmentData ;
			private const string DEPARTMENT_BASE  = "ROOT";
		
			private void UcDepartment_Load(object sender, System.EventArgs e)
			{
			  
			}
			public void RefreshTree()
			{
				TreeNodeLoadData();
			}
			private void TreeNodeLoadData()
			{
				try
				{
					if (this.ultraTree1.Nodes.Count > 0)
						RemoveAllTreeNode(this.ultraTree1);

					UltraTreeFillData();

//					this.ultraTree1.ExpandAll();
				}
        catch (System.Exception ex)
        {
          System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
			}
			private void RemoveAllTreeNode(UltraTree ut)
			{
				int count = ut.Nodes.Count ;
				
				for (int i = count -1; i >= 0 ; i--)
				{
					ut.Nodes[i].Remove();
				}
			}
			private void UltraTreeFillData()
			{
				if (_departmentData != null && _departmentData.Tables.Count > 0)
				{
					DataTable dt = _departmentData.Tables[0];

					DataRow[] drs = dt.Select("DEPARTMENTPID = '"+DEPARTMENT_BASE+"'");
			
					foreach(DataRow dr in drs)
					{
						try
						{
							UltraTreeNode tn = new UltraTreeNode();
							tn.Text = dr["DEPARTMENTNAME"].ToString();
							tn.Key  = dr["DEPARTMENTID"].ToString();
							AddTreeNodes(tn);

							this.ultraTree1.Nodes.Add(tn);
						}
            catch (System.Exception ex)
            {
              System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
					}
				}
			}
			private void AddTreeNodes(UltraTreeNode tn)
			{
				DataTable dt = _departmentData.Tables[0];
				DataRow[] drs = dt.Select("DEPARTMENTPID = '"+tn.Key +"'");

				foreach(DataRow dr in drs)
				{
					try
					{
						UltraTreeNode utn = new UltraTreeNode();
						utn.Text = dr["DEPARTMENTNAME"].ToString();
						utn.Key  = dr["DEPARTMENTID"].ToString();
						AddTreeNodes(utn);
					
						tn.Nodes.Add(utn);
					}
					catch(System.Exception ex)
          {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
          }
				}
			}
      string _ParentID = "";
      public string ParentID
      {
        get 
        {
          try
          {
            _ParentID = "";
            if (this.ultraTree1.SelectedNodes.Count > 0)
            {
              GetParenID(this.ultraTree1.SelectedNodes[0]);
            }
          }
          catch (System.Exception ex)
          {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
          }
          return _ParentID;
        }
      }
      public void GetParenID(Infragistics.Win.UltraWinTree.UltraTreeNode TreeNode)
      {
        if (TreeNode.Parent == null)
        {
          return;
        }
        else
        {
          _ParentID = _ParentID + TreeNode.Key + ";";
          GetParenID(TreeNode.Parent);
        }
      }
		}
	}