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
{
    public class IndexCategoryController : BaseController
    {
        //获取指标分类列表
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            string sql = "";
            DataTable dt = new DataTable();
            string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                
                if (strpageindex.Trim() != "")
                {
                    pageindex = Convert.ToInt32(strpageindex);
                }

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

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

                res = Content(obj.ToJson());

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

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

            return res;
        }

        public ActionResult GetAllList()
        {
            var list = new BLL.T_QC_IndexCategory().GetModelList("");
            return Success("获取下拉指标分类成功", list);
        }

        //获取指标分类
        public ActionResult GetIndexCategory(string cid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

                if (cid.Trim() != "")
                {
                    BLL.T_QC_IndexCategory dBLL = new BLL.T_QC_IndexCategory();
                    Model.T_QC_IndexCategory dModel = dBLL.GetModel(int.Parse(cid.Trim()));
                    if (dModel != null)
                    {
                        res = Success("获取指标分类成功", dModel);
                    }
                    else
                    {
                        res = Error("获取指标分类失败");
                    }
                }
                else
                {
                    res = Error("获取参数失败");
                }

            return res;
        }

        //添加指标分类
        public ActionResult AddIndexCategory(string categoryName, string sort, string pid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

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

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

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

            }
            return res;
        }

        //编辑指标分类
        public ActionResult EditIndexCategory(string cid,string categoryName, string sort, string pid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                    if (cid.Trim() != "")
                    {
                        BLL.T_QC_IndexCategory dBLL = new BLL.T_QC_IndexCategory();
                        Model.T_QC_IndexCategory dModel = dBLL.GetModel(int.Parse(cid.Trim()));
                        if (dModel != null)
                        {
                            if (!string.IsNullOrWhiteSpace(categoryName.Trim()))
                            {
                                dModel.F_CategoryName = WebHelper.UrlDecode(categoryName.Trim());
                            }
                            if (!string.IsNullOrWhiteSpace(sort.Trim()))
                            {
                                dModel.F_Sort = Convert.ToInt32(sort.Trim());
                            }
                            if (!string.IsNullOrWhiteSpace(pid.Trim()))
                            {
                                dModel.F_ParentId = Convert.ToInt32(pid.Trim());
                            }
                            bool b = dBLL.Update(dModel);
                            if (b)
                            {
                                res = Success("编辑成功");
                            }
                            else
                            {
                                res = Success("编辑失败");
                            }
                        }
                        else
                            res = Error("获取信息失败");
                    }
                    else
                    {
                        res = Error("请选择要编辑的分类");
                    }

            }
            return res;
        }

        //删除指标分类记录
        public ActionResult DelIndexCategory(string id)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            DataTable dt = new DataTable();

                if (!string.IsNullOrEmpty(id.Trim()))
                {
                    dt = new BLL.T_QC_IndexBase().GetList(" F_CategoryId=" + id + " ").Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        res = Error("请先删除此分类下的指标");
                    }
                    else
                    {
                        if (new BLL.T_QC_IndexCategory().Delete(Convert.ToInt32(id)))
                        {
                            res = Success("删除成功");
                        }
                        else
                        {
                            res = Error("删除失败");
                        }

                    }
                }
                else
                {
                    res = Error("请选择要删除的记录");
                }

            return res;
        }
    }
}