using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using EKETEAM.Data;
using EKETEAM.FrameWork;

namespace eFrameWork.Plugins
{
    public partial class UserSelect : System.Web.UI.Page
    {        
        public string obj = eParameters.QueryString("obj");
        public string area = eParameters.QueryString("area");
        bool change = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            eTable etb = new eTable("Organizationals");
            change = etb.ColumnCollection.ContainsKey("change");
            litBody.Text = getTree("");
        }
        private string getTree(string ParentID)
        {
            string siteid = eBase.getSiteID();// eConfig.SiteID();
            string comcode = "";
            string usertype = "";
            if (area.Length > 0)
            {
                eUser user = new eUser(area);
                if (user.Logined)
                {
                    if (user["siteid"].ToString().Length > 0) siteid = user["siteid"].ToString();
                    if (user["comcode"].ToString().Length > 0) comcode = user["comcode"].ToString();
                    if (user["usertype"].ToString().Length > 0) usertype = user["usertype"].ToString();
                }
            }
            StringBuilder sb = new StringBuilder();
            //eBase.Writeln(comcode);

           // Change
            string sql = "select OrganizationalID,ParentID,MC,Code from Organizationals where DelTag=0";
            //sql += (ParentID.Length == 0 ? " and ParentID IS NULL" : " and ParentID='" + ParentID + "'");
            if (ParentID.Length == 0)
            {
                if (comcode.Length > 0 && usertype != "1")
                {
                    sql += " and code ='" + comcode + "'"; // and IsCorp=1
                }
                else
                {
                    sql += " and ParentID IS NULL"; // and IsCorp=1
                }
            }
            else
            {
                sql += " and ParentID='" + ParentID + "'"; 
            }

            if (change) sql += " and change=0";

            sql += " and " + (siteid.Length > 0 ? "(SiteID=0 or SiteID='" + siteid + "')" : "SiteID=0");
            sql += " and show=1";
            sql += " Order by IsCorp,PX,addTime";
            DataTable tb = eBase.DataBase.getDataTable(sql);
            //eBase.Writeln(sql);
            //eBase.PrintDataTable(tb);

            if (ParentID.Length == 0)
            {
                sb.Append("<ul id=\"etree\" class=\"etree\" PID=\"NULL\">\r\n");
            }
            else
            {
                sb.Append("<ul PID=\"" + ParentID + "\">\r\n");
            }
            foreach (DataRow dr in tb.Rows)
            {
                #region 循环部门
                sql = "select count(*) from  Organizationals where SiteID='" + siteid + "' and DelTag=0 and ParentID='" + dr["OrganizationalID"].ToString() + "'";
                if (change) sql += " and change=0";
                string ct = eBase.DataBase.getValue(sql);
                //sql = "select count(1) from a_eke_sysUsers where code like '" + dr["code"].ToString() + "%' and len(xm)>0 and Active=1 and deltag=0";
                sql = "select count(1) from a_eke_sysUsers where code ='" + dr["code"].ToString() + "' and len(xm)>0 and Active=1 and deltag=0";
                string userct = eBase.UserInfoDB.getValue(sql);
                if (ct == "0" && userct == "0") continue;
                //if (userct == "0") continue;//


                sb.Append("<li dataid=\"" + dr["OrganizationalID"].ToString() + "\" ");// oncontextmenu=\"return false;\"
                sb.Append(" class=\"\">");
                sb.Append("<div>");// oncontextmenu=\"return false;\"
                sb.Append("<a href=\"javascript:;\" onclick=\"setUsers(this)\" oncontextmenu=\"return false;\">");
                sb.Append(dr["MC"].ToString() );
                sb.Append( "</a>"); // (" + ct + ")
                sb.Append("</div>");
                if (userct != "0")
                { 
                    //部门
                    sql = "select UserID,XM from a_eke_sysUsers where code='" + dr["code"].ToString() + "' and len(xm)>0 and Active=1 and deltag=0 order by PostLevel,addtime";
                    DataTable dt = eBase.UserInfoDB.getDataTable(sql);
                    if (dt.Rows.Count > 0)
                    {
                        sb.Append("<ul PID=\"" + dr["OrganizationalID"].ToString() + "\">\r\n");
                        foreach (DataRow _dr in dt.Rows)
                        {
                            sb.Append("<li userid=\"" + _dr["UserID"].ToString() + "\" name=\"" + _dr["xm"].ToString() + "\" dataid=\"\"");// oncontextmenu=\"return false;\"
                            sb.Append(" class=\"user" + (_dr["xm"].ToString().IndexOf("DD") > -1 ? " userselected" : "") + "\" onclick=\"setUser('" + _dr["UserID"].ToString() + "','" + _dr["XM"].ToString() + "');\">");
                            sb.Append("<div>");// oncontextmenu=\"return false;\"
                            //sb.Append("<a href=\"javascript:;\" oncontextmenu=\"return false;\">");
                            sb.Append( _dr["xm"].ToString());
                            //sb.Append("</a>"); // (" + ct + ")
                            sb.Append("</div>");
                            sb.Append("</li>\r\n");
                        }
                        sb.Append("</ul>\r\n");
                    }
                }
                if (ct != "0") sb.Append(getTree(dr["OrganizationalID"].ToString()));
                sb.Append("</li>\r\n");
                #endregion
            }
            if (ParentID.Length == 0 && comcode.Length==0)
            {

                sql = "select UserID,XM from a_eke_sysUsers where code is null and len(xm)>0 and Active=1 and deltag=0 order by PostLevel,addtime";
                DataTable dt = eBase.UserInfoDB.getDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    sb.Append("<li dataid=\"0\" ");// oncontextmenu=\"return false;\"
                    sb.Append(" class=\"\">");
                    sb.Append("<div>");// oncontextmenu=\"return false;\"
                    sb.Append("<a href=\"javascript:;\" onclick=\"setUsers(this)\" oncontextmenu=\"return false;\">");
                    //sb.Append("未设置部门");
                    sb.Append("部门不详");
                    sb.Append("</a>"); // (" + ct + ")
                    sb.Append("</div>");
                    if (dt.Rows.Count > 0)
                    {
                        sb.Append("<ul PID=\"0\">\r\n");
                        foreach (DataRow _dr in dt.Rows)
                        {
                            sb.Append("<li userid=\"" + _dr["UserID"].ToString() + "\" name=\"" + _dr["xm"].ToString() + "\" dataid=\"\"");// oncontextmenu=\"return false;\"
                            sb.Append(" class=\"user" + (_dr["xm"].ToString().IndexOf("DD") > -1 ? " userselected" : "") + "\" onclick=\"setUser('" + _dr["UserID"].ToString() + "','" + _dr["XM"].ToString() + "');\">");
                            sb.Append("<div>");// oncontextmenu=\"return false;\"
                            //sb.Append("<a href=\"javascript:;\" oncontextmenu=\"return false;\">");
                            sb.Append(_dr["xm"].ToString());
                            //sb.Append("</a>"); // (" + ct + ")
                            sb.Append("</div>");
                            sb.Append("</li>\r\n");
                        }
                        sb.Append("</ul>\r\n");
                    }
                    sb.Append("</li>\r\n");
                }
            }
            sb.Append("</ul>\r\n");
            return sb.ToString();
        }
    }
}