using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.knowledge
{
    public class KnowledgeClassController : BaseController
    {
        // 获取知识库分类列表
        public ActionResult GetAllList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            
                DataTable dt = new DataTable();
                string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
                string sql = " F_DeleteFlag=0 ";

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

                res = Success("加载成功", dt);

            return res;
        }
        // 获取知识库分类列表
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");

                DataTable dt = new DataTable();
                string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
                string sql = " and F_DeleteFlag=0 and F_CategoryType is null";
                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)
                {
                    res = Success("加载成功", modelList);
                }
                else
                    res = Error("加载失败");

            return res;
        }
        //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 + " and F_DeleteFlag=0 and F_CategoryType is null 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;
            }

        }

        //获取知识库分类
        public ActionResult GetClass(string categoryid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

                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)
                    {
                        res = Success("获取知识库分类信息成功", dModel);
                    }
                    else
                    {
                        res = Error("获取知识库分类信息失败");
                    }
                }
                else
                {
                    res = Error("获取参数失败");
                }

            return res;
        }
        //[Authority]
        //添加知识库分类
        public ActionResult AddClass(string cname, string pid, string sort)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                    Model.T_RepositoryCategory dModel = new Model.T_RepositoryCategory();
                    if (pid != "")
                    {
                        Model.T_RepositoryCategory pModel = new BLL.T_RepositoryCategory().GetModel(int.Parse(pid.Trim()));
                        dModel.F_ParentId = int.Parse(pid.Trim());
                        if (pModel != null)
                            dModel.F_Expand1 = pModel.F_CategoryName;
                    }
                    else
                        dModel.F_ParentId = 0;

                    if (sort != "")
                    {
                        dModel.F_Sort = int.Parse(sort.Trim());
                    }
                    else
                        dModel.F_Sort = 0;
                    dModel.F_CategoryName = cname.Trim();

                    dModel.F_CreateOn = DateTime.Now;
                    dModel.F_CreateBy = CurrentUser.UserData.F_UserId;
                    dModel.F_DeleteFlag = 0;
                    int b = new BLL.T_RepositoryCategory().Add(dModel);
                    if (b > 0)
                    {
                        res = Success("添加成功");
                    }
                    else
                    {
                        res = Success("添加失败");
                    }

            }
            return res;
        }
        //[Authority]
        //编辑知识库分类
        public ActionResult EditClass(string cid, string cname, string pid, string sort)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                    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() != "")
                            {
                                Model.T_RepositoryCategory pModel = new BLL.T_RepositoryCategory().GetModel(int.Parse(pid.Trim()));
                                dModel.F_ParentId = int.Parse(pid.Trim());
                                if (pModel != null)
                                    dModel.F_Expand1 = pModel.F_CategoryName;
                            }

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

            }
            return res;
        }
        //[Authority]
        //删除知识库分类
        public ActionResult DelClass(string ids)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

                //string ids = RequestString.GetQueryString("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)
                        res = Success("删除成功");
                    else
                    {
                        res = Error("删除失败");
                    }
                }
                else
                {
                    res = Error("获取参数失败");
                }

            return res;
        }

    }
}