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
{
    [Authority]
    public class DutyPhoneController : BaseController
    {
        //获取值班电话列表
        public ActionResult GetList()
        {
            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 F_CreateTime >= '" + Convert.ToDateTime(strbtime.Trim()) + "' ";
            }
            if (stretime.Trim() != "" && stretime != "undefined")
            {
                sql += " and F_CreateTime <= '" + Convert.ToDateTime(stretime.Trim()) + "' ";
            }

            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
            };

            return Content(obj.ToJson());

        }

        //获取值班电话
        public ActionResult GetDutyPhone(string zbid)
        {
            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)
                {
                    return Success("获取值班电话成功", dModel);
                }
                else
                {
                    return Error("获取值班电话失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //添加值班电话
        public ActionResult AddDutyPhone(string zbid, string telphone, string des)
        {
            return 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)
                {
                    dModel.F_ZBDH = telphone.Trim();
                    dModel.F_Remark = des.Trim();

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

                dModel.F_ZBDH = telphone.Trim();
                dModel.F_Remark = des.Trim();
                dModel.F_CreateBy = User.UserData["F_UserName"];
                dModel.F_CreateID = Utils.StrToInt(User.UserData["F_UserID"], 0);
                dModel.F_CreateTime = DateTime.Now;

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

        }

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

        }
    }
}