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
{
    public class KnowledgeController : BaseController
    {
        // 获取知识库列表
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");

                DataTable dt = new DataTable();
                string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
                string keywords = HttpUtility.UrlDecode(RequestString.GetQueryString("keywords"));
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;

                string sql = " and F_DeleteFlag=0";
                if (pid.Trim() != "")
                {
                    sql += " and F_CategoryId=" + pid.Trim();
                }
                if (keywords.Trim() != "")
                {
                    sql += " and (F_Content like '%" + keywords.Trim() + "%' or F_Title like '%"
                        + keywords.Trim() + "%' or F_KeyWords like '%" + keywords.Trim() + "%')";
                }
                if (strpageindex.Trim() != "")
                {
                    pageindex = Convert.ToInt32(strpageindex);
                }

                if (strpagesize.Trim() != "")
                {
                    pagesize = Convert.ToInt32(strpagesize);
                }
                int recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                    "T_RepositoryInformation",
                    "F_RepositoryId",
                    "*",
                    sql,
                    "ORDER BY F_RepositoryId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                //System.Collections.Generic.List<Model.T_RepositoryInformation> modelList = new BLL.T_RepositoryInformation().DataTableToList(dt);

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

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

                res = Content(obj.ToJson());

            return res;
        }

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

                if (infoid != null && infoid.Trim() != "")
                {
                    BLL.T_RepositoryInformation dBLL = new BLL.T_RepositoryInformation();
                    Model.T_RepositoryInformation dModel = dBLL.GetModel(int.Parse(infoid.Trim()));
                    if (dModel != null)
                    {
                        res = Success("获取知识库信息成功", dModel);
                    }
                    else
                    {
                        res = Error("获取知识库信息失败");
                    }
                }
                else
                {
                    res = Error("获取参数失败");
                }

            return res;
        }

        //[Authority]
        //添加知识库
        public ActionResult AddInfo(string title, string key, string con, string pid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

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

                    dModel.F_Title = title.Trim();
                    dModel.F_KeyWords = key.Trim();
                    dModel.F_Content = WebHelper.UrlDecode(con.Trim());
                    dModel.F_Description = WebHelper.NoHtml(WebHelper.UrlDecode(con.Trim()));

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

            }
            return res;
        }
        //[Authority]
        //编辑知识库
        public ActionResult EditInfo(string infoid, string title, string key, string con, string pid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                    if (infoid != null && infoid.Trim() != "")
                    {
                        BLL.T_RepositoryInformation dBLL = new BLL.T_RepositoryInformation();
                        Model.T_RepositoryInformation dModel = dBLL.GetModel(int.Parse(infoid.Trim()));
                        if (dModel != null)
                        {
                            if (pid.Trim() != "")
                            {
                                Model.T_RepositoryCategory pModel = new BLL.T_RepositoryCategory().GetModel(int.Parse(pid.Trim()));
                                dModel.F_CategoryId = int.Parse(pid.Trim());
                                if (pModel != null)
                                    dModel.F_Expand1 = pModel.F_CategoryName;
                            }
                            if (title.Trim() != "")
                            {
                                dModel.F_Title = title.Trim();
                            }
                            if (key.Trim() != "")
                            {
                                dModel.F_KeyWords = key.Trim();
                            }
                            if (con.Trim() != "")
                            {
                                dModel.F_Content = WebHelper.UrlDecode(con.Trim());
                                dModel.F_Description = WebHelper.NoHtml(WebHelper.UrlDecode(con.Trim()));
                            }

                            dModel.F_ModifyBy = CurrentUser.UserData.F_UserId;
                            dModel.F_ModifyOn = DateTime.Now;
                            bool b = dBLL.Update(dModel);
                            if (b)
                            {
                                res = Success("编辑成功");
                            }
                            else
                            {
                                res = Success("编辑失败");
                            }
                        }
                        else
                            res = Error("获取信息失败");
                    }
                    else
                    {
                        res = Error("请选择要编辑的知识库");
                    }

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

                //string ids = RequestString.GetQueryString("ids");
                if (ids != null && ids.Length > 0)
                {
                    string idd = " ";
                    foreach (string str in ids)
                    {
                        idd += str + ",";
                    }
                    if (new BLL.T_RepositoryInformation().DeleteList(idd.TrimEnd(',')))
                    {
                        res = Success("删除成功");
                    }
                    else
                        res = Error("删除失败");
                }
                else
                {
                    res = Error("获取参数失败");
                }

            return res;
        }
    }
}