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
{
    [Authority]
    public class RecordSetController : BaseController
    {
        BLL.T_Call_RecordSet dBLL = new BLL.T_Call_RecordSet();
        //获取录音设置列表
        public ActionResult GetList(string tel, int? type)
        {
            string sql = "";
            DataTable dt = new DataTable();

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

            if (tel != null && tel.Trim() != "")
            {
                sql += " and F_TelPhone like '%" + tel.Trim() + "%' ";
            }
            if (type != null)
            {
                sql += " and F_Type=" + type + " ";
            }

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

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

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

            return Content(obj.ToJson());

        }

        //获取录音设置
        public ActionResult GetTelSet(string rid)
        {
            if (rid != null && rid.Trim() != "")
            {
                Model.T_Call_RecordSet dModel = dBLL.GetModel(int.Parse(rid.Trim()));
                if (dModel != null)
                {
                    return Success("获取录音设置成功", dModel);
                }
                else
                {
                    return Error("获取录音设置失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //添加录音设置
        public ActionResult saveTelSet(string rid, string telphone, int? type, string des)
        {
            Model.T_Call_RecordSet dModel = new Model.T_Call_RecordSet();
            if (rid != null && rid.Trim() != "")
            {
                dModel = dBLL.GetModel(int.Parse(rid.Trim()));
                if (dModel != null)
                {
                    if (telphone != null && telphone.Trim() != "")
                        dModel.F_Telphone = telphone.Trim();
                    if (type != null)
                        dModel.F_Type = type;
                    if (des != null && des.Trim() != "")
                        dModel.F_Des = des.Trim();

                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Error("编辑失败");
                    }
                }
                else
                {
                    return Error("编辑失败");
                }
            }
            else
            {
                if (telphone != null && telphone.Trim() != "")
                    dModel.F_Telphone = telphone.Trim();
                if (type != null)
                    dModel.F_Type = type;
                if (des != null && des.Trim() != "")
                    dModel.F_Des = des.Trim();
                dModel.F_CreateBy = int.Parse(User.UserData["F_UserID"]);
                dModel.F_CreateOn = DateTime.Now;
                int b = dBLL.Add(dModel);
                if (b > 0)
                {
                    return Success("添加成功");
                }
                else
                {
                    return Error("添加失败");
                }
            }

        }


        //删除号码设置
        public ActionResult DelTelSet(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }
                if (dBLL.DeleteList(idd.TrimEnd(',')))
                {
                    return Success("删除成功");
                }
                else
                    return Error("删除失败");
            }
            else
            {
                return Error("请选择要删除的记录");
            }

        }
    }
}