using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Threading.Tasks;
using CallCenterApi.IRepositories;
using MadRunFabric.Model;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;

namespace CallCenterApi.Controllers.systemmanagement
{
    /// <summary>
    /// 工作日历管理
    /// </summary>
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class SysWorkOFFDaysController : BaseController
    {
        private readonly ILogger<SysWorkOFFDaysController> _logger;
        private readonly ISys_WorkOFFDaysRepository _sys_workoffdaysRepository;

        public SysWorkOFFDaysController(ILogger<SysWorkOFFDaysController> logger, ISys_WorkOFFDaysRepository sys_workoffdaysRepository)
        {
            _logger = logger;
            _sys_workoffdaysRepository = sys_workoffdaysRepository;
        }

        [Authorize]
        [HttpGet("getlist")]
        public async Task<IActionResult> GetListAsync(string strdate)
        {
            try
            {
                //排序字段
                var sort = Builders<Sys_WorkOFFDays>.Sort.Descending("offdate");
                
                //根据条件查询集合
                var flist = new List<FilterDefinition<Sys_WorkOFFDays>>();
                flist.Add(Builders<Sys_WorkOFFDays>.Filter.Eq("isdelete", false));
                flist.Add(Builders<Sys_WorkOFFDays>.Filter.Eq("offstate", 0));

                if (!string.IsNullOrWhiteSpace(strdate))
                {
                    DateTime dt2 = new DateTime();
                    if (DateTime.TryParse(strdate.Trim(), out dt2))
                        flist.Add(Builders<Sys_WorkOFFDays>.Filter.Eq("offdate", strdate));
                }

                var filter = Builders<Sys_WorkOFFDays>.Filter.And(flist);
                var list = await _sys_workoffdaysRepository.Get(filter, null, sort);

                return Success("根据条件获取数据成功", list);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取非工作日历数据异常");
                return Error("获取非工作日历数据失败");
            }
        }

        //设置(非)工作日
        [Authorize]
        [HttpPost("setofforon")]
        public async Task<IActionResult> setOfforOn(string offdate, int type,string id)
        {
            var res = Error("");
            if (!string.IsNullOrWhiteSpace(offdate))
            {
                DateTime dt = new DateTime();
                if (DateTime.TryParse(offdate.Trim(), out dt))
                {
                    try
                    {
                        var nmodel = new Sys_WorkOFFDays();

                        if (type == 0)
                        {//设为休息日
                            nmodel.offdate = dt.ToLocalTime();

                            nmodel.offstate = 0;
                            nmodel.createby = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
                            nmodel.createtime = DateTime.Now.ToLocalTime();
                            nmodel.isdelete = false;
                            bool n = await _sys_workoffdaysRepository.Add(nmodel);
                            if (n)
                            {
                                return Success("设为休息日成功");
                            }
                            else
                            {
                                return Error("设为休息日失败");
                            }
                        }
                        else
                        {//取消休息日
                            if (!string.IsNullOrWhiteSpace(id))
                            {
                                nmodel = await _sys_workoffdaysRepository.GetSingle(id);
                                if (nmodel != null)
                                {
                                    nmodel.isdelete = true;
                                    nmodel.offstate = 1;
                                    if (await _sys_workoffdaysRepository.Update(nmodel))
                                    {
                                        return Success("取消休息日成功");
                                    }
                                }
                            }
                            return Error("取消休息日失败");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "设置休息日异常");
                        return Error("设置失败");
                    }
                }
                else
                {
                    res = Error("日期格式不正确");
                }
            }
            else
            {
                res = Error("获取参数失败");
            }

            return res;
        }
    }
}