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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getservicedepttreelist":
                    context.Response.Write(LoadServerDeptTree(context));
                    break;
                case "getregionlist":
                    context.Response.Write(LoadRegionList(context));
                    break;
                case "getdictionarylist":
                    context.Response.Write(LoadDictionaryList(context));
                    break;
                case "getaftersalelist":
                    context.Response.Write(LoadAfterSaleList(context));
                    break;
                case "add":
                    //context.Response.Write(LoadCustomerList(context));
                    break;
                case "edit":
                    //context.Response.Write(LoadCustomerList(context));
                    break;
                case "clear":
                    break;
            }
        }
        private string LoadServerDeptTree(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;
        }
        private string LoadRegionList(HttpContext context)
        {
            string res = "";
            try
            {
                List<Model.T_RegionCategory> pageModel = new List<Model.T_RegionCategory>();

                if (context.Request.Params["regionpid"] != null)
                    pageModel = new BLL.T_RegionCategory().GetModelList("F_ParentId=" + context.Request.Params["regionpid"]);
                else
                    pageModel = new BLL.T_RegionCategory().GetModelList("F_ParentId=0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = 
                    new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.T_RegionCategory>));
                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
            {

            }
            return res;
        }
        private string LoadRegionTree()
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

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

                System.Collections.Generic.List<Model.TreeModel> modelList = BindRegionTree(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>
        private 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;
            }

        }
        private List<Model.TreeModel> BindRegionTree(DataTable tab, string parentid)
        {
            DataTable tab2 = new DataTable();

            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_RegionCategory> categorylist = new BLL.T_RegionCategory().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_RegionId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_RegionName;
                    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;
            }

        }
        private string LoadDictionaryList(HttpContext context)
        {
            string res = "";
            try
            {
                List<Model.T_Sys_DictionaryValue> pageModel = new List<Model.T_Sys_DictionaryValue>();
                if (context.Request.Params["param"] != null)
                    pageModel = new BLL.T_Sys_DictionaryValue().GetModelList("F_DictionaryFlag='" + context.Request.Params["param"] + "'");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<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
            {
                
            }
            return res;
        }
        private string LoadAfterSaleList(HttpContext context)
        {
            string res = "";
            try
            {
                List<Model.T_Sys_UserAccount> pageModel = new List<Model.T_Sys_UserAccount>();
                //if (context.Request.Params["param"] != null)
                    pageModel = new BLL.T_Sys_UserAccount().GetModelList("");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.T_Sys_UserAccount>));

                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
            {

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