using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HySoft.Common;
using System.Data;

namespace HySoft.BaseCallCenter.Web.sysmanage.ajax
{
    /// <summary>
    /// deptcategory 的摘要说明
    /// </summary>
    public class deptcategory : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "gettreelist":
                    context.Response.Write(LoadTree(context));
                    break;
                case "getZXZlist":
                    context.Response.Write(LoadZXZTree(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteById(CommonRequest.GetQueryString("id")));
                    break;
                case "clear":
                    break;
            }
        }

        #region 获取分类数据

        private string LoadZXZTree(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='ZXZ' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = BindZXZTree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, modelList);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        private string LoadTree(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_Department().GetList(" F_ParentId=0 ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = BindTree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, modelList);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

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

            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_Sys_Department> categorylist = new BLL.T_Sys_Department().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_DeptId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_DeptName;
                    tab2 = new BLL.T_Sys_Department().GetList("F_ParentId=" + currentID + " order by f_sort").Tables[0];
                    if (tab2 != null && tab2.Rows.Count > 0)
                    {
                        model.children = BindTree(tab2, currentID);
                    }
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }

        public List<Model.TreeModel> BindZXZTree(DataTable tab, string parentid)
        {
            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_Sys_DictionaryValue> categorylist = new BLL.T_Sys_DictionaryValue().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_DictionaryValueId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = categorylist[i].F_DictionaryFlag;
                    model.text = categorylist[i].F_Name;
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }

        #endregion

        #region 删除数据

        /// <summary>
        /// 删除操作字典
        /// </summary>
        /// <returns></returns>
        public string DeleteById(string id)
        {
            bool res = false;
            res = new BLL.T_Sys_Department().Delete(Convert.ToInt32(id));
            return res ? "success" : "field";
        }

        #endregion




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}