using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Filter;
using CallCenterApi.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.callout
{
    public class ExcelTmpController : BaseController
    {
        private readonly BLL.T_Form_EntityAttribute formEntityAttrBLL = new BLL.T_Form_EntityAttribute();
        private readonly BLL.T_Call_PageField pageFieldBLL = new BLL.T_Call_PageField();
        private readonly BLL.T_Call_PageTemp pageTempBLL = new BLL.T_Call_PageTemp();
        /// <summary>
        /// 获取所有自定义字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterExcelTmp filter)
        {
            string sql = " and F_DBTableName='T_Call_PageField' ";
            if (filter.TempId > 0)
            {
                sql += " and F_AttributeId not in (SELECT [F_FeildId] FROM [T_Call_PageField] where [F_TempId]=" + filter.TempId + ") ";
            }
            else
            {
                sql += " and F_AttributeId not in (SELECT [F_FeildId] FROM [T_Call_PageField] where [F_TaskId]=" + filter.TaskId + ") ";
            }

            if (filter.Type > 0)
            {
                sql += " and F_DBTypeName='" + filter.Type + "' ";
            }


            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "T_Form_EntityAttribute",
                "F_AttributeId",
                "*",
                sql,
                "ORDER BY F_ColumnNumber ",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);
            var modelList = formEntityAttrBLL.DataTableToList(dt);

            var obj = new
            {
                rows = modelList.Select(x => new
                {
                    id = x.F_AttributeId,
                    name = x.F_Name,
                    length = x.F_Length,
                    type = x.F_DBTypeName == "nvarchar" ? "文本" : (x.F_DBTypeName == "int" ? "整数" : (x.F_DBTypeName == "datetime" ? "时间" : "")),
                    field = x.F_DBFieldlName
                }),
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取已选择的自定义字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetSelectList(FilterExcelTmp filter)
        {
            var sql = "";
            if (filter.TaskId > 0)
            {
                sql += " and [F_TaskId]=" + filter.TaskId + " ";
            }
            else if (filter.TaskId == 0 && filter.TempId > 0)
            {
                sql += " and [F_TaskId]=0 and F_TempId=" + filter.TempId;
            }
            else
            {
                sql += " and [F_TaskId]=-1 ";
            }


            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                             "vw_Call_PageField",
                             "F_Id",
                             "*",
                             sql,
                             "ORDER BY F_Sort ",
                             filter.PageSize,
                             filter.PageIndex,
                             true,
                             out recordCount);
            var modelList = pageFieldBLL.DataTableToListView(dt);
            var obj = new
            {
                rows = modelList.Select(x => new
                {
                    id = x.F_Id,
                    name = x.F_Name,
                    length = x.F_Length,
                    type = x.F_DBTypeName == "nvarchar" ? "文本" : (x.F_DBTypeName == "int" ? "整数" : (x.F_DBTypeName == "datetime" ? "时间" : "")),
                    field = x.F_DBFieldlName
                }),
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取已选择的自定义字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetSelectIdList(FilterExcelTmp filter)
        {
            var sql = "";
            if (filter.TaskId > 0)
            {
                sql += "  [F_TaskId]=" + filter.TaskId + " ";
            }
            else
            {
                sql += "  [F_TaskId]=-1 ";
            }

            var idList = pageFieldBLL.DataTableToListView(pageFieldBLL.GetListView(sql).Tables[0]);
            return Success("获取成功", idList.Select(x => new
            {
                id = x.F_FeildId,
                selectid = x.F_Id
            }));
        }

        /// <summary>
        /// 获取已选择的自定义字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetSelectedList(FilterExcelTmp filter)
        {
            var sql = "";
            if (filter.TempId > 0)
            {
                sql += "  [F_TempId]=" + filter.TempId + " ";
            }
            else
            {
                sql += "  [F_TaskId]=-1 ";
            }

            var idList = pageFieldBLL.DataTableToListView(pageFieldBLL.GetListView(sql).Tables[0]);
            return Success("获取成功", idList.Select(x => new
            {
                id = x.F_Id,
                name = x.F_Name
            }));
        }

        /// <summary>
        /// 选择自定义字段
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public ActionResult Add(int[] ids, int taskId, int tempId)
        {
            if (ids == null)
                return Error("");
            var sort = 0;
            foreach (var id in ids)
            {
                var fieldmodel = formEntityAttrBLL.GetModel(id);
                if (fieldmodel == null)
                    continue;
                var itemmodel = new T_Call_PageField();
                itemmodel.F_Name = fieldmodel.F_Name;
                itemmodel.F_DBFieldlName = fieldmodel.F_DBFieldlName;
                itemmodel.F_TaskId = tempId > 0 ? 0 : taskId;
                itemmodel.F_Sort = sort++;
                itemmodel.F_FeildId = id;
                itemmodel.F_TempId = tempId;
                if (pageFieldBLL.Add(itemmodel) > 0)
                    continue;
            }
            return Success("添加成功");
        }

        /// <summary>
        /// 去除自定义字段
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Remove(int[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                pageFieldBLL.DeleteList(string.Join(",", ids));
            }
            return Success("删除成功");
        }

        /// <summary>
        /// 修改自定义字段
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult Update(int id, string name)
        {
            if (id <= 0 || string.IsNullOrWhiteSpace(name))
                return Error("参数错误");
            var model = pageFieldBLL.GetModel(id);
            model.F_Name = name;
            if (pageFieldBLL.Update(model))
                return Success("修改成功");
            return Error("修改失败");
        }

        public ActionResult AddTemp(int id = 0, string name = "")
        {
            if (string.IsNullOrWhiteSpace(name))
                return Error("请填写模板名称");
            T_Call_PageTemp model = new T_Call_PageTemp();
            if (id <= 0)
            {
                model = new T_Call_PageTemp
                {
                    F_Name = name
                };
                id = pageTempBLL.Add(model);
                if (id > 0)
                {
                    pageFieldBLL.UpdateByTempId(id);
                }
                return Success("");
            }
            model = pageTempBLL.GetModel(id);
            model.F_Name = name;
            if (pageTempBLL.Update(model))
                return Success("");
            return Error("");
        }

        public ActionResult GetAllTemp()
        {
            var dt = DbHelperSQL.Query("SELECT F_ID,F_Name from T_Call_PageTemp").Tables[0];
            return Success("", dt);
        }

        public ActionResult DeleteTemp(int id)
        {
            if (id <= 0)
                return Error("请选择需要删除的模板");
            if (pageTempBLL.Delete(id))
                return Success("");
            return Error("删除失败");
        }



    }
}