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()
        {
            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 like '%" + 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;
            string tablenew = "( select a.*,b.F_username from T_Call_Blacklist a left join  T_Sys_UserAccount b on  a.F_UserId=b.F_UserId)as t ";

            dt = BLL.PagerBLL.GetListPager(
                tablenew,
                "F_BlackId",
                "*",
                sql,
                "ORDER BY F_BlackId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);
            BLL.T_Sys_UserAccount userbll = new BLL.T_Sys_UserAccount();
         List<Model.T_Sys_UserAccount> userlist= userbll.GetModelList("");
          
      
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());
        }

        //获取黑名单
        public ActionResult GetBlack(string blackid)
        {
            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)
                {
                    return Success("获取黑名单成功", dModel);
                }
                else
                {
                    return Error("获取黑名单失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        //添加黑名单
        public ActionResult AddBlack(string blackid, string telphone, string settime, string removetime, string des)
        {
          // return 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)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return 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;
                dModel.F_UserId = CurrentUser.UserData.F_UserId;
                int b = new BLL.T_Call_Blacklist().Add(dModel);
                if (b > 0)
                {
                    return Success("添加成功");
                }
                else
                {
                    return Error("添加失败");
                }
            }
            return Error("操作失败");
        }


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