using Edu.Common.API;
using Edu.Common.Enum.Course;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.EduTask;
using Edu.Module.Course;
using Edu.Module.EduTask;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Edu.WebApi.Controllers.Applet
{
    /// <summary>
    /// 改变流程相关申请
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class LeaveStudentController : AppletBaseController
    {
        /// <summary>
        /// 学生请假处理类对象
        /// </summary>
        private readonly LeaveStudentModule leaveStudentModule = AOP.AOPHelper.CreateAOPObject<LeaveStudentModule>();

        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = AOP.AOPHelper.CreateAOPObject<ClassModule>();
        /// <summary>
        /// 新增修改学生请假
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStudentLeave()
        {
            var model = new RB_Student_Leave_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                ClassId = base.ParmJObj.GetInt("ClassId"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                OrderGuestId = base.ParmJObj.GetInt("OrderGuestId"),
                Remarks = base.ParmJObj.GetStringValue("Remarks"),
                LeaveType = base.ParmJObj.GetInt("LeaveType")
            };
            var classModel = classModule.GetClassModule(model.ClassId);
            string receiptFile = string.Empty;
            try
            {

                List<LeaveInfoModel> LeaveInfoList = Common.Plugin.JsonHelper.DeserializeObject<List<LeaveInfoModel>>(base.ParmJObj.GetStringValue("LeaveInfoList"));
                if (LeaveInfoList == null || !LeaveInfoList.Any())
                {
                    return ApiResult.Failed("请选择请假时间");
                }
                else
                {
                    model.LeaveInfo = JsonHelper.Serialize(LeaveInfoList);
                    model.ClassTimeIds = string.Join(",", LeaveInfoList.Select(x => x.ClassTimeId));
                }

                List<string> receiptFileList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(base.ParmJObj.GetStringValue("ReceiptFileList"));
                if (receiptFileList == null || !receiptFileList.Any())
                {
                    receiptFile = "";
                    // return ApiResult.Failed("附件必须上传");
                }
                else
                {
                    receiptFile = JsonHelper.Serialize(receiptFileList);
                }
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "SetStudentLeave");
            }


            if (model.Id == 0)
            {
                model.CreateBy = base.AppletUserInfo.Id;
                model.CreateTime = DateTime.Now;
                model.Group_Id = base.AppletUserInfo.Group_Id;
                model.School_Id = classModel.School_Id;
            }
            model.UpdateBy = base.AppletUserInfo.Id;
            model.UpdateTime = DateTime.Now;
            bool flag = leaveStudentModule.SetStudentLeaveModule(model, receiptFile);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }




        public ApiResult TestUpdateClassPlan()
        {

            int id = base.ParmJObj.GetInt("Id");
            var result = leaveStudentModule.UpdateChangeClassPlan(id);
            return ApiResult.Success("");
        }
    }
}
