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 CallrecordsController : BaseController
    {
        //获取通话记录列表
        public ActionResult GetList(string phone, string usercode)
        {
            string sql = "";
            DataTable dt = new DataTable();
            int roleid = CurrentUser.UserData.F_RoleId;
            int deptid = CurrentUser.UserData.F_DeptId;
            string deptcode = CurrentUser.UserData.F_DeptCode;
            string callstate = HttpUtility.UrlDecode(RequestString.GetQueryString("callstate"));
            string calltype = HttpUtility.UrlDecode(RequestString.GetQueryString("calltype"));
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string dept = HttpUtility.UrlDecode(RequestString.GetQueryString("dept"));

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

            if (roleid != 0)
            {
                if (roleid != 17)
                {
                    usercode = CurrentUser.UserData.F_UserCode;
                    sql += " and UserCode='" + usercode + "'";
                }
            }
            #region
            //if (roleid != 0)
            //{
            //    if (roleid != 17)
            //    {
            //        //if (deptcode.IndexOf("|533|") > 0)
            //        //{
            //        //    sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + deptid + ")";
            //        //}
            //        if (deptcode.IndexOf("|531|") > 0)
            //        {
            //            if (roleid == 32)
            //            {
            //                usercode = CurrentUser.UserData.F_UserCode;
            //                sql += " and UserCode='" + usercode + "'";
            //            }
            //            else
            //            {
            //                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptID in (select F_DeptId from T_Sys_Department where F_DeptCode like '" + deptcode + "%')) ";
            //            }
            //        }
            //        else
            //        {
            //            sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + deptid + ")";
            //        }
            //    }
            //}
            #endregion

            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and UserCode='" + usercode + "'";
            }
            if (dept != null && dept.Trim() != "" && Convert.ToInt32(dept) > 0)
            {
                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dept.Trim() + ")";
            }
            if (phone != null && phone.Trim() != "")
            {
                sql += " and CallNumber like '%" + phone + "%'";
            }
            if (callstate.Trim() != "")
            {
                sql += " and CallState='" + callstate + "'";
            }
            if (calltype.Trim() != "")
            {
                sql += " and CallType='" + calltype + "'";
            }
            if (starttime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
            }
            if (endtime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
            }

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

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

            var config = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='PlayPath' ").FirstOrDefault();
            var users = new BLL.T_Sys_UserAccount().GetModelList("");
            foreach (DataRow dr in dt.Rows)
            {
                string path = dr["FilePath"] != null ? dr["FilePath"].ToString() : "";
                if (path != "" && config != null && !string.IsNullOrEmpty(config.F_ParamValue))
                {
                    var ym = config.F_ParamValue;
                    if (ym.Substring(ym.Length - 1) == "/")
                    {
                        ym = ym.Substring(0, ym.Length - 1);
                    }
                    dr["FilePath"] = ym + path.Substring(path.IndexOf(':') + 1).Replace('\\', '/');
                }
                string ucode = dr["UserCode"] != null ? dr["UserCode"].ToString() : "";
                if (ucode != "")
                {
                    var uu = users.Where(u => u.F_UserCode.Equals(ucode)).FirstOrDefault();
                    if (uu != null)
                        dr["UserName"] = uu.F_UserName;
                }
            }

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

            return Content(obj.ToJson());
        }

        //通话记录导出
        public ActionResult GetListExpt(string phone, string usercode)
        {
            string sql = " 1=1 ";
            DataTable dt = new DataTable();

            int userid = CurrentUser.UserData.F_UserId;
            int roleid = CurrentUser.UserData.F_RoleId;
            int deptid = CurrentUser.UserData.F_DeptId;
            string deptcode = CurrentUser.UserData.F_DeptCode;
            string callstate = HttpUtility.UrlDecode(RequestString.GetQueryString("callstate"));
            string calltype = HttpUtility.UrlDecode(RequestString.GetQueryString("calltype"));
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string dept = HttpUtility.UrlDecode(RequestString.GetQueryString("dept"));
            if (roleid != 0)
            {
                if (roleid != 17)
                {
                    usercode = CurrentUser.UserData.F_UserCode;
                    sql += " and UserCode='" + usercode + "'";
                }
            }
            #region
            //if (roleid != 0)
            //{
            //    if (roleid != 17)
            //    {
            //        //if (deptcode.IndexOf("|533|") > 0)
            //        //{
            //        //    sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + deptid + ")";
            //        //}
            //        if (deptcode.IndexOf("|531|") > 0)
            //        {
            //            if (roleid == 32)
            //            {
            //                usercode = CurrentUser.UserData.F_UserCode;
            //                sql += " and UserCode='" + usercode + "'";
            //            }
            //            else
            //            {
            //                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptID in (select F_DeptId from T_Sys_Department where F_DeptCode like '" + deptcode + "%')) ";
            //            }
            //        }
            //        else
            //        {
            //            sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + deptid + ")";
            //        }
            //    }
            //}
            #endregion
            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and UserCode='" + usercode + "'";
            }
            if (dept != null && dept.Trim() != "")
            {
                sql += " and UserCode in (select F_UserCode from T_Sys_UserAccount where F_DeptId=" + dept.Trim() + ")";
            }
            if (phone != null && phone.Trim() != "")
            {
                sql += " and CallNumber like '%" + phone + "%'";
            }
            if (callstate.Trim() != "")
            {
                sql += " and CallState='" + callstate + "'";
            }
            if (calltype.Trim() != "")
            {
                sql += " and CallType='" + calltype + "'";
            }
            if (starttime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
            }
            if (endtime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
            }

            dt = new BLL.T_Call_CallRecords().GetListExpt(sql).Tables[0];

            NPOIHelper npoi = new NPOIHelper();
            if (npoi.ExportToExcel("通话记录数据", dt, col()) == "")
            {
                return Success("导出成功");
            }
            else
            {
                return Error("导出失败");
            }

        }

        private string[] col()
        {
            string[] ccc = {
                   "电话号码","呼叫方向","呼叫状态","坐席工号","坐席姓名",
                  "开始时间","通话开始时间","通话结束时间","结束时间","通话时长"
            };
            //string[] ccc = {
            //       "电话号码","呼叫方向","呼叫状态","是否处理","坐席工号","坐席姓名",
            //      "开始时间","通话开始时间","通话结束时间","结束时间","通话时长"
            //};
            return ccc;
        }

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

        }
    }
}