using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers.System
{
    public class SeatGroupController : BaseController
    {
        // GET: SeatGroup
        Bll.T_Sys_SeatGroup seatBLL = new Bll.T_Sys_SeatGroup();
        /// <summary>
        /// 获取坐席组列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList(string keyword, int page = 1, int pagesize = 10)
        {
            DataTable dt = new DataTable();
            string sql = "";
            if (!string.IsNullOrEmpty(keyword))
            {
                sql += $"and (F_ZXZCode like '%" + keyword.Trim() + "%' or F_ZXZName like '%" + keyword.Trim() + "%' or F_ZXAtt like '%" + keyword.Trim() + "%')";
            }
            int recordCount = 0;
            dt = Bll.PagerBll.GetListPager(
                "T_Sys_SeatGroup",
                "F_ID",
                "*,dbo.GetUserNameNew(F_CreateUser) F_CreateName ",
                sql,
                "ORDER BY F_ID desc",
                pagesize,
                page,
                true,
                out recordCount);

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

            return Content(obj.ToJson());
        }

        //获取坐席组列表
        public ActionResult GetSeatList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                List<Model.T_Sys_SeatGroup> seatList = seatBLL.GetModelList(" 1=1 order by F_ID desc ");
                if (seatList.Count > 0)
                    res = Success("坐席组列表加载成功", seatList);
                else
                    res = Error("坐席组列表加载失败");
            }
            return res;
        }

        /// <summary>
        /// 获取坐席组
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetSeatGroup(int ZXZID = 0)
        {
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(ZXZID);
                if (sModel != null)
                    return Success("获取坐席组信息成功", sModel);
                return Error("获取坐席组信息失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 添加坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddSeatGroup(string ZXZCode, string ZXZName, string ZXAtt, string WHWDKey, string WHBDKey, int IVRKey, string Des)
        {
            int n = seatBLL.GetRecordCount(" (F_ZXZCode='" + ZXZCode + "' or F_ZXZName='" + ZXZName + "')");
            if (Int32.Parse(n.ToString()) > 0)
            {
                return Error("坐席组编号和名称必须唯一，请重新输入！");
            }

            Model.T_Sys_SeatGroup sModel = new Model.T_Sys_SeatGroup();

            sModel.F_ZXZCode = ZXZCode.Trim();
            sModel.F_ZXZName = ZXZName.Trim();
            sModel.F_ZXAtt = ZXAtt.Trim();
            sModel.F_WHWDKey = WHWDKey.Trim();
            sModel.F_WHBDKey = WHBDKey.Trim();
            sModel.F_IVRKey = IVRKey;
            if (Des != null)
                sModel.F_Des = Des.Trim();

            sModel.F_CreateTime = DateTime.Now;
            sModel.F_CreateUser = User.F_UserCode;

            if (seatBLL.Add(sModel) > 0)
                return Success("添加成功");
            else
                return Error("添加失败");
        }

        /// <summary>
        /// 编辑坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditSeatGroup(int ZXZID, string ZXZCode, string ZXZName, string ZXAtt, string WHWDKey, string WHBDKey, int IVRKey, string Des)
        {
            if (ZXZID <= 0)
                return Error("请选择要编辑的坐席组");
            Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(ZXZID);
            if (sModel == null)
                return Error("获取信息失败");

            int n = seatBLL.GetRecordCount(" F_ID!='" + ZXZID + "' and (F_ZXZCode='" + ZXZCode + "' or F_ZXZName='" + ZXZName + "')");
            if (Int32.Parse(n.ToString()) > 0)
            {
                return Error("坐席组编号和名称必须唯一，请重新输入！");
            }

            sModel.F_ZXZCode = ZXZCode.Trim();
            sModel.F_ZXZName = ZXZName.Trim();
            sModel.F_ZXAtt = ZXAtt.Trim();
            sModel.F_WHWDKey = WHWDKey.Trim();
            sModel.F_WHBDKey = WHBDKey.Trim();
            sModel.F_IVRKey = IVRKey;
            sModel.F_Des = Des.Trim();

            if (seatBLL.Update(sModel))
                return Success("编辑成功");
            return Error("编辑失败");
        }

        /// <summary>
        /// 删除坐席组
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelSeatGroup(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("请选择要删除的坐席组");

            var idStr = string.Join(",", ids);
            if (string.IsNullOrEmpty(idStr.Trim()))
                return Error("请选择要删除的坐席组");

            if (seatBLL.DeleteList(idStr))
                return Success("删除成功");
            return Error("删除失败");
        }
    }
}