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

namespace Edu.WebApi.Controllers.Course
{
    /// <summary>
    /// 学员相关
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class StuController : BaseController
    {
        /// <summary>
        /// 学员单据处理类对象
        /// </summary>
        private readonly StudentBillModule studentBillModule = AOP.AOPHelper.CreateAOPObject<StudentBillModule>();

        private readonly OrderModule orderModule= AOP.AOPHelper.CreateAOPObject<OrderModule>();

        /// <summary>
        /// 获取学员退课单据分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudentBackClassPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取退款单据审核信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetBackBillAduit()
        {
            //退课单据号
            var BackId = base.ParmJObj.GetInt("BackId");
            //审核状态
            var AuditStatus = base.ParmJObj.GetInt("AuditStatus");
            //审核备注
            string Description = base.ParmJObj.GetStringValue("Description");
            //退课金额
            decimal BackMoney= base.ParmJObj.GetDecimal("BackMoney");
            //是否是特殊节点
            var SpecialNode = base.ParmJObj.GetInt("SpecialNode");
            //抄送人
            string RecipientIds = base.ParmJObj.GetStringValue("RecipientIds");


           // var flag = studentBillModule.SetBackBillAduitModule(BackId, AuditStatus, Description, base.UserInfo, out string message, SpecialNode: SpecialNode, BackMoney: BackMoney, RecipientIds: RecipientIds);
            //return flag ? ApiResult.Success(message: message) : ApiResult.Failed(message: message);

            return ApiResult.Failed();
        }

        /// <summary>
        /// 获取学员预计退课金额
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetBackBillMoney()
        {
            //退课单据号
            var BackId = base.ParmJObj.GetInt("BackId");
            var data = studentBillModule.GetBackBillMoneyModule(BackId);
            return ApiResult.Success(data: data);
        }

        /// <summary>
        /// 退学  一键生成财务单据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStudentBackFinance()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int BackClassId = parms.GetInt("BackClassId", 0);
            int IsPublic = parms.GetInt("IsPublic", 0);
            int CurrencyId = parms.GetInt("CurrencyId", 0);
            int ClientType = parms.GetInt("ClientType", 0);
            int ClientId = parms.GetInt("ClientId", 0);
            if (BackClassId <= 0)
            {
                return ApiResult.ParamIsNull(message:"请传递退课编号!");
            }
            if (ClientId <= 0)
            {
                return ApiResult.ParamIsNull(message:"请传递客户编号!");
            }
            string msg = studentBillModule.SetStudentBackFinance(BackClassId, IsPublic, CurrencyId, ClientType, ClientId, userInfo);
            return string.IsNullOrEmpty(msg) ? ApiResult.Success() : ApiResult.Failed(msg);
        }

        /// <summary>
        /// 学员续费订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RenewOrder()
        {
            //订单编号
            int OrderId = base.ParmJObj.GetInt("OrderId");
            //学员编号
            int guestId = base.ParmJObj.GetInt("GuestId");
            var oldOrderModel = orderModule.GetClassOrderInfoModule(OrderId);
            var orderModel = new RB_Order_ViewModel()
            {
                OrderId=0,
                ClassId = base.ParmJObj.GetInt("ClassId"),//班级编号
                GuestNum = 1,
                OrderSource= oldOrderModel.OrderSource,
                OrderType=oldOrderModel.OrderType,
                Class_Price = base.ParmJObj.GetDecimal("Class_Price"),//单价
                Unit_Price = base.ParmJObj.GetDecimal("Class_Price"),//成交单价
                PreferPrice = base.ParmJObj.GetDecimal("PreferPrice"),//应收，
                Income = 0,
                HelpEnterId=0,
                GeneralOccupation="",
                EduOccupation="",
                SaleRemark =base.ParmJObj.GetStringValue("SaleRemark"),//销售备注
                SourceId=0,
                IsLessPrice=0,
                LessPrice=0,
                OrderNature=oldOrderModel.OrderNature,
                OldPreferPrice = base.ParmJObj.GetDecimal("PreferPrice"),//应收，
                CourseId=0,
                StartClassHours=0,
                IsChaBan=0,
                UpOrderId=0,
                VisitorReserveId=0,
                IsRenewOrder=1,
            };
            orderModel.RenewOrderId = OrderId;
            orderModel.Group_Id = base.UserInfo.Group_Id;
            orderModel.School_Id = base.UserInfo.School_Id;
            orderModel.Dept_Id = base.UserInfo.DeptId;
            orderModel.OrderForm = OrderFormEnum.Computer;
            orderModel.EnterID = base.UserInfo.Id;
            orderModel.OrderState = OrderStateEnum.Normal;
            orderModel.TradeWay = TradeWayEnum.OnLine;
            orderModel.CreateBy = base.UserInfo.Id;
            orderModel.CreateTime = DateTime.Now;
            orderModel.UpdateBy = base.UserInfo.Id;
            orderModel.UpdateTime = DateTime.Now;
            orderModel.OldGuestId = guestId;
            bool flag = orderModule.SetClassOrderModule(orderModel, base.UserInfo, base.CheckUserActionAuth("Edit_Order"), out string message);
            return flag ? ApiResult.Success() : ApiResult.Failed(message: message);
        }

        /// <summary>
        /// 获取续费班级列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetRenewClass()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Class_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
               //School_Id = base.ParmJObj.GetInt("School_Id"),
                ClassType = base.ParmJObj.GetInt("ClassType"),
                StartTime = base.ParmJObj.GetStringValue("StartTime"),
                EndTime = base.ParmJObj.GetStringValue("EndTime"),
                TeacherName = base.ParmJObj.GetStringValue("TeacherName"),
                CouseId = base.ParmJObj.GetInt("CouseId"),
                OldClassId = base.ParmJObj.GetInt("ClassId"),
                QClassStatus = "1,2"
            };
            var obj = studentBillModule.GetRenewClassModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = Convert.ToInt32(rowsCount);
            pageModel.PageData = obj;
            return ApiResult.Success(data: pageModel);
        }
    }
}
