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

namespace Edu.WebApi.Controllers.EduTask
{
    /// <summary>
    /// 流程设置相关接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class EducationReceiptController : BaseController
    {
        /// <summary>
        /// 教务单据处理类对象
        /// </summary>
        private readonly EducationReceiptModule educationReceiptModule = AOP.AOPHelper.CreateAOPObject<EducationReceiptModule>();

        /// <summary>
        /// 获取教务单据分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEducationReceiptPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new EducationReceiptQuery()
            {
                Group_Id = base.UserInfo.Group_Id,
                StartTime = base.ParmJObj.GetStringValue("StartTime"),
                EndTime = base.ParmJObj.GetStringValue("EndTime"),
                Conditon = (EduReceiptConditionEnum)base.ParmJObj.GetInt("Conditon"),
                ReceiptType = base.ParmJObj.GetInt("ReceiptType"),
                Title = base.ParmJObj.GetStringValue("Title"),
                
            };
            if (!string.IsNullOrEmpty(base.ParmJObj.GetStringValue("VerifyStatus")))
            {
                query.VerifyStatus = base.ParmJObj.GetInt("VerifyStatus", -1);
            }
            var id = base.ParmJObj.GetInt("Id");
            if (id > 0)
            {
                query.QIds = id.ToString();
            }
            query.CreateBy = base.UserInfo.Id;
            var list = educationReceiptModule.GetEducationReceiptPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.PageData = list;
            pageModel.Count = rowsCount;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取教务单据当前审核人列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCurrentAuditMan()
        {
            var Id = base.ParmJObj.GetInt("Id");
            var Info = educationReceiptModule.GetCurrentAuditManModule(Id);
            return ApiResult.Success(data: Info);
        }

        /// <summary>
        /// 教务单据作废
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult InvalidReceipt()
        {
            var Id = base.ParmJObj.GetInt("Id");
            var flag = educationReceiptModule.InvalidReceiptModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 修改教务单据当前审核人
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult EditAuditMan()
        {
            List<RB_Education_Record_ViewModel> list = new List<RB_Education_Record_ViewModel>();
            JArray jArray = JArray.Parse(base.ParmJObj.GetStringValue("Info"));
            if (jArray != null && jArray.Count > 0)
            {
                foreach (var item in jArray)
                {
                    JObject jObj = JObject.Parse(item.ToString());
                    RB_Education_Record_ViewModel model = new RB_Education_Record_ViewModel()
                    {
                        Id=jObj.GetInt("Id"),
                        ConditionId=jObj.GetInt("ConditionId"),
                        AuditId=jObj.GetInt("AuditId"),
                        AuditEmId=jObj.GetInt("AuditEmId"),
                        NewAuditEmId=jObj.GetInt("NewAuditEmId"),
                    };
                    list.Add(model);
                }
            }
            var flag = educationReceiptModule.EditAuditManModule(list);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取教务单据类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEduReceiptInfo()
        {
            var Id = base.ParmJObj.GetInt("Id");
            var Info = educationReceiptModule.GetEducationReceiptInfoModule(Id);
            return ApiResult.Success(data: Info);
        }

        /// <summary>
        /// 教务单据审核
        /// </summary>
        /// <param name="model">审核信息</param>
        /// <param name="user">登录用户信息</param>
        /// <param name="message">返回提示信息</param>
        /// <returns></returns>
        public ApiResult AduitEducationReceipt()
        {
            var auditModel = new EduReceiptAudit()
            {
                Id = base.ParmJObj.GetInt("Id"),
                AuditStatus = base.ParmJObj.GetInt("AuditStatus"),
                BackMoney = base.ParmJObj.GetDecimal("BackMoney"),
                Description = base.ParmJObj.GetStringValue("Description"),
                RecipientIds = base.ParmJObj.GetStringValue("RecipientIds"),
                SpecialNode = base.ParmJObj.GetInt("SpecialNode"),
            };
            var flag = educationReceiptModule.AduitEducationReceiptModule(auditModel, base.UserInfo, out string message);
            return flag?ApiResult.Success():ApiResult.Failed(message: message);
        }


        /// <summary>
        /// 获取单据类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEducationReceiptType()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(ReceiptTypeEnum));
            return ApiResult.Success(data: list);
        }
    }
}