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 WorkTimeController : BaseController
    {
        private Bll.T_Sys_SeatGroup group = new Bll.T_Sys_SeatGroup();
        private Bll.T_Sys_WorkTime time = new Bll.T_Sys_WorkTime();
        // GET: WorkTime
        public ActionResult GetList(string groupcode, string key, int pageindex = 1, int pagesize = 10)
        {
            if (User != null)
            {
                #region 查询添加
                DataTable data = new DataTable();
                string sql = $" and F_IsDelte=0";
                if (!string.IsNullOrEmpty(groupcode))
                    sql += $" and F_GroupCode='{groupcode}'";
                if (!string.IsNullOrEmpty(key))
                    sql += $" and F_Remark like '%{key}%'";
                #endregion
                int recordCount = 0;
                data = Bll.PagerBll.GetListPager
                      (
                      "T_Sys_WorkTime aa",
                      "F_Id",
                      "*,(select F_ZXZName from T_Sys_SeatGroup  where F_ZXZCode= aa.F_GroupCode)as F_GroupName",
                      sql,
                      "ORDER BY F_Id desc",
                      pagesize,
                      pageindex,
                      true,
                      out recordCount
                      );
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = data,
                    total = recordCount
                };
                return Content(obj.ToJson());

            }
            return Error("无操作权限！");
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public ActionResult GetModel(int fid)
        {
            if (fid < 0)
                return Error("参数错误！");
            var model = time.GetModel(fid);
            if (model != null && !string.IsNullOrEmpty(model.F_GroupCode))
            {
                model.F_GroupName = group.GetModelList($"F_ZXZCode='{model.F_GroupCode}'").FirstOrDefault()?.F_ZXZName ?? "";
                return Content(model.ToJson());
            }
            else
                return Error("查询失败！");
        }
        /// <summary>
        /// 添加时间
        /// </summary>
        /// <param name="workstart"></param>
        /// <param name="workend"></param>
        /// <param name="groupcode"></param>
        /// <param name="type"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public ActionResult AddOrEditTime(string workstart, string workend, string groupcode, int type, string remark, int id = 0)
        {
            if (User != null)
            {
                if (string.IsNullOrEmpty(workstart))
                    return Error("开始时间不能为空！");
                if (string.IsNullOrEmpty(workend))
                    return Error("结束时间不能为空！");
                if (string.IsNullOrEmpty(groupcode))
                    return Error("请选择坐席组！");
                if (id > 0)
                {
                    var timemodel = time.GetModel(id);
                    if (timemodel != null)
                    {
                        timemodel.F_StartTime = workstart;
                        timemodel.F_EndTime = workend;
                        timemodel.F_GroupCode = groupcode;
                        timemodel.F_Type = type;
                        timemodel.F_Remark = remark;
                        time.Update(timemodel);
                        return Success("修改成功！");
                    }
                    else
                        return Error("修改失败！");
                }
                else
                {
                    Model.T_Sys_WorkTime timemodel = new Model.T_Sys_WorkTime();
                    timemodel.F_StartTime = workstart;
                    timemodel.F_EndTime = workend;
                    timemodel.F_GroupCode = groupcode;
                    timemodel.F_Type = type;
                    timemodel.F_Remark = remark;
                    timemodel.F_CreateBy = User.F_UserCode;
                    timemodel.F_CreateTime = DateTime.Now;
                    timemodel.F_IsDelte = false;
                    time.Add(timemodel);
                    return Success("保存成功！");
                }
            }
            return Error("无操作权限！");

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idstr = string.Join(",", ids);
                if (time.DeleteList(idstr))
                    return Success("删除成功！");
                else return Error("删除失败！");
            }
            else
            {
                return Error("请选择要删除的记录");
            }
        }
    }
}