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

namespace CallCenterApi.Interface.Controllers.knowledge
{
    [Authority]
    public class KnowledgeClassController : BaseController
    {
        // 获取知识库分类列表
        public ActionResult GetAllList()
        {
            DataTable dt = new DataTable();
            string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
            string sql = "";

            dt = new BLL.T_RepositoryCategory().GetList(sql).Tables[0];

            return Success("加载成功", dt);
        }
        // 获取知识库分类列表
        public ActionResult GetList()
        {
            DataTable dt = new DataTable();
            string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
            string sql = "";
            if (pid.Trim() != "")
            {
                sql += " and F_ParentId=" + pid.Trim();
            }
            else
                sql += " and F_ParentId=0";

            dt = new BLL.T_RepositoryCategory().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_RepositoryCategory> categorylist = new BLL.T_RepositoryCategory().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_CategoryId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_CategoryName;
                    tab2 = new BLL.T_RepositoryCategory().GetList(" F_ParentId=" + currentID + " ").Tables[0];
                    if (tab2 != null && tab2.Rows.Count > 0)
                    {
                        model.children = BindTree(tab2, currentID);
                    }
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }

        //获取知识库分类
        public ActionResult GetClass(string categoryid)
        {
            if (categoryid != null && categoryid.Trim() != "")
            {
                BLL.T_RepositoryCategory dBLL = new BLL.T_RepositoryCategory();
                Model.T_RepositoryCategory dModel = dBLL.GetModel(int.Parse(categoryid.Trim()));
                if (dModel != null)
                {
                    return Success("获取知识库分类信息成功", dModel);
                }
                else
                {
                    return Error("获取知识库分类信息失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }
        //[Authority]
        //添加知识库分类
        public ActionResult AddClass(string cname, string pid)
        {
            Model.T_RepositoryCategory dModel = new Model.T_RepositoryCategory();
            if (pid != "")
            {
                dModel.F_ParentId = int.Parse(pid.Trim());
            }
            else
                dModel.F_ParentId = 0;

            dModel.F_CategoryName = cname.Trim();

            dModel.F_CreateOn = DateTime.Now;
            dModel.F_CreateBy = int.Parse(User.UserData["F_UserID"]);

            int b = new BLL.T_RepositoryCategory().Add(dModel);
            if (b > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }

        }
        //[Authority]
        //编辑知识库分类
        public ActionResult EditClass(string cid, string cname, string pid)
        {
            if (cid != null && cid.Trim() != "")
            {
                BLL.T_RepositoryCategory dBLL = new BLL.T_RepositoryCategory();
                Model.T_RepositoryCategory dModel = dBLL.GetModel(int.Parse(cid.Trim()));
                if (dModel != null)
                {
                    if (pid.Trim() != "")
                    {
                        dModel.F_ParentId = int.Parse(pid.Trim());
                    }

                    if (cname.Trim() != "")
                    {
                        dModel.F_CategoryName = cname.Trim();
                    }
                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
            }
            else
            {
                return Error("请选择要编辑的知识库分类");
            }

        }
        //[Authority]
        //删除知识库分类
        public ActionResult DelClass(string ids)
        {
            if (ids != null && ids.Length > 0)
            {
                DataTable dt = new BLL.T_RepositoryInformation().GetList("F_CategoryId=" + Convert.ToInt32(ids)).Tables[0];
                string rid = string.Empty;
                if (dt.Rows.Count > 0)
                {
                    //先删除知识库信息再删除知识库分类
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        rid = dt.Rows[i]["F_RepositoryId"].ToString();
                        if (!string.IsNullOrEmpty(rid))
                        {
                            new BLL.T_RepositoryInformation().Delete(Convert.ToInt32(rid));
                        }
                    }
                }
                bool r = new BLL.T_RepositoryCategory().Delete(Convert.ToInt32(ids));

                if (r)
                    return Success("删除成功");
                else
                {
                    return Error("删除失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }
    }
}