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

using Infragistics.Win.UltraWinTree;

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

        public FrmSetDepart()
        {
            //
            // 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.ucDepartment1 = new Core.Mes.ClientPurviewManager.UcDepartment();
            this.Button1 = new Infragistics.Win.Misc.UltraButton();
            this.Button2 = new Infragistics.Win.Misc.UltraButton();
            this.ultraLabel1 = new Infragistics.Win.Misc.UltraLabel();
            this.ultraTextEditor1 = new Infragistics.Win.UltraWinEditors.UltraTextEditor();
            ((System.ComponentModel.ISupportInitialize)(this.ultraTextEditor1)).BeginInit();
            this.SuspendLayout();
            // 
            // ucDepartment1
            // 
            this.ucDepartment1.Dock = System.Windows.Forms.DockStyle.Top;
            this.ucDepartment1.Location = new System.Drawing.Point(0, 0);
            this.ucDepartment1.Name = "ucDepartment1";
            this.ucDepartment1.Size = new System.Drawing.Size(230, 311);
            this.ucDepartment1.TabIndex = 0;
            // 
            // Button1
            // 
            this.Button1.Location = new System.Drawing.Point(24, 359);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(75, 26);
            this.Button1.TabIndex = 1;
            this.Button1.Text = "确定";
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            // 
            // Button2
            // 
            this.Button2.Location = new System.Drawing.Point(128, 359);
            this.Button2.Name = "Button2";
            this.Button2.Size = new System.Drawing.Size(75, 26);
            this.Button2.TabIndex = 2;
            this.Button2.Text = "取消";
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            // 
            // ultraLabel1
            // 
            this.ultraLabel1.Location = new System.Drawing.Point(28, 330);
            this.ultraLabel1.Name = "ultraLabel1";
            this.ultraLabel1.Size = new System.Drawing.Size(56, 16);
            this.ultraLabel1.TabIndex = 3;
            this.ultraLabel1.Text = "选择部门";
            // 
            // ultraTextEditor1
            // 
            this.ultraTextEditor1.AutoSize = true;
            this.ultraTextEditor1.Location = new System.Drawing.Point(94, 325);
            this.ultraTextEditor1.Name = "ultraTextEditor1";
            this.ultraTextEditor1.Size = new System.Drawing.Size(100, 21);
            this.ultraTextEditor1.TabIndex = 4;
            // 
            // FrmSetDepart
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(230, 395);
            this.Controls.Add(this.ultraTextEditor1);
            this.Controls.Add(this.ultraLabel1);
            this.Controls.Add(this.Button2);
            this.Controls.Add(this.Button1);
            this.Controls.Add(this.ucDepartment1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmSetDepart";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "选择部门";
            this.Load += new System.EventHandler(this.FrmSetDepart_Load);
            ((System.ComponentModel.ISupportInitialize)(this.ultraTextEditor1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion
        private DataSet _departmentData;

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

            this.ucDepartment1.ultraTree1.AfterSelect += new Infragistics.Win.UltraWinTree.AfterNodeSelectEventHandler(ultraTree1_AfterSelect);
            this.ucDepartment1.ultraTree1.DoubleClick += new EventHandler(ultraTree1_DoubleClick);
        }
        //==========================================================
        // 获得部门数据
        //==========================================================
        private void GetDepartmentData()
        {
            try
            {
                CallingMessage par = new CallingMessage();
                par.ServerName = "PurviewManager";
                //par.AssemblyName  = "Core.Mes.PurviewManager";
                par.ClassName = "Core.Mes.PurviewManager.PurviewManager";
                par.MethodName = "SelectUAM_DEPARTMENT";
                par.args = new object[] { " order by DEPARTMENTID " };
                par.ServerType = MesServerType.MesSystemBaseServer;

                string strOut = "";

                _departmentData = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod(par, out strOut) as DataSet;

            }
            catch //(Exception ex)
            {
                //	CoreMesLogger.log(this.GetType()).Error(ex.Message);
            }
        }
        private void ultraTree1_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e)
        {
            if (e.NewSelections == null || e.NewSelections.Count == 0)
            { }
            else
            {
                ParentID = e.NewSelections[0].Key;
                this.ultraTextEditor1.Text = e.NewSelections[0].Text;
                this.ultraTextEditor1.Tag = e.NewSelections[0].Key;

                GetParenID(e.NewSelections[0]);

                string str = "'" + e.NewSelections[0].Key + "'";
                str = GetSelectedID(ref str, e.NewSelections[0]);

                this.Button1.Tag = 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 "";
            }
        }

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

            SelectedName = "";
            SelectedID = "";
            if (GetChildIDFlag)
            {
                SelectedName = this.ultraTextEditor1.Text;
                SelectedID = this.Button1.Tag.ToString();
            }
            else
            {
                if (this.ultraTextEditor1.Text.Trim() != "" && this.ultraTextEditor1.Tag.ToString() != "")
                {
                    SelectedName = this.ultraTextEditor1.Text;
                    SelectedID = this.ultraTextEditor1.Tag.ToString();
                }
            }
            this.Close();
        }
        public string SelectedName = "";
        public string SelectedID = "";
        public string ParentID = "";
        public string RootID = "";
        public bool GetChildIDFlag = true;
        private void Button2_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        private void ultraTree1_DoubleClick(object sender, System.EventArgs e)
        {
            Button1_Click(null, new EventArgs());
        }
        public void GetParenID(Infragistics.Win.UltraWinTree.UltraTreeNode TreeNode)
        {
            if (TreeNode.Parent == null)
            {
                return;
            }
            else
            {
                ParentID = ParentID + TreeNode.Key + ";";
                GetParenID(TreeNode.Parent);
            }
        }
    }
}
