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.CallTel
{
    public class CallleaveController : BaseController
    {
        //获取留言列表
        public ActionResult GetList(string settime, string removetime, string strtelnum, int status = -1, int page = 1, int pagesize = 10)
        {
            string sql = "";
            DataTable dt = new DataTable();

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

            int recordCount = 0;
            dt = Bll.PagerBll.GetListPager(
                "T_Call_LeaveRecord",
                "F_Id",
                "*",
                sql,
                "ORDER BY F_Id desc",
                pagesize,
                page,
                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)
        {
            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 = User.F_UserName;
                        model.F_UserCode = User.F_UserCode;
                        model.F_UserId = User.F_UserId;

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

        /// <summary>
        /// 获取留言信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetLeaveRecord(int id)
        {
            return Success("获取成功", new Bll.T_Call_LeaveRecord().GetModel(id));
        }



        public ActionResult DealLeaveRecord(string ids, string workorderid)
        {
            if (!string.IsNullOrEmpty(ids))
            {
                Model.T_Call_LeaveRecord model = new Bll.T_Call_LeaveRecord().GetModel(Convert.ToInt32(ids));
                if (model != null)
                {
                    model.F_Status = 1;
                    model.F_DealTime = DateTime.Now;
                    model.F_DealContent = "留言创建工单";
                    model.F_UserName = User.F_UserName;
                    model.F_UserCode = User.F_UserCode;
                    model.F_UserId = User.F_UserId;
                    model.F_WorkOrderId = workorderid;
                    new Bll.T_Call_LeaveRecord().Update(model);
                }

                return Success("处理成功");
            }
            else
            {
                return Error("请选择要处理的留言");
            }
        }

    }
}