using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;

/// <summary>
/// 客服快捷回复
/// </summary>
namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class QuickMsgsController : BaseController
    {
        BLL.kf_quick_msgs busQuickMsgs = new BLL.kf_quick_msgs();
       

        #region 纯视图
        /// <summary>
        /// 回复列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            return View();
        }

        /// <summary>
        /// 添加 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 修改 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            return View();
        }


        /// <summary>
        /// 历史记录
        /// </summary>
        /// <returns></returns>
        public ActionResult GetHistory()
        {
            return View();
        }

        #endregion

        [ActionName("GetListData")]
        public string GetListData(DateTime? NowDateTime,string key = "", int type = -1, int page = 0, int limit = 20)
        {
            string strWhere = string.Format("  service_id in (0,{0})", F_PId.ToInt32());
            if (!string.IsNullOrEmpty(key))
            {
                strWhere += string.Format(" and msg like '%{0}%'", key);
            }
            if (type != -1)
            {
                strWhere += string.Format(" and service_id {0}", type == 1 ? "!=0" : "=0");
            }


            DataTable dt = busQuickMsgs.GetListByPage(strWhere, " service_id asc, rate desc ", (page - 1) * limit, limit).Tables[0];
            int count = busQuickMsgs.GetRecordCount(strWhere);
            return Success("成功", dt, count);
        }

        [ActionName("GetData")]
        public string GettData(int id = 0)
        {
            if (id == 0)
                return Error("请输入ID");

            Model.kf_quick_msgs model = busQuickMsgs.GetModel(id);
            return Success("成功", model, 1);
        }

        [ActionName("adddata")]
        public string AddData(string msg = "", int type = 0,int rate=0)
        {
          
            if (string.IsNullOrEmpty(msg))
            {
                return Error("请输入值msg");
            }
            if (type != 1 && type != 0)
            {
                return Error("请输入值type");
            }

            string strWhere = string.Format("  msg='{0}' and service_id{1} ", msg, type==1 ?"="+F_PId.ToInt32():"=0");
            int count = busQuickMsgs.GetRecordCount(strWhere);
            if (count > 0)
            {
                return Error("已存在");
            }

            Model.kf_quick_msgs model = new kf_quick_msgs();            
            model.service_id = type==0?0:F_PId.ToInt32();            
            model.msg = msg;
            model.rate = rate;

            if (busQuickMsgs.Add(model))
            {
                AddLog("kf_quick_msgs", model.id.ToString(),"添加消息","", model);
                return Success("成功", model, 1);
            }
            else
                return Error("失败");
        }

        [ActionName("editdata")]
        public string EditData(string msg = "",int id=0,int rate=-1)
        {

            if (string.IsNullOrEmpty(msg))
            {
                return Error("请输入值msg");
            }
            if(id==0)
                return Error("请输入值id");

            Model.kf_quick_msgs model = busQuickMsgs.GetModel(id);
            string modeljson = JsonConvert.SerializeObject(model);
            
            model.msg = msg;
            if (rate >= 0)
            {
                model.rate = rate;
            }
            if (busQuickMsgs.Update(model))
            {
                AddLog("kf_quick_msgs", model.id.ToString(), "修改消息", modeljson, model);
                return Success("成功", model, 1);
            }
            else
                return Error("失败");
        }
        [ActionName("deletedata")]
        public string DeleteData(int id = 0)
        {
            Model.kf_quick_msgs model = busQuickMsgs.GetModel(id);
            if (busQuickMsgs.Delete(id))
            {
                AddLog("kf_quick_msgs", model.id.ToString(), "删除消息", JsonConvert.SerializeObject(model), "");
                return Success("删除成功");
            }              
            else
                return Error("失败");
        }
    }
}
