using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Filter;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    public class SeatGroupController : BaseController
    {
        BLL.T_Sys_SeatGroup seatBLL = new BLL.T_Sys_SeatGroup();
        /// <summary>
        /// 获取坐席组列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList(FilterSeatGroup filter)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            DataTable dt = new DataTable();
            string sql = "";
            if (!string.IsNullOrWhiteSpace(filter.SeatName))
            {
                sql += $" and F_ZXZName like '%{filter.SeatName}%'";
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_SeatGroup",
                "F_ZXZID",
                "*",
                sql,
                "ORDER BY F_ZXZID desc",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);

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

            res = Content(obj.ToJson());

            return res;
        }

        /// <summary>
        /// 获取坐席组列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetListDrop()
        {
            var list = seatBLL.DataTableToList(seatBLL.GetAllList().Tables[0]);
            return Success("获取成功", list.Select(x => new
            {
                id = x.F_ZXZID,
                name = x.F_ZXZName
            }));
        }

        //获取坐席组列表
        public ActionResult GetSeatList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                List<Model.T_Sys_SeatGroup> seatList = seatBLL.GetModelList(" 1=1 order by F_ZXZID desc ");
                if (seatList.Count > 0)
                    res = Success("坐席组列表加载成功", seatList);
                else
                    res = Error("坐席组列表加载失败");
            }
            return res;
        }

        /// <summary>
        /// 获取坐席组
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetSeatGroup(int ZXZID = 0)
        {
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(ZXZID);
                if (sModel != null)
                    return Success("获取坐席组信息成功", sModel);
                return Error("获取坐席组信息失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 添加坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddSeatGroup(SeatGroupInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_SeatGroup sModel = new Model.T_Sys_SeatGroup();
                if (input.ZXZCode != null)
                    sModel.F_ZXZCode = input.ZXZCode.Trim();
                else
                {
                    res = Error("编号不能为空");
                    return res;
                }
                if (input.ZXZName != null)
                    sModel.F_ZXZName = input.ZXZName.Trim();
                else
                {
                    res = Error("名称不能为空");
                    return res;
                }
                if (input.ZXAtt != null)
                    sModel.F_ZXAtt = input.ZXAtt.Trim();
                else
                {
                    res = Error("坐席地区号不能为空");
                    return res;
                }
                if (input.WHWDKey != null)
                    sModel.F_WHWDKey = input.WHWDKey.Trim();
                else
                {
                    res = Error("外呼外地前缀不能为空");
                    return res;
                }
                if (input.WHBDKey != null)
                    sModel.F_WHBDKey = input.WHBDKey.Trim();
                else
                {
                    res = Error("外呼本地前缀不能为空");
                    return res;
                }
                if (input.IVRKey != null)
                    sModel.F_IVRKey = input.IVRKey;
                if (input.Des != null)
                    sModel.F_Des = input.Des.Trim();
                if (!Validate.IsNumber(input.WHWDKey.ToString()))
                {
                    res = Error("外呼外地前缀必须为数字");
                    return res;
                }
                if (!Validate.IsNumber(input.WHBDKey.ToString()))
                {
                    res = Error("外呼本地前缀必须为数字");
                    return res;
                }
                sModel.F_CreateTime = DateTime.Now;
                sModel.F_CreateByName = CurrentUser.UserData.F_UserName;
                sModel.F_CreateBy = CurrentUser.UserData.F_UserId;

                if (seatBLL.Add(sModel) > 0)
                    res = Success("添加成功");
                else
                    res = Success("添加失败");
            }
            return res;
        }

        /// <summary>
        /// 编辑坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditSeatGroup(SeatGroupInput input)
        {
            if (Request.IsAuthenticated)
            {
                if (input.ZXZID <= 0)
                    return Error("请选择要编辑的坐席组");

                Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(input.ZXZID);
                if (sModel == null)
                    return Error("获取信息失败");

                if (input.ZXZCode != null)
                    sModel.F_ZXZCode = input.ZXZCode.Trim();
                if (input.ZXZName != null)
                    sModel.F_ZXZName = input.ZXZName.Trim();
                if (input.ZXAtt != null)
                    sModel.F_ZXAtt = input.ZXAtt.Trim();
                if (input.WHWDKey != null)
                    sModel.F_WHWDKey = input.WHWDKey.Trim();
                if (input.WHBDKey != null)
                    sModel.F_WHBDKey = input.WHBDKey.Trim();
                if (input.IVRKey != null)
                    sModel.F_IVRKey = input.IVRKey;
                if (input.Des != null)
                    sModel.F_Des = input.Des.Trim();
                
                if (seatBLL.Update(sModel))
                    return Success("编辑成功");
                return Error("编辑失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 删除坐席组
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelSeatGroup(string[] ids)
        {
            if (Request.IsAuthenticated)
            {
                if (ids == null || ids.Length <= 0)
                    return Error("请选择要删除的坐席组");

                var idStr = string.Join(",", ids);
                if (string.IsNullOrEmpty(idStr.Trim()))
                    return Error("请选择要删除的坐席组");

                if (seatBLL.DeleteList(idStr))
                    return Success("删除成功");
                return Error("删除失败");
            }
            return NoToken("未知错误，请重新登录");
        }


    }
}