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 CallleaveController : BaseController
    {
        //获取留言列表
        public ActionResult GetList(string strtelnum)
        {
            string sql = "";
            DataTable dt = new DataTable();


            string strsettime = HttpUtility.UrlDecode(RequestString.GetQueryString("settime"));
            string strremovetime = HttpUtility.UrlDecode(RequestString.GetQueryString("removetime"));
            string status = HttpUtility.UrlDecode(RequestString.GetQueryString("status"));

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

            if (strtelnum != null && strtelnum.Trim() != "")
            {
                sql += " and F_Phone= '" + strtelnum.Trim() + "' ";
            }
            if (strsettime.Trim() != "" && strsettime != "undefined")
            {
                sql += " and CONVERT(varchar(10),F_LeaveTime, 23) = '" + strsettime.Trim() + "' ";
            }
            if (strremovetime.Trim() != "" && strremovetime != "undefined")
            {
                sql += " and CONVERT(varchar(10),F_DealTime, 23) = '" + strremovetime.Trim() + "' ";
            }
            if (status.Trim() != "-1" && status != "undefined")
            {
                sql += " and F_Status = " + Convert.ToInt32(status);
            }

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

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

            var config = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='PlayLeaveVoice' ").FirstOrDefault();
            foreach (DataRow dr in dt.Rows)
            {
                string path = dr["F_RecFileUrl"] != null ? dr["F_RecFileUrl"].ToString() : "";
                if (path != "" && config != null && !string.IsNullOrEmpty(config.F_ParamValue))
                {
                    dr["F_RecFileUrl"] = config.F_ParamValue + path;
                }
            }

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

            return Content(obj.ToJson());

        }

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

        }

        //更新留言处理状态
        public ActionResult OptLeaveRecord(string[] ids)
        {
            var ua = CurrentUser.UserData;
            if (ids.Length > 0)
            {
                foreach (string arrid in ids)
                {
                    Model.T_Call_LeaveRecord model = new BLL.T_Call_LeaveRecord().GetModel(Convert.ToInt32(arrid));
                    if (model != null)
                    {
                        model.F_Status = 1;
                        model.F_DealTime = DateTime.Now;
                        model.F_DealContent = "1";
                        model.F_UserName = ua.F_UserName;
                        model.F_UserCode = ua.F_UserCode;
                        model.F_UserId = ua.F_UserId;

                        new BLL.T_Call_LeaveRecord().Update(model);
                    }
                }
                return Success("处理成功");
            }
            else
            {
                return Error("请选择要处理的留言");
            }

        }
    }
}