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 DutyNumberController : BaseController
    {
        BLL.T_HW_DutyNumber busdutyNumber = new BLL.T_HW_DutyNumber();
        BLL.T_Sys_SystemConfig busConfig = new BLL.T_Sys_SystemConfig();

        /// <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();
        }

        [ActionName("GetListData")]
        public string GetListData(string phone="",string name="",int page=0,int limit = 20)
        {
            AddAction("t_hw_dutynumber", "page", "获取值班电话列表", string.Format("phone={0},name={1}", phone,name));
            string strWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(phone))
            {
                strWhere += string.Format(" and F_DutyNumber='{0}'", phone);
            }
            if (!string.IsNullOrEmpty(name))
            {
                strWhere += string.Format(" and F_Remark like '%{0}%'", name);
            }

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

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

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

        [ActionName("adddata")]
        public string AddData(string phone = "", string name = "")
        {
            string strWhere = " 1=1 ";
            if (string.IsNullOrEmpty(phone))
            {
                return Error("请输入值班电话");
            }
            else {

                    strWhere += string.Format(" and F_DutyNumber='{0}'", phone);
            }

            int count = busdutyNumber.GetRecordCount(strWhere);
            if (count > 0)
            {
                return Error("值班电话已存在");
            }

            Model.T_HW_DutyNumber model = new T_HW_DutyNumber();
            model.F_AddTime = DateTime.Now;
            model.F_AddUser = F_UserCode;
            model.F_DutyNumber = phone;
            model.F_Remark = name;
            model.F_State = 0;
            model.F_Group = "";
            model.F_Groupid = 0;
            int id = busdutyNumber.Add(model);
            if (id > 0)
            {
                AddAction("t_hw_dutynumber", id.ToMyString(), "新增值班电话", phone);
                return Success("成功", model, id);
            }
            else
                return Error("失败");
        }

        [ActionName("editdata")]
        public string EditData(string phone = "", string name = "",int id=0)
        {
            string strWhere = " 1=1 ";
            if (string.IsNullOrEmpty(phone))
            {
                return Error("请输入值班电话");
            }
            else
            {
                strWhere += string.Format(" and F_ID!={1} and F_DutyNumber='{0}'", phone, id);
            }
            if (id==0)
            {
                return Error("请输入id");
            }
            int count = busdutyNumber.GetRecordCount(strWhere);
            if (count > 0)
            {
                return Error("值班电话已存在");
            }
            Model.T_HW_DutyNumber model = busdutyNumber.GetModel(id);

            model.F_AddTime = DateTime.Now;
            model.F_AddUser = F_UserCode;
            model.F_DutyNumber = phone;
            model.F_Remark = name;
            model.F_State = 0;
            model.F_Group = "";
            model.F_Groupid = 0;
            if (busdutyNumber.Update(model))
            {
                AddAction("t_hw_dutynumber", id.ToMyString(), "更新值班电话", phone);
                return Success("成功");
            }
            else
                return Error("失败");
        }
        [ActionName("deletedata")]
        public string DeleteData( int id = 0)
        {
            Model.T_HW_DutyNumber model = busdutyNumber.GetModel(id);
            if (busdutyNumber.Delete(id))
            {
                AddAction("t_hw_dutynumber", id.ToMyString(), "删除值班电话",JsonConvert.SerializeObject(model),"敏感信息");
                AddAction("t_hw_dutynumber", id.ToMyString(), "删除值班电话");
                return Success("删除成功");
            }
            else
                return Error("失败");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns>0留言 1值班电话</returns>
        [ActionName("getstate")]
        public string GetState()
        {
           string value = GetSysconfig("changeflag");
            if (value == "")
            {
                value = "0";
            }
           return Success("成功", value);
        }
        [ActionName("setstate")]
        public string SetState()
        {
            string value = GetSysconfig("changeflag");
            if (value == ""|| value=="0")
            {
                value = "1";
            }
            else
            {
                value = "0";
            }
            if (busConfig.UpdateValueByCode(value))
            {
                return Success("成功", value);
            }
            return Error("失败");           
        }
    }
}
