using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinTree;
using Core.Mes.ClientFrameWork;


namespace Core.Mes.ClientManager
{
    public partial class frmMyMenuInfo : Form
    {
        public frmMyMenuInfo()
        {
            InitializeComponent();
        }

        private void frmMyMenuInfo_Load(object sender, EventArgs e)
        {
            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "selectPT_MenuInfo", 
                new object [] {}, out err);
            if (err == "")
            {
                DataTable dtMenuPos = ((DataSet)obj).Tables[0];
                dtMenuPos.PrimaryKey = new DataColumn[] { dtMenuPos.Columns[0] };
                UltraTreeNode rootNode = ultraTree1.Nodes.Add("", "/");
                AddMenuNode(rootNode, dtMenuPos.Copy());
                ultraTree1.Nodes[0].Selected = true;
                ultraTree1.Nodes[0].Expanded = true;
                setNodeStyle(ultraTree1.Nodes);
            }

            obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetPurviewTree",
                new object[] { "AND ROLEID='"+ClientCommon._UserInfo.UserRoleID+"'" }, out err);
            if (err == "")
            {
                DataTable dtUserMenu = ((DataSet)obj).Tables[0];
                for (int i = 0; i < dtUserMenu.Rows.Count; i++)
                {
                    setMyNodeStyle(dtUserMenu.Rows[i]["MENUID"].ToString(), ultraTree1.Nodes[0].Nodes);
                }
            }

            ultraTree1.ExpandAll();
        }

        void AddMenuNode(UltraTreeNode pNode, DataTable dtab)
        {
            DataView dvw = dtab.Copy().DefaultView;
            if (pNode.Key != "")
                dvw.RowFilter = "PARENTID='" + pNode.Key + "'";
            else
                dvw.RowFilter = "PARENTID is null";
            dvw.Sort = "XH ASC";

            foreach (DataRowView drow in dvw)
            {
                UltraTreeNode tNode = pNode.Nodes.Add(drow["ID_"].ToString(), drow["CAPTION"].ToString());
                AddMenuNode(tNode, dtab);
            }
        }

        void setNodeStyle(TreeNodesCollection nodes)
        {
            foreach (UltraTreeNode nd in nodes)
            {
                nd.Override.NodeAppearance.ForeColor = Color.Gray;
                if (nd.Nodes.Count > 0)
                    setNodeStyle(nd.Nodes);
            }
        }

        void setMyNodeStyle(string sFindMenuID, TreeNodesCollection nodes)
        { 
            foreach(UltraTreeNode nd in nodes)
            {
                if (nd.Key == sFindMenuID)
                {
                    nd.Override.NodeAppearance.ForeColor = Color.Black;
                    break;
                }
                else
                {
                    if (nd.Nodes.Count > 0)
                        setMyNodeStyle(sFindMenuID, nd.Nodes);
                }
            }
        }
    }
}