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.Quality
{
    [Authority]
    public class IndexCategoryController : BaseController
    {
        // GET: IndexCategory

        //获取指标分类列表
        public ActionResult GetList(int page = 1, int pagesize = 10)
        {
            string sql = " and F_DeleteFlag=0";
            int recordCount = 0;
            DataTable dt = BLL.PagerBLL.GetListPager(
                "T_QC_IndexCategory",
                "F_CategoryId",
                "*",
                sql,
                "ORDER BY F_CategoryId ",
                pagesize,
                page,
                true,
                out recordCount);

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

            return Content(obj.ToJson());
        }

        //获取指标分类列表
        public ActionResult GetAllList()
        {
            return Success("成功", new BLL.T_QC_IndexCategory().GetList("F_DeleteFlag=0").Tables[0]);
        }

        //获取指标分类
        public ActionResult GetIndexCategory(int id)
        {
            if (id > 0)
            {
                BLL.T_QC_IndexCategory dBLL = new BLL.T_QC_IndexCategory();
                Model.T_QC_IndexCategory dModel = dBLL.GetModel(id);
                if (dModel != null)
                {
                    return Success("获取指标分类成功", dModel);
                }
                else
                {
                    return Error("获取指标分类失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        //添加指标分类
        public ActionResult AddIndexCategory(string categoryName, int sort=0, int pid=0)
        {

            Model.T_QC_IndexCategory dModel = new Model.T_QC_IndexCategory();

            if (!string.IsNullOrWhiteSpace(categoryName.Trim()))
            {
                dModel.F_CategoryName = WebHelper.UrlDecode(categoryName.Trim());
            }
            dModel.F_Sort = sort;
            dModel.F_ParentId = pid;
            dModel.F_Score = 0;
            dModel.F_CreateOn = DateTime.Now;
            dModel.F_CreateBy = User.F_UserId;
            dModel.F_DeleteFlag = 0;

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

        //编辑指标分类
        public ActionResult EditIndexCategory(int id, string categoryName, int sort = 0, int pid=0)
        {
            if (id > 0)
            {
                BLL.T_QC_IndexCategory dBLL = new BLL.T_QC_IndexCategory();
                Model.T_QC_IndexCategory dModel = dBLL.GetModel(id);
                if (dModel != null)
                {
                    if (!string.IsNullOrWhiteSpace(categoryName.Trim()))
                    {
                        dModel.F_CategoryName = WebHelper.UrlDecode(categoryName.Trim());
                    }
                    dModel.F_Sort = sort;
                    dModel.F_ParentId = pid;
                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
            }
            else
            {
                return Error("请选择要编辑的分类");
            }
        }

        //删除指标分类记录
        public ActionResult DelIndexCategory(int id)
        {

            if (id>0)
            {
                var n = DB.DbHelperSQL.GetSingle(" select count(1) from T_QC_IndexBase where F_CategoryId=" + id)?.ToString() ?? "0";
                if (int.Parse(n) > 0)
                {
                    return Error("请先删除此分类下的指标");
                }
                else
                {
                    if (new BLL.T_QC_IndexCategory().Delete(id))
                    {
                        return Success("删除成功");
                    }
                    else
                    {
                        return Error("删除失败");
                    }
                }
            }
            else
            {
                return Error("请选择要删除的记录");
            }
        }
    }
}