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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getlist":
                    context.Response.Write(LoadList(context));
                    break;
                case "getrootlist":
                    context.Response.Write(LoadRootList(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteById(context));
                    break;
                case "gettreelist":
                    context.Response.Write(LoadTree(context));
                    break;
            }
        }

        #region 绑定树形分类

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

            try
            {

                dt = new BLL.T_QC_IndexCategory().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_QC_IndexCategory> categorylist = new BLL.T_QC_IndexCategory().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_CategoryId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_CategoryName;
                    tab2 = new BLL.T_QC_IndexCategory().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;
            }

        }

        #endregion

        #region 删除分类

        private string DeleteById(HttpContext context)
        {
            int res = 0;
            string str = "error";

            DataTable dt = new DataTable();
            try
            {
                dt = new BLL.T_QC_IndexBase().GetList(" F_CategoryId=" + context.Request.Params["id"] + " ").Tables[0];
                res = dt.Rows.Count;
                if (res > 0)
                {
                    str = res.ToString();
                }
                else
                {
                    if (new BLL.T_QC_IndexCategory().Delete(Convert.ToInt32(context.Request.Params["id"])))
                    {
                        str = "success";
                    }
                    else
                    {
                        str = "error";
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return str;
        }

        #endregion

        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

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


                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_QC_IndexCategory> pageModel = new Model.PageData<Model.T_QC_IndexCategory>();

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

                System.Collections.Generic.List<Model.T_QC_IndexCategory> modelList = new BLL.T_QC_IndexCategory().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_QC_IndexCategory>));

                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;
        }
        #endregion

        #region 获取数据
        private string LoadRootList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                List<Model.T_QC_IndexCategory> pageModel = new List<Model.T_QC_IndexCategory>();

                dt = new BLL.T_QC_IndexCategory().GetList(" F_DeleteFlag=0 and F_ParentId=0 order by F_Sort ").Tables[0];

                if (dt != null)
                {
                    System.Collections.Generic.List<Model.T_QC_IndexCategory> modelList = new BLL.T_QC_IndexCategory().DataTableToList(dt);
                    pageModel = modelList;
                    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.T_QC_IndexCategory>));

                    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;
        }
        #endregion

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