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

namespace CallCenterApi.Interface.Controllers.callout
{
    public class BanCallOutController : BaseController
    {
        private readonly BLL.T_Call_BanCallOut banCallOutBLL = new BLL.T_Call_BanCallOut();
        private readonly BLL.T_Sys_BanCallOutTime banCallOutTimeBLL = new BLL.T_Sys_BanCallOutTime();


        #region 禁止外呼号码管理
        public ActionResult GetList(FilterBanCallOut filter)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(filter.Phone))
            {
                sb.Append($"  and  F_Phone='{filter.Phone}'");
            }
            //if (filter.Start != null && filter.End != null)
            //{
            //    sb.Append($"  and  F_Phone='{filter.Phone}'");
            //}
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                             "T_Call_BanCallOut",
                             "F_Id",
                             "*",
                             sb.ToString(),
                             "ORDER BY F_ID desc",
                             filter.PageSize,
                             filter.PageIndex,
                             true,
                             out recordCount);
            List<Model.T_Call_BanCallOut> modelList = new BLL.T_Call_BanCallOut().DataTableToList(dt);
            var obj = new
            {
                rows = modelList,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        public ActionResult Add(BanCallOutInput input)
        {
            if (string.IsNullOrWhiteSpace(input.Phone))
                return Error("号码不可为空");
            var model = banCallOutBLL.GetModel(input.Phone);
            if (model != null)
                return Error("该号码已被限制外呼");
            if (banCallOutBLL.Add(new Model.T_Call_BanCallOut()
            {
                F_Phone = input.Phone,
                F_Remark = input.Remark
            }) > 0)
                return Success("添加成功");
            return Error("添加失败");
        }

        public ActionResult Delete(int[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("请选择需要删除的项");
            var idsStr = string.Join(",", ids);
            banCallOutBLL.deleteBatch(idsStr);
            return Success("删除成功");
        }
        #endregion

        #region 限制外呼时间管理
        public ActionResult GetBanCallOutTimeList(FilterBanCallOut filter)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(filter.Phone))
            {
                sb.Append($"  and  F_Phone='{filter.Phone}'");
            }
            //if (filter.Start != null && filter.End != null)
            //{
            //    sb.Append($"  and  F_Phone='{filter.Phone}'");
            //}
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                             "T_Sys_BanCallOut",
                             "F_Id",
                             "*",
                             sb.ToString(),
                             "ORDER BY F_ID desc",
                             filter.PageSize,
                             filter.PageIndex,
                             true,
                             out recordCount);
            List<Model.T_Sys_BanCallOutTime> modelList = new BLL.T_Sys_BanCallOutTime().DataTableToList(dt);
            var obj = new
            {
                rows = modelList,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        public ActionResult AddBanCallOutTime(BanCallOutInput input)
        {
            if (string.IsNullOrWhiteSpace(input.Phone))
                return Error("号码不可为空");
            var model = banCallOutBLL.GetModel(input.Phone);
            if (model != null)
                return Error("该号码已被限制外呼");
            if (banCallOutBLL.Add(new Model.T_Call_BanCallOut()
            {
                F_Phone = input.Phone,
                F_Remark = input.Remark
            }) > 0)
                return Success("添加成功");
            return Error("添加失败");
        }

        public ActionResult DeleteBanCallOutTime(int[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("请选择需要删除的项");
            var idsStr = string.Join(",", ids);
            banCallOutBLL.deleteBatch(idsStr);
            return Success("删除成功");
        }
        #endregion

    }
}