using CallCenter.Utility;
using CallCenterApi.Common;
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();
        private BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
        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 + "' ";
                sql += " and F_PrentId= '" + 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());

        }
        //[Authority]
        /// <summary>
        /// 获取字典值列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDicValueListById(int type=0)
        {
            //int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            //Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            int id = RequestString.GetInt("id", 0);
            var sql = " F_State=0 ";
            if (id == 1)
                sql += " and  (F_PrentId=36 or F_ItemId='" + id + "')";
            else if (id == 2)
                sql += " and  (F_PrentId=37 or F_ItemId='" + id + "')";
            else if (id == 3)
            {
                //if (type >0)
                //{
                //    sql += " and  (F_PrentId=38 or F_ItemId='" + id + "')";
                //}
                //else
                //{
                //    if (userModel.F_RoleCode == "DFZF")
                //        sql += " and  (F_PrentId=38 or F_ItemId='" + id + "')  and F_Deptid='" + userModel.F_DeptId + "'";
                //    else
                //        sql += " and  (F_PrentId=38 or F_ItemId='" + id + "')";
                //}
                sql += " and  (F_PrentId=38 or F_ItemId='" + id + "')";
            }
               
            else
                sql += " and  (F_PrentId='" + id + "' or F_ItemId='" + id + "')";
            DataTable dt = new DataTable();
            //dt = new BLL.T_Sys_DictionaryValue().GetList(" F_ItemId='" + id + "' and F_State=0 ").Tables[0];
            dt = new BLL.T_Sys_DictionaryValue().GetList(sql).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)
            {
                string deptname="";
                 if (valueModel.F_Deptid!=null )
                {
                    var dept = new BLL.T_Sys_Department().GetModel((int )valueModel.F_Deptid);
                    if (dept != null)
                        deptname = dept.F_DeptName;
                }
                var obj = new
                {
                    F_CreateDate=  valueModel.F_CreateDate ,
                    F_CreateUser = valueModel.F_CreateUser,
                    F_IsLeaf = valueModel.F_IsLeaf,
                    F_ItemId = valueModel.F_ItemId,
                    F_Layer = valueModel.F_Layer,
                    F_PrentId = valueModel.F_PrentId,
                    F_Remark = valueModel.F_Remark,
                    F_State = valueModel.F_State,
                    F_Value = valueModel.F_Value,
                    F_ValueId = valueModel.F_ValueId,
                    F_Deptid = valueModel.F_Deptid,
                    F_DeptName= deptname
                };
                return Success("加载字典值成功", obj);
            }
               
            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");
            int deptid = RequestString.GetInt("deptid", 0);
            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+"' ");
                var list = orderBll.GetModelList(" F_Value='" + name + "' and F_PrentId='" + pid + "' and F_State=0 ");
                if (list.Count > 0)
                {
                    return Error("此类别下已经存在此字典值");
                }
                else
                {
                    orderModel.F_ItemId = pid;
                    orderModel.F_Value = name;
                    orderModel.F_PrentId = pid;
                    orderModel.F_State = 0;
                    orderModel.F_Deptid  = deptid;
                    
                    int n = orderBll.Add(orderModel);
                    if (n > 0)
                    {
                        if (deptid > 0)
                        {
                            var dept = new BLL.T_Sys_Department().GetModel(deptid);
                            if (dept != null )
                            {
                                dept.F_Valueid = n;                     }
                            var b = new BLL.T_Sys_Department().Update(dept);
                            }
                        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 + "'");
                    var list = orderBll.GetModelList(" F_Value='" + name + "' and F_PrentId='" + pid + "' and  F_ValueId!='" + id + "' and F_State=0 ");
                    if (list.Count > 0)
                    {
                        return Error("此类别下已经存在此字典值");
                    }
                    else
                    {
                        orderModel.F_ItemId = pid;
                        orderModel.F_Value = name;
                        orderModel.F_PrentId = pid;
                        orderModel.F_Deptid = deptid;
                        if (orderBll.Update(orderModel))
                        {
                            if (deptid > 0)
                            {
                                var dept = new BLL.T_Sys_Department().GetModel(deptid);
                                if (dept != null)
                                {
                                    dept.F_Valueid = orderModel.F_ValueId ;
                                }
                                var b = new BLL.T_Sys_Department().Update(dept);
                            }
                            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
    }
}