using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    public class SeatGroupController : BaseController
    {
        BLL.T_Sys_SeatGroup seatBLL = new BLL.T_Sys_SeatGroup();
        /// <summary>
        /// 获取坐席组列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            
                DataTable dt = new DataTable();
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
            string strcontent = RequestString.GetQueryString("content");
            
            string sql = "";
            if (strcontent!=null&&strcontent != "")
            {
                sql = " and( F_ZXZCode like '" + strcontent + "' or F_ZXZName  like '%" + strcontent + "%'  or F_WHBDKey like '" + strcontent + "%'  or F_WHWDKey  like '" + strcontent + "%'  or F_ZXAtt like '" + strcontent + "'  or F_Des like '" + strcontent + "')";
            }
                if (strpageindex.Trim() != "")
                {
                    pageindex = Convert.ToInt32(strpageindex);
                }

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

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

                res = Content(obj.ToJson());
         
            return res;
        }

        //获取坐席组列表
        public ActionResult GetSeatList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                List<Model.T_Sys_SeatGroup> seatList = seatBLL.GetModelList(" 1=1 order by F_ZXZID 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);
                string Region="";
                if (sModel != null)
                {
                    if (!string .IsNullOrEmpty (sModel.F_Region) )
                    {
                        string[] mRegion = sModel.F_Region.Split(',');
                        if (mRegion.Length >0)
                        {
                            foreach (var it in mRegion)
                            {
                                    try
                                    {
                                    if (int.Parse(it) < 32)
                                    {
                                        var model = new BLL.T_RegionCategory().GetModel(int.Parse(it));
                                        if (model != null)
                                        {
                                            if (Region == "")
                                                Region = model.F_RegionName;
                                            else
                                                Region = Region + "," + model.F_RegionName;
                                        }
                                      }
                                   }
                                    catch
                                    {

                                    }
                                   
                               
                               
                            }
                        }
                    }
                    var obj = new
                    {
                        sModel,
                        Region
                    };
                    return Success("获取坐席组信息成功",
                         obj);
                }
                  
                return Error("获取坐席组信息失败");
            }
            return NoToken("未知错误，请重新登录");
        }
        /// <summary>
        /// 验证code是否唯一
        /// </summary>
        private bool getunphone( string ZXZCode)
        {
            var sql = " ";
            sql += " (F_ZXZCode='" + ZXZCode + "')";
            var count = seatBLL.GetModelList(sql).Count();
            return count > 0;
        }
        /// <summary>
        /// 添加坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddSeatGroup(SeatGroupInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_SeatGroup sModel = new Model.T_Sys_SeatGroup();
                if (input.ZXZCode != null)
                {
                    if (getunphone( input.ZXZCode))
                        return Error("已存在该编号勿重复添加");
                    else
                        sModel.F_ZXZCode = input.ZXZCode.Trim();
                }
                else
                {
                    res = Error("编号不能为空");
                    return res;
                }
                if (input.ZXZName != null)
                    sModel.F_ZXZName = input.ZXZName.Trim();
                else
                {
                    res = Error("名称不能为空");
                    return res;
                }
                if (input.ZXAtt != null)
                    sModel.F_ZXAtt = input.ZXAtt.Trim();
                else
                {
                    res = Error("坐席地区号不能为空");
                    return res;
                }
                if (input.WHWDKey != null)
                    sModel.F_WHWDKey = input.WHWDKey.Trim();
                else
                {
                    res = Error("外呼外地前缀不能为空");
                    return res;
                }
                if (input.WHBDKey != null)
                    sModel.F_WHBDKey = input.WHBDKey.Trim();
                else
                {
                    res = Error("外呼本地前缀不能为空");
                    return res;
                }
                if (input.IVRKey != null)
                    sModel.F_IVRKey = input.IVRKey;
                if (input.Des != null)
                    sModel.F_Des = input.Des.Trim();
                if (input.Region  != null)
                    sModel.F_Region = input.Region.Trim();
                if (!Validate.IsNumber(input.WHWDKey.ToString()))
                {
                    res = Error("外呼外地前缀必须为数字");
                    return res;
                }
                if (!Validate.IsNumber(input.WHBDKey.ToString()))
                {
                    res = Error("外呼本地前缀必须为数字");
                    return res;
                }
               

                sModel.F_CreateTime = DateTime.Now;
                sModel.F_CreateByName = CurrentUser.UserData.F_UserName;
                sModel.F_CreateBy = CurrentUser.UserData.F_UserId;

                if (seatBLL.Add(sModel) > 0)
                    res = Success("添加成功");
                else
                    res = Error("添加失败");//res = Success("添加失败");//2017-11-15
            }
            return res;
        }

        /// <summary>
        /// 编辑坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditSeatGroup(SeatGroupInput input)
        {
            if (Request.IsAuthenticated)
            {
                if (input.ZXZID <= 0)
                    return Error("请选择要编辑的坐席组");

                Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(input.ZXZID);
                if (sModel == null)
                    return Error("获取信息失败");

                if (input.ZXZCode != null)
                    sModel.F_ZXZCode = input.ZXZCode.Trim();
                if (input.ZXZName != null)
                    sModel.F_ZXZName = input.ZXZName.Trim();
                if (input.ZXAtt != null)
                    sModel.F_ZXAtt = input.ZXAtt.Trim();
                if (input.WHWDKey != null)
                    sModel.F_WHWDKey = input.WHWDKey.Trim();
                if (input.WHBDKey != null)
                    sModel.F_WHBDKey = input.WHBDKey.Trim();
                if (input.IVRKey != null)
                    sModel.F_IVRKey = input.IVRKey;
                if (input.Des != null)
                    sModel.F_Des = input.Des.Trim();
                if (input.Region != null)
                    sModel.F_Region = input.Region.Trim();
                if (seatBLL.Update(sModel))
                    return Success("编辑成功");
                return Error("编辑失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 删除坐席组
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelSeatGroup(string[] ids)
        {
            if (Request.IsAuthenticated)
            {
                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("删除失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        #region 按内容搜索
        /// <summary>
        /// 获取坐席组
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetSearch()
        {
            if (Request.IsAuthenticated)
            {
                string strcontent= RequestString.GetQueryString("content");
                string strwhere = "";
                if (strcontent != "")
                {
                    strwhere = " F_ZXZCode='" + strcontent + "' or F_ZXZName='" + strcontent + "'";
                }
                DataSet ds = seatBLL.GetList(strwhere);
                if (ds != null)
                    return Success("查询坐席组信息成功", ds);
                return Error("查询坐席组信息失败");
            }
            return NoToken("未知错误，请重新登录");
        }
        #endregion
    }
}