using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using COE.Components;
using MyApp.AppFunction;
using MyApp.DBTable.company;

namespace MYAPP.Manage.SelectItem
{
    public partial class Select_UserSingle_ByRole : MyApp.Web.UI.PublicPage
    {
        bool _MTF = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    _MTF = Convert.ToBoolean(Request.QueryString["MTF"]);
                }
                catch (Exception) { }
                BuildTree();
            }
        }

        protected void BuildTree()
        {
            TV_Tree.Nodes.Clear();

            db.DataTableFill("select Role_ID,User_ID,UI_Name,UI_Sex,UI_Online from [user].User_Info", "User_Info");

            ComponentArt.Web.UI.TreeViewNode newNode = CreateNode("无", -1, true, "folder.gif", "", true, false);
            TV_Tree.Nodes.Add(newNode);

            PopulateTeacher(0, newNode);

            db.DataTableFill("select Role_ID,RI_Name,RI_UpID from [user].Role_Info order by RI_Sort", "RI");

            DataRow dr = db.myds.Tables["RI"].NewRow(); dr["Role_ID"] = 0; db.myds.Tables["RI"].Rows.Add(dr);
            db.myds.Relations.Add("NodeRelation", db.myds.Tables["RI"].Columns["Role_ID"], db.myds.Tables["RI"].Columns["RI_UpID"]);
            PopulateSubTree(dr, 0, newNode);
        }

        private void PopulateSubTree(DataRow dbRow, int _UpID, ComponentArt.Web.UI.TreeViewNode node)
        {
            int len = dbRow.GetChildRows("NodeRelation").Length;

            foreach (DataRow dr in dbRow.GetChildRows("NodeRelation"))
            {
                ComponentArt.Web.UI.TreeViewNode newNode = CreateNode(dr["RI_Name"].ToString(), -1, true, "folder.gif", "", true, false);
                node.Nodes.Add(newNode);
                PopulateTeacher(Convert.ToInt32(dr["Role_ID"].ToString()), newNode);
                PopulateSubTree(dr, Convert.ToInt32(dr["Role_ID"]), newNode);
            }
        }

        private void PopulateTeacher(int _role_id, ComponentArt.Web.UI.TreeViewNode node)
        {
            db.myds.Tables["User_Info"].DefaultView.RowFilter = "Role_ID=" + _role_id.ToString();
            for (int ii = 0; ii < db.myds.Tables["User_Info"].DefaultView.ToTable().Rows.Count; ii++)
            {
                DataRow dr = db.myds.Tables["User_Info"].DefaultView.ToTable().Rows[ii];
                node.Nodes.Add(CreateNode(dr["UI_Name"].ToString(), Convert.ToInt32(dr["User_ID"].ToString()), true, "UI_Online-" + dr["UI_Online"].ToString() + ".gif", "", true, true));
            }
        }

        private ComponentArt.Web.UI.TreeViewNode CreateNode(string name, int id, bool expanded, string imageurl, string navurl, bool showcheck, bool selectable)
        {
            if (!_MTF)
            {
                showcheck = false;
            }

            ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
            node.Text = name;
            node.NavigateUrl = navurl;
            node.Target = "mainBEFrame";
            node.Value = id.ToString();
            node.ImageUrl = imageurl;
            node.Expanded = expanded;
            node.ShowCheckBox = showcheck;
            node.Selectable = selectable;
            return node;
        }


        protected void ButPost_Click(object sender, EventArgs e)
        {
            String strid = "";
            String strname = "";
            CheckTVNodes(ref strid, ref strname, TV_Tree.Nodes);
            try
            {
                strid = strid.Remove(0, 1);
                strname = strname.Remove(0, 1);
            }
            catch (Exception) { }
            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>oMyObject.document.getElementById('" + Request["name"] + "').value='" + strname.ToString() + "';oMyObject.document.getElementById('" + Request["id"] + "').value='" + strid.ToString() + "';window.close();</script>");
        }

        private void CheckTVNodes(ref String strid, ref String strname, ComponentArt.Web.UI.TreeViewNodeCollection nodes)
        {
            foreach (ComponentArt.Web.UI.TreeViewNode node in nodes)
            {
                if ((node.Value != "-1") && ((node.Checked) || (node.IsSelected)))
                {
                    strid += "," + node.Value;
                    strname += "," + node.Text;
                    //return;
                }
                if (nodes.Count > 0)
                {
                    CheckTVNodes(ref strid, ref strname, node.Nodes);
                }
            }
        }
    }
}
