using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{

    public class DictionaryController : BaseController
    {
        private BLL.T_Sys_DictionaryValue dictionaryValueBLL = new BLL.T_Sys_DictionaryValue();

        public ActionResult GetZTreeList()
        {
            DataTable dt = new DataTable();
            string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
            string sql = " and F_State=0 ";
            if (pid.Trim() != "")
            {
                sql += " and F_PrentId=" + pid.Trim();
            }
            else
                sql += " and F_PrentId=0";

            dt = new BLL.T_Sys_DictionaryValue().GetList(" 1=1 " + sql).Tables[0];
            List<Model.TreeModel> modelList = BindTree(dt, "0");

            if (modelList.Count > 0)
            {
                return Success("加载成功", modelList);
            }
            else
                return Error("加载失败");

        }

        //tree  树形知识库分类
        private List<Model.TreeModel> BindTree(DataTable tab, string parentid)
        {
            DataTable tab2 = new DataTable();

            if (tab != null && tab.Rows.Count > 0)
            {
                List<Model.T_Sys_DictionaryValue> categorylist = new BLL.T_Sys_DictionaryValue().DataTableToList(tab);
                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_ValueId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_Value;
                    tab2 = new BLL.T_Sys_DictionaryValue().GetList(" F_PrentId=" + currentID + " and F_State=0  ").Tables[0];
                    if (tab2 != null && tab2.Rows.Count > 0)
                    {
                        model.children = BindTree(tab2, currentID);
                    }
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }

        #region 字典值操作

        [Authority]
        /// <summary>
        /// 获取字典值列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDicValueList()
        {
            string name = RequestString.GetQueryString("name");
            int id = RequestString.GetInt("id", 0);
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            string sql = "  and F_State=0 ";
            DataTable dt = new DataTable();

            if (id != 0)
            {
                sql += " and F_ItemId= '" + id + "' ";
            }

            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += " and F_Value like '%" + name + "%' ";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

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

            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_DictionaryValue",
                "F_ValueId",
                "*",
                sql,
                "ORDER BY F_ValueId ",
                pagesize,
                pageindex,
                true,
                out recordCount);
            List<Model.T_Sys_DictionaryValue> modelList = new BLL.T_Sys_DictionaryValue().DataTableToList(dt);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取字典值列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDicValueListById()
        {
            int id = RequestString.GetInt("id", 0);
            DataTable dt = new DataTable();
            dt = new BLL.T_Sys_DictionaryValue().GetList(0," F_ItemId='" + id + "' and F_State=0 "," F_Remark asc").Tables[0];
            return Success("列表加载成功", dt);

        }

        /// <summary>
        /// 获取字典值列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDicValueListByParentId()
        {
            int pid = RequestString.GetInt("pid", 0);
            DataTable dt = new DataTable();

            dt = new BLL.T_Sys_DictionaryValue().GetList(" F_PrentId='" + pid + "' and F_State=0  ").Tables[0];

            return Success("列表加载成功", dt);

        }

        /// <summary>
        /// 加载字典值
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDicValue()
        {
            int id = RequestString.GetInt("id", 0);

            Model.T_Sys_DictionaryValue valueModel = dictionaryValueBLL.GetModel(id);
            if (valueModel != null)
                return Success("加载字典值成功", valueModel);
            else
                return Error("加载字典值失败");

        }

        [Authority]
        /// <summary>
        /// 添加/编辑字典值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddDicValue()
        {
            int id = RequestString.GetInt("id", 0);
            //int iid = RequestString.GetInt("iid", 0);
            int pid = RequestString.GetInt("pid", 0);
            string name = RequestString.GetFormString("name");

            Model.T_Sys_DictionaryValue orderModel = new Model.T_Sys_DictionaryValue();
            BLL.T_Sys_DictionaryValue orderBll = new BLL.T_Sys_DictionaryValue();

            if (id == 0)
            {
                var list = orderBll.GetModelList(" F_Value='" + name + "' and F_ItemId='" + pid + "' ");
                if (list.Count > 0)
                {
                    return Error("此类别下已经存在此字典值");
                }
                else
                {
                    orderModel.F_ItemId = pid;
                    orderModel.F_Value = name;
                    orderModel.F_PrentId = pid;
                    orderModel.F_State = 0;

                    int n = orderBll.Add(orderModel);
                    if (n > 0)
                        return Success("字典值添加成功", n);
                    else
                        return Error("字典值添加失败");
                }
            }
            else
            {
                orderModel = orderBll.GetModel(id);
                if (orderModel != null)
                {
                    var list = orderBll.GetModelList(" F_Value='" + name + "' and F_ItemId='" + pid + "' and  F_ValueId!='" + id + "'");
                    if (list.Count > 0)
                    {
                        return Error("此类别下已经存在此字典值");
                    }
                    else
                    {
                        orderModel.F_ItemId = pid;
                        orderModel.F_Value = name;
                        orderModel.F_PrentId = pid;

                        if (orderBll.Update(orderModel))
                            return Success("字典值修改成功");
                        else
                            return Error("字典值修改失败");
                    }
                }
                else
                {
                    return Error("字典值修改失败");
                }
            }

        }

        [Authority]
        /// <summary>
        /// 删除字典值
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDicValue(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("获取参数失败");
            var idStr = string.Join(",", ids);
            if (new BLL.T_Sys_DictionaryValue().DeleteList1(idStr))
                return Success("删除成功");
            else
                return Error("删除失败");

        }
        #endregion
    }
}