using System;
using System.Collections.Generic;
using System.Common;
using System.IRepositories;
using System.Linq;
using System.Model;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TVShoppingCallCenter_ZLJ.Models.Inputs;

namespace TVShoppingCallCenter_ZLJ.Controllers.Traffic
{
    [Authorize]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class SetWorkOffDayController : BaseController
    {
        private readonly ISys_WorkOffDaysRepository _sys_workoffdaysRepository;
        public SetWorkOffDayController(ISys_WorkOffDaysRepository sys_workoffdaysRepository)
        {
            _sys_workoffdaysRepository = sys_workoffdaysRepository;
        }

        [Authorize]
        [HttpGet("getlist")]
        public async Task<IActionResult> GetListAsync(string strdate,string groupcode)
        {
            List<IConditionalModel> conModels = new List<IConditionalModel>();
            #region 条件筛选
            conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });

            if (!string.IsNullOrWhiteSpace(strdate))
            {
                DateTime dt2 = new DateTime();
                if (DateTime.TryParse(strdate.Trim(), out dt2))
                    conModels.Add(new ConditionalModel() { FieldName = "F_Date", ConditionalType = ConditionalType.Equal, FieldValue = strdate });
            }
            if(!string.IsNullOrWhiteSpace(groupcode))
                conModels.Add(new ConditionalModel() { FieldName = "F_GroupCode", ConditionalType = ConditionalType.Equal, FieldValue = groupcode });

            #endregion

            var list = await _sys_workoffdaysRepository.GetListALL(conModels, "F_Date desc");

            return Success("根据条件获取数据成功", list);
        }

        //设置(非)工作日
        [Authorize]
        [HttpPost("setofforon")]
        public async Task<IActionResult> setOfforOn(string offdate, string groupcode, string remark, int type = 0, int id = 0)
        {
            var res = Error("");
            if (string.IsNullOrWhiteSpace(offdate))
                return Error("获取参数失败");
            DateTime dt = new DateTime();
            if (!DateTime.TryParse(offdate.Trim(), out dt))
                return Error("日期格式不正确");

            var nmodel = new T_Sys_WorkOffDays();

            if (type == 0)
            {//设为休息日
                nmodel.F_Date = offdate;
                nmodel.F_GroupCode = groupcode;
                nmodel.F_Remark = remark;

                nmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
                nmodel.F_CreateOn = DateTime.Now;
                nmodel.F_State = (int)EnumDelState.Enabled;
                var n = await _sys_workoffdaysRepository.Add(nmodel);
                if (n > 0)
                {
                    return Success("设为休息日成功");
                }
                else
                {
                    return Error("设为休息日失败");
                }
            }
            else
            {//取消休息日
                if (id <= 0)
                    return Error("获取参数失败");

                nmodel = await _sys_workoffdaysRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
                if (nmodel == null)
                    return Error("获取信息失败");
                nmodel.F_State = (int)EnumDelState.UnEnabled;
                nmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
                nmodel.F_LastModifyOn = DateTime.Now;
                if (await _sys_workoffdaysRepository.Update(nmodel))
                {
                    return Success("取消休息日成功");
                }
                else
                    return Success("取消休息日失败");
            }

        }
    }
}