using CallCenter.Utility;
using CallCenterApi.Common;
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 DutyPhoneController : BaseController
    {
        //获取值班电话列表
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();

                string strtelnum = HttpUtility.UrlDecode(RequestString.GetQueryString("telnum"));
                string strbtime = HttpUtility.UrlDecode(RequestString.GetQueryString("btime"));
                string stretime = HttpUtility.UrlDecode(RequestString.GetQueryString("etime"));

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

                if (strtelnum.Trim() != "" && strtelnum != "undefined")
                {
                    sql += " and F_ZBDH like '%" + strtelnum.Trim() + "%' ";
                }
                if (strbtime.Trim() != "" && strbtime != "undefined")
                {
                    sql += $" and DATEDIFF(s,'{Convert.ToDateTime(strbtime.Trim()).ToShortDateString()} 00:00:01',F_CreateTime)>=0  ";
                }
                if (stretime.Trim() != "" && stretime != "undefined")
                {
                    sql += $" and DATEDIFF(s,F_CreateTime,'{Convert.ToDateTime(stretime.Trim()).ToShortDateString()} 23:59:59')>=0  ";
                }

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

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

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

                res = Content(obj.ToJson());

            }
            return res;
        }

        //获取值班电话
        public ActionResult GetDutyPhone(string zbid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                if (zbid != null && zbid.Trim() != "")
                {
                    BLL.T_Call_ZBDH dBLL = new BLL.T_Call_ZBDH();
                    Model.T_Call_ZBDH dModel = dBLL.GetModel(int.Parse(zbid.Trim()));
                    if (dModel != null)
                    {
                        res = Success("获取值班电话成功", dModel);
                    }
                    else
                    {
                        res = Error("获取值班电话失败");
                    }
                }
                else
                {
                    res = Error("获取参数失败");
                }

            }
            return res;
        }

        //添加值班电话
        public ActionResult AddDutyPhone(string zbid, string telphone, string des)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                res = Error("操作失败");
                if (zbid != null && zbid.Trim() != "")
                {
                    BLL.T_Call_ZBDH dBLL = new BLL.T_Call_ZBDH();
                    Model.T_Call_ZBDH dModel = dBLL.GetModel(int.Parse(zbid.Trim()));
                    if (dModel != null)
                    {

                        if (!Validate.IsNumber(telphone.Trim()))
                        {
                            res = Error("电话号码必须为数字！");
                            return res;
                        }
                        dModel.F_ZBDH = telphone.Trim();
                        dModel.F_Remark = des.Trim();

                        bool b = dBLL.Update(dModel);
                        if (b)
                        {
                            res = Success("编辑成功");
                        }
                        else
                        {
                            res = Error("编辑失败");
                        }
                    }
                }
                else
                {
                    Model.T_Call_ZBDH dModel = new Model.T_Call_ZBDH();
                    if (!Validate.IsNumber(telphone.Trim()))
                    {
                        res = Error("电话号码必须为数字！");
                        return res;
                    }
                    dModel.F_ZBDH = telphone.Trim();
                    dModel.F_Remark = des.Trim();
                    dModel.F_CreateBy = CurrentUser.UserData.F_UserName;
                    dModel.F_CreateID = CurrentUser.UserData.F_UserId;
                    dModel.F_CreateTime = DateTime.Now;

                    int b = new BLL.T_Call_ZBDH().Add(dModel);
                    if (b > 0)
                    {
                        res = Success("添加成功");
                    }
                    else
                    {
                        res = Error("添加失败");
                    }
                }

            }
            return res;
        }

        //删除值班电话
        public ActionResult DelDutyPhone(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_ZBDH().DeleteList(idd.TrimEnd(',')))
                {
                    res = Success("删除成功");
                }
                else
                    res = Error("删除失败");
            }
            else
            {
                res = Error("请选择要删除的记录");
            }

            return res;
        }
    }
}