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 UserSelectN : System.Web.UI.Page
    {        
        public string obj = eParameters.QueryString("obj");
        private string area = eParameters.QueryString("area");
        bool change = false;
        public string multi = eParameters.QueryString("multi");
        private DataTable _organizationals;
        private DataTable Organizationals
        {
            get
            {
                if (_organizationals == null)
                {
                    string CacheName = "DataCache_Tables_Organizationals";
                    _organizationals = (DataTable)HttpRuntime.Cache.Get(CacheName);
                    if (_organizationals == null)
                    {
                        _organizationals = eBase.DataBase.getDataTable("SELECT * FROM Organizationals where delTag=0");
                        HttpRuntime.Cache.Insert(CacheName, _organizationals);;
                    }
                }
                return _organizationals;
            }
        }
        string siteid = "1";
        int showLevel = 2;//显示两层
        string baseurl = "";
        string curcode = eParameters.Request("code");
        List<string> allIds = new List<string>();
        public bool mobile = eBase.IsMobile();
        protected void Page_Load(object sender, EventArgs e)
        {
            eUser user = new eUser(area);
            user.Check();

            eTable etb = new eTable("Organizationals");
            change = etb.ColumnCollection.ContainsKey("change");
            siteid = eBase.getSiteID();

            baseurl = Request.Url.PathAndQuery;
            baseurl = baseurl.removeQuery("t");
            baseurl = baseurl.removeQuery("page");
            baseurl = baseurl.removeQuery("s1");
            baseurl = baseurl.removeQuery("s2");
            baseurl = baseurl.removeQuery("code");

            getParentIDS();
            //eBase.WriteDebugLine(string.Join(",", allIds));

            litBody.Text = getUsers();
            LitMenu.Text = getMenu();
        }
        private void getParentIDS(string code="")
        {
            if (code.Length == 0) code = curcode;
            if (code.Length == 0) return;

            DataRow[] rows = Organizationals.Select("Code='" + code + "'");
            if (rows.Length == 0) return;
            allIds.Add(rows[0]["OrganizationalID"].ToString().ToLower());
            string ParentCode = rows[0]["ParentCode"].ToString();
            if (ParentCode.Length > 0)
            {
                getParentIDS(ParentCode);
            }
        }
        private string getMenu(string ParentID = "",int level = 1)
        {          
            string cond = "siteid=" + siteid + " " + (ParentID.Length == 0 ? " and ParentID is null" : " and ParentID='" + ParentID + "'");
            if (Organizationals.Columns.Contains("change")) cond += " and change=0";

            DataRow[] rows = Organizationals.Select(cond, "px,addtime");
            if (rows.Length == 0) return "";
            StringBuilder sb = new StringBuilder();
            if (ParentID.Length == 0)
            {
                sb.Append("<ul id=\"etree\" class=\"etree\" PID=\"NULL\">\r\n");
            }
            else
            {
                sb.Append("<ul PID=\"" + ParentID + "\"");
                if (level > showLevel && !allIds.Contains(ParentID)) sb.Append(" style=\"display:none;\"");
                sb.Append(">\r\n");
            }
            foreach (DataRow dr in rows)
            {
                DataRow[] sub = Organizationals.Select("siteid=" + siteid + " and ParentID='" + dr["OrganizationalID"].ToString() + "'");

                sb.Append("<li dataid=\"" + dr["OrganizationalID"].ToString() + "\" ");
                sb.Append(" class=\"" + (level > (showLevel - 1) && sub.Length > 0 &&  !allIds.Contains(dr["OrganizationalID"].ToString())  ? "close" : "") + "\">");
                sb.Append("<div>");
                sb.Append("<a href=\""+ baseurl + "&code=" + dr["code"].ToString() +"\" oncontextmenu=\"return false;\">");
                sb.Append(dr["MC"].ToString() );
                sb.Append("</a>");
                sb.Append("</div>");
                if(sub.Length>0) sb.Append(getMenu(dr["OrganizationalID"].ToString(), level + 1));
                sb.Append("</li>");

            }
            if (ParentID.Length == 0)
            {
                sb.Append("<li dataid=\"0\" ");
                sb.Append(" class=\"" + "\">");
                sb.Append("<div>");
                sb.Append("<a href=\"" + baseurl + "&code=0\" oncontextmenu=\"return false;\">部门不详</a>");
                sb.Append("</div>");
                sb.Append("</li>");
                /*
                sb.Append("<li dataid=\"0\" ");
                sb.Append(" class=\"" + "\">");
                sb.Append("<div>");
                sb.Append("<a href=\"" + baseurl + "&code=0\" oncontextmenu=\"return false;\">最近联系人</a>");
                sb.Append("</div>");
                sb.Append("</li>");
                */
            }
           

            sb.Append("</ul>\r\n");
            return sb.ToString();
        }
        private string getUsers()
        {
            StringBuilder sb = new StringBuilder();
            string sql = "select UserID,XM from a_eke_sysUsers where len(xm)>0 and Active=1 and deltag=0";
            string s1 = eParameters.Request("s1");
            if (s1.Length > 0) sql += " and XM like '%" + s1 + "%'";
            string s2 = eParameters.Request("s2");
            if (s2.Length > 0) sql += " and XMEng like '%" + s2 + "%'";
            string code = eParameters.Request("code");
            if (code.Length > 0)
            {
                sql += code == "0" ? " and len(isnull(OrgCode,''))=0" : " and OrgCode like '" + code + "%'";
            }
            sql += " order by PostLevel,addtime";
            DataTable dt = eBase.UserInfoDB.getDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                sb.Append("<div class=\"users\">");
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append("<label userid=\"" + dr["UserID"].ToString() + "\" class=\"item" + (multi == "false" ? " single" : " multi") + "\" onclick=\"setUser(this,'" + dr["userid"].ToString() + "','" + dr["xm"].ToString() + "');\">" + dr["xm"].ToString() + "</label>");
                }
                sb.Append("</div>");
            }
            else
            {
                sb.Append("<div style=\"margin:20px 0 0 30px;color:#666;\">暂无相关信息!</div>");
            }
            return sb.ToString();
        }
    }
}