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.tel
{
    public class CallblackController : BaseController
    {
        //获取黑名单列表
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
              
                    string strtelnum = HttpUtility.UrlDecode(RequestString.GetQueryString("tel"));
                    string strsettime = HttpUtility.UrlDecode(RequestString.GetQueryString("settime"));
                    string strremovetime = HttpUtility.UrlDecode(RequestString.GetQueryString("removetime"));

                    string strpageindex = RequestString.GetQueryString("page");
                    int pageindex = 1;
                    string strpagesize = RequestString.GetQueryString("pagesize");
                    int pagesize = 10;

                    if (strtelnum != null && strtelnum.Trim() != "")
                    {
                        sql += " and F_TelPhone= '" + strtelnum.Trim() + "' ";
                    }
                    if (strsettime.Trim() != "" && strsettime != "undefined")
                    {
                        sql += " and F_SetTime > '" + Convert.ToDateTime(strsettime.Trim()) + "' ";
                    }
                    if (strremovetime.Trim() != "" && strremovetime != "undefined")
                    {
                        sql += " and F_RemoveTime < '" + Convert.ToDateTime(strremovetime.Trim()) + "' ";
                    }

                    if (strpageindex.Trim() != "")
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }

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

                    var obj = new
                    {
                        state = "success",
                        message = "成功",
                        rows = dt,
                        total = recordCount
                    };

                    res = Content(obj.ToJson());
             
            }
            return res;
        }

        //获取黑名单
        public ActionResult GetBlack(string blackid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
               
                    if (blackid != null && blackid.Trim() != "")
                    {
                        BLL.T_Call_Blacklist dBLL = new BLL.T_Call_Blacklist();
                        Model.T_Call_Blacklist dModel = dBLL.GetModel(int.Parse(blackid.Trim()));
                        if (dModel != null)
                        {
                            res = Success("获取黑名单成功", dModel);
                        }
                        else
                        {
                            res = Error("获取黑名单失败");
                        }
                    }
                    else
                    {
                        res = Error("获取参数失败");
                    }
               
            }
            return res;
        }

        //添加黑名单
        public ActionResult AddBlack(string blackid,string telphone,string settime,string removetime,string des)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                
                    res = Error("操作失败");
                    if (blackid != null && blackid.Trim() != "")
                    {
                        BLL.T_Call_Blacklist dBLL = new BLL.T_Call_Blacklist();
                        Model.T_Call_Blacklist dModel = dBLL.GetModel(int.Parse(blackid.Trim()));
                        if (dModel != null)
                        {
                            dModel.F_TelPhone = telphone.Trim();
                            dModel.F_SetTime = Convert.ToDateTime(settime.Trim());
                            dModel.F_RemoveTime = Convert.ToDateTime(removetime.Trim());
                            dModel.F_Describe = des.Trim();
                            dModel.F_InterceptNum += 1;

                            bool b = dBLL.Update(dModel);
                            if (b)
                            {
                                res = Success("编辑成功");
                            }
                            else
                            {
                                res = Error("编辑失败");
                            }
                        }
                    }
                    else
                    {
                        Model.T_Call_Blacklist dModel = new Model.T_Call_Blacklist();

                        dModel.F_TelPhone = telphone.Trim();
                        dModel.F_SetTime = Convert.ToDateTime(settime.Trim());
                        dModel.F_RemoveTime = Convert.ToDateTime(removetime.Trim());
                        dModel.F_Describe = des.Trim();
                        dModel.F_InterceptNum = 1;
                        int b = new BLL.T_Call_Blacklist().Add(dModel);
                        if (b > 0)
                        {
                            res = Success("添加成功");
                        }
                        else
                        {
                            res = Error("添加失败");
                        }
                    }
              
            }
            return res;
        }
        

        //删除黑名单记录
        public ActionResult DelCallBlack(string[] ids)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
           
                if (ids!=null&&ids.Length>0)
                {
                    string idd = " ";
                    foreach (string str in ids)
                    {
                        idd += str + ",";
                    }
                    if (new BLL.T_Call_Blacklist().DeleteList(idd.TrimEnd(',')))
                    {
                        res = Success("删除成功");
                    }
                    else
                        res = Error("删除失败");
                }
                else
                {
                    res = Error("请选择要删除的记录");
                }
         
            return res;
        }
    }
}