using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Common;
using System.Web.Services;

namespace HySoft.BaseCallCenter.Web.sysmanage
{
    public partial class rolelist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
            }
        }
        [WebMethod]
        public static string GetID(string id)
        {
            string F_FunctionId = "";
            string F_ModuleId = "";
            try
            {
                DataTable dt1 = DBUtility.DbHelperSQL.Query("select F_FunctionId from T_Sys_RoleFunction where F_RoleId='" + id + "'").Tables[0];
                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        F_FunctionId += dt1.Rows[i]["F_FunctionId"].ToString() + ",";
                    }
                }
                DataTable dt2 = DBUtility.DbHelperSQL.Query("select F_Id from T_Sys_RoleOpt where F_RoleId='" + id + "'").Tables[0];
                if (dt2.Rows.Count > 0)
                {
                    for (int i = 0; i < dt2.Rows.Count; i++)
                    {
                        F_ModuleId += dt2.Rows[i]["F_Id"].ToString() + "s,";
                    }
                }
                return F_FunctionId + "@" + F_ModuleId;
            }
            catch (Exception e) { return e.ToString(); }
        }
        [WebMethod]
        public static string GetChildrenID(string id, string roleid, string pandaunClick, string F_FunctionId, string F_ModuleId)
        {
            string JieDianF_FunctionId = id + ",";
            string JieDianF_ModuleId = "";
            try
            {
                if (roleid == "0")
                {
                    DataTable dt11 = DBUtility.DbHelperSQL.Query("select F_RoleId from T_Sys_RoleInfo order by F_RoleId desc").Tables[0];
                    if (dt11.Rows.Count > 0)
                    {
                        roleid = dt11.Rows[0]["F_RoleId"].ToString();
                    }
                }
                //DataTable dt1 = DBUtility.DbHelperSQL.Query("select F_FunctionId from T_Sys_RoleFunction where F_RoleId='" + roleid + "' and F_FunctionId='" + id + "'").Tables[0];
                //if (dt1.Rows.Count > 0)
                //{
                //    panduan = "true";
                //}
                //else 
                //{
                //    panduan = "false";
                //}

                if (pandaunClick == "true")
                {
                    System.Collections.Generic.List<Model.TreeModel> modelList = new System.Collections.Generic.List<Model.TreeModel>();
                    F_FunctionId = F_FunctionId.Replace(id + ",", "");
                    DataTable dt = new BLL.T_Sys_ModuleFunctions().GetList("F_ParentId=" + id + " and F_StateFlag=1 order by F_Sort").Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        modelList = BindTree(dt, "0");
                    }
                    else
                    {
                        DataTable dtOpt = new BLL.T_Sys_ModuleFunctionsOpt().GetList("F_FunctionId=" + id + "  order by F_Id").Tables[0];
                        modelList = BindTree(dtOpt, "1");
                    }
                    if (modelList != null)
                    {
                        if (F_FunctionId.Length > 0)
                        {
                            F_FunctionId = "," + F_FunctionId;
                        }
                        if (F_ModuleId.Length > 0)
                        {
                            F_ModuleId = "," + F_ModuleId;
                        }
                        foreach (Model.TreeModel model in modelList)
                        {
                            if (model.IconCls == "1")
                            {
                                JieDianF_FunctionId += model.id + ",";
                                F_FunctionId = F_FunctionId.Replace("," + model.id + ",", ",");
                            }
                            else
                            {
                                JieDianF_ModuleId += model.id + ",";
                                F_ModuleId = F_ModuleId.Replace("," + model.id + ",", ",");
                            }
                        }
                        if (F_FunctionId.Length > 0)
                        {
                            F_FunctionId = F_FunctionId.Substring(1, F_FunctionId.Length - 1);
                        }
                        if (F_ModuleId.Length > 0)
                        {
                            F_ModuleId = F_ModuleId.Substring(1, F_ModuleId.Length - 1);
                        }
                    }
                }
                else
                {
                    System.Collections.Generic.List<Model.TreeModel> modelList = new System.Collections.Generic.List<Model.TreeModel>();
                    F_FunctionId += id + ",";
                    DataTable dt = new BLL.T_Sys_ModuleFunctions().GetList("F_ParentId=" + id + " and F_StateFlag=1 order by F_Sort").Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        modelList = BindTree(dt, "0");
                    }
                    else
                    {
                        DataTable dtOpt = new BLL.T_Sys_ModuleFunctionsOpt().GetList("F_FunctionId=" + id + "  order by F_Id").Tables[0];
                        modelList = BindTree(dtOpt, "1");
                    }
                    if (modelList != null)
                    {
                        F_FunctionId = "," + F_FunctionId;
                        F_ModuleId = "," + F_ModuleId;
                        foreach (Model.TreeModel model in modelList)
                        {
                            if (model.IconCls == "1")
                            {
                                JieDianF_FunctionId += model.id + ",";
                                if (F_FunctionId.IndexOf("," + model.id + ",") == -1)
                                {
                                    F_FunctionId += model.id + ",";
                                }
                            }
                            else
                            {
                                JieDianF_ModuleId += model.id + ",";
                                if (F_ModuleId.IndexOf("," + model.id + ",") == -1)
                                {
                                    F_ModuleId += model.id + ",";
                                }
                            }
                        }
                        if (F_FunctionId.Length > 0)
                        {
                            F_FunctionId = F_FunctionId.Substring(1, F_FunctionId.Length - 1);
                        }
                        if (F_ModuleId.Length > 0)
                        {
                            F_ModuleId = F_ModuleId.Substring(1, F_ModuleId.Length - 1);
                        }
                    }
                }

                return F_FunctionId + "@" + F_ModuleId + "@" + pandaunClick + "@" + JieDianF_FunctionId + "@" + JieDianF_ModuleId;
            }
            catch (Exception e) { return e.ToString(); }
        }

        public static List<Model.TreeModel> BindTree(DataTable tab, string parentid)
        {
            DataTable tab2 = new DataTable();

            if (tab != null && tab.Rows.Count > 0)
            {

                if (parentid == "0")
                {
                    System.Collections.Generic.List<Model.T_Sys_ModuleFunctions> categorylist = new BLL.T_Sys_ModuleFunctions().DataTableToList(tab);
                    System.Collections.Generic.List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                    for (int i = 0; i < categorylist.Count; i++)
                    {
                        Model.TreeModel model = new Model.TreeModel();
                        string currentID = categorylist[i].F_FunctionId.ToString();//当前功能ID
                        model.id = currentID;
                        model.IconCls = "1";//图标
                        modelList.Add(model);
                        //model.text = categorylist[i].F_Name;
                        tab2 = new BLL.T_Sys_ModuleFunctions().GetList("F_ParentId=" + currentID + " and F_StateFlag=1  order by F_Sort").Tables[0];
                        if (tab2 != null && tab2.Rows.Count > 0)
                        {
                            // BindTree(tab2, "0");
                            foreach (Model.TreeModel modeltree in BindTree(tab2, "0"))
                            {
                                modelList.Add(modeltree);
                            }
                        }
                        tab2 = new BLL.T_Sys_ModuleFunctionsOpt().GetList("F_FunctionId=" + currentID + " order by F_Id").Tables[0];
                        if (tab2 != null && tab2.Rows.Count > 0)
                        {
                            //BindTree(tab2, "1");
                            foreach (Model.TreeModel modeltree in BindTree(tab2, "1"))
                            {
                                modelList.Add(modeltree);
                            }
                        }

                    }
                    return modelList;
                }
                else
                {
                    System.Collections.Generic.List<Model.T_Sys_ModuleFunctionsOpt> categorylist1 = new BLL.T_Sys_ModuleFunctionsOpt().DataTableToList(tab);
                    System.Collections.Generic.List<Model.TreeModel> modelList1 = new List<Model.TreeModel>(categorylist1.Count);
                    for (int i = 0; i < categorylist1.Count; i++)
                    {
                        Model.TreeModel model = new Model.TreeModel();
                        string currentID = categorylist1[i].F_Id.ToString();//当前功能ID
                        model.id = currentID + "s";
                        model.IconCls = "4";//图标
                        modelList1.Add(model);
                    }
                    return modelList1;
                }

            }
            else
            {
                return null;
            }

        }

        private void getData()
        {
            try
            {
                DataTable dt = DBUtility.DbHelperSQL.Query("select F_RoleId from T_Sys_RoleInfo order by F_RoleId desc").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    string F_RoleId = dt.Rows[0]["F_RoleId"].ToString();
                    DataTable dt1 = DBUtility.DbHelperSQL.Query("select F_FunctionId from T_Sys_RoleFunction where F_RoleId='" + F_RoleId + "'").Tables[0];
                    if (dt1.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt1.Rows.Count; i++)
                        {
                            hdF_FunctionId.Value += dt1.Rows[i]["F_FunctionId"].ToString() + ",";
                        }
                    }
                    DataTable dt2 = DBUtility.DbHelperSQL.Query("select F_Id from T_Sys_RoleOpt where F_RoleId='" + F_RoleId + "'").Tables[0];
                    if (dt2.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt2.Rows.Count; i++)
                        {
                            hdF_ModuleId.Value += dt2.Rows[i]["F_Id"].ToString() + "s,";
                        }
                    }
                }
            }
            catch { }
        }
    }
}