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>
    /// dictionarymanage 的摘要说明
    /// </summary>
    public class dictionarymanage : 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 "getlist":
                    context.Response.Write(LoadList(context));
                    break;
                case "deletelist":
                    context.Response.Write(Delete(context));
                    break;
                case "deletetree":
                    context.Response.Write(DeleteById(CommonRequest.GetQueryString("id")));
                    break;
                case "clear":
                    break;
            }
        }
        #region 删除数据
        private string Delete(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Sys_DictionaryValue().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Sys_DictionaryValue().DeleteList(arr);
            }


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

        #endregion
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                string flag = HttpUtility.UrlDecode(CommonRequest.GetQueryString("flag"));
                string name = HttpUtility.UrlDecode(CommonRequest.GetQueryString("name"));
                string id = HttpUtility.UrlDecode(CommonRequest.GetQueryString("id"));

                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;

                if (id.Trim() != "" && id != "undefined")
                {
                    sql += " and F_DictionaryFlag= '" + id.Trim() + "' ";
                }

                if (flag.Trim() != "")
                {
                    sql += " and F_DictionaryFlag like '%" + flag.Trim() + "%' ";
                }
                if (name.Trim() != "")
                {
                    sql += " and F_Name like '%" + name.Trim() + "%' ";
                }

                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }

                int recordCount = 0;

                Model.PageData<Model.T_Sys_DictionaryValue> pageModel = new Model.PageData<Model.T_Sys_DictionaryValue>();

                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_DictionaryValue",
                    "F_DictionaryValueId",
                    "*",
                    sql,
                    "ORDER BY F_Sort ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Sys_DictionaryValue> modelList = new BLL.T_Sys_DictionaryValue().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_Sys_DictionaryValue>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    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_DictionaryBase().GetList(" ").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;
        }
             /// <summary>
             /// 递归获取树形结构
             /// </summary>
             /// <param name="tab">数据源</param>
             /// <param name="parentid">父节点ID</param>
             /// <returns></returns>
             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_DictionaryBase> categorylist = new BLL.T_Sys_DictionaryBase().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_DictionaryFlag.ToString();//当前功能ID
                         model.id = currentID;
                         model.IconCls = "";//图标
                         model.text = categorylist[i].F_DictionaryName;
                         modelList.Add(model);
                     }
                     return modelList;

                 }
                 else
                 {
                     return null;
                 }

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