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
{
    [Authority]
    public class WorkcalendarController : BaseController
    {
        // 获取工作日历
        public ActionResult GetCalendarList(string strdate)
        {
            string sql = "";

            if (strdate != null && strdate.Trim() != "")
            {
                BLL.T_Sys_WorkCalendar WorkCalendar = new BLL.T_Sys_WorkCalendar();
                sql = "  DATEDIFF (m,'" + strdate + "',F_DateId )=0 ";
                List<Model.T_Sys_WorkCalendar> modelList = WorkCalendar.GetModelList(sql);
                if (modelList.Count > 0)
                {
                    return Success("日历加载成功", modelList);
                }
                else
                {
                    return Error("日历加载失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //设置(非)工作日
        public ActionResult Setworkingday(string date)
        {
            if (date != null && date != "")
            {
                DateTime dt = new DateTime();
                if (DateTime.TryParse(date, out dt))
                {
                    Model.T_Sys_WorkCalendar wc = new BLL.T_Sys_WorkCalendar().GetModel(dt);
                    if (wc != null)
                    {
                        if (wc.F_EditFlag == false) //没有修改过
                        {
                            if (wc.F_WorkingFlag)
                            {
                                wc.F_WorkingFlag = false;
                            }
                            else
                            {
                                wc.F_WorkingFlag = true;
                            }
                            wc.F_EditFlag = true;
                        }
                        else//修改过 
                        {
                            if (wc.F_WorkingFlag)
                            {
                                wc.F_WorkingFlag = false;
                            }
                            else
                            {
                                wc.F_WorkingFlag = true;
                            }
                            wc.F_EditFlag = false;
                        }
                        bool rvalue = new BLL.T_Sys_WorkCalendar().Update(wc);
                        if (rvalue)
                        {
                            return Success("设置成功！");
                        }
                        else
                        {
                            return Error("设置失败！");
                        }
                    }
                    else
                    {
                        return Error("获取对象失败");
                    }
                }
                else
                {
                    return Error("日期格式不正确");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //获取上班时间段
        public ActionResult GetTimesList()
        {
            string sql = "";
            DataTable dt = new DataTable();
            try
            {

                string keyword = HttpUtility.UrlDecode(RequestString.GetQueryString("keyword"));
                if (keyword.Trim() != "" && keyword != "undefined")
                {
                    sql += " and (convert(varchar,F_WorkEndTimes,120) like '%" + keyword.Trim() + "%' or convert(varchar,F_WorkEndTimes,120) like '%" + keyword.Trim()
                        + "%' or F_TimeName like '%" + keyword.Trim() + "%'or F_Remark like '%" + keyword.Trim() + "%')";
                }

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

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

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

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

                return Content(obj.ToJson());
            }
            catch (Exception ex)
            {
                return Error("上班时间段列表加载失败,错误：" + ex.Message);
            }

        }

        //获取某一条时间段信息
        public ActionResult GetTimes(string id)
        {
            if (id != null && id.Trim() != "")
            {
                BLL.T_Sys_WorkTimes dBLL = new BLL.T_Sys_WorkTimes();
                Model.T_Sys_WorkTimes dModel = dBLL.GetModel(int.Parse(id.Trim()));
                if (dModel != null)
                {
                    return Success("获取成功", dModel);
                }
                else
                {
                    return Error("获取失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //添加/修改上班时间段
        public ActionResult AddTimes(string workid, string workstart, string workend, string timename, string remark)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);

            if (workid != null && workid != "")
            {
                int id = Int32.Parse(workid);
                BLL.T_Sys_WorkTimes dBLL = new BLL.T_Sys_WorkTimes();
                Model.T_Sys_WorkTimes dModel = dBLL.GetModel(id);
                if (dModel != null)
                {
                    if (!string.IsNullOrWhiteSpace(workstart.Trim()))
                    {
                        DateTime dt1 = new DateTime();
                        if (DateTime.TryParse(workstart.Trim(), out dt1))
                            dModel.F_WorkStartTimes = Convert.ToDateTime(dt1.ToString("1900-01-01 HH:mm:ss"));
                    }
                    if (!string.IsNullOrWhiteSpace(workend.Trim()))
                    {
                        DateTime dt1 = new DateTime();
                        if (DateTime.TryParse(workend.Trim(), out dt1))
                            dModel.F_WorkEndTimes = Convert.ToDateTime(dt1.ToString("1900-01-01 HH:mm:ss"));
                    }
                    if (!string.IsNullOrWhiteSpace(timename.Trim()))
                    {
                        dModel.F_TimeName = timename.Trim();
                    }
                    if (!string.IsNullOrWhiteSpace(remark.Trim()))
                    {
                        dModel.F_Remark = remark.Trim();
                    }

                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Error("编辑失败");
                    }
                }
                else
                {
                    return Error("编辑失败");
                }
            }
            else
            {
                Model.T_Sys_WorkTimes _model = new Model.T_Sys_WorkTimes();
                if (!string.IsNullOrWhiteSpace(workstart.Trim()))
                {
                    DateTime dt2 = new DateTime();
                    if (DateTime.TryParse("1900-01-01 " + workstart.Trim(), out dt2))
                        _model.F_WorkStartTimes = Convert.ToDateTime(dt2.ToString("1900-01-01 HH:mm:ss"));
                }
                if (!string.IsNullOrWhiteSpace(workend.Trim()))
                {
                    DateTime dt2 = new DateTime();
                    if (DateTime.TryParse("1900-01-01 " + workend.Trim(), out dt2))
                        _model.F_WorkEndTimes = Convert.ToDateTime(dt2.ToString("1900-01-01 HH:mm:ss"));
                }
                if (!string.IsNullOrWhiteSpace(timename.Trim()))
                {
                    _model.F_TimeName = timename.Trim();
                }
                if (!string.IsNullOrWhiteSpace(remark.Trim()))
                {
                    _model.F_Remark = remark.Trim();
                }
                _model.F_CreateOn = DateTime.Now;
                _model.F_UserID = ua.F_UserId;
                _model.F_UserName = ua.F_UserName;
                _model.F_UserCode = ua.F_UserCode;

                int b = new BLL.T_Sys_WorkTimes().Add(_model);
                if (b > 0)
                {
                    return Success("添加成功");
                }
                else
                {
                    return Error("添加失败");
                }
            }
        }

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

        }
    }
}