using System;
using System.Linq;
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.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class EducationContractController : BaseController
    {
        /// <summary>
        /// 教育合同处理类对象
        /// </summary>
        private readonly EducationContractModule educationContractModule = AOP.AOPHelper.CreateAOPObject<EducationContractModule>();

        #region 合同列表

        /// <summary>
        /// 获取学员信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudentInfo()
        {
            int OrderId = base.ParmJObj.GetInt("OrderId", 0);//订单编号
            int GuestId = base.ParmJObj.GetInt("GuestId", 0);//学员编号
            var model = educationContractModule.GetStudentInfoModule(OrderId, GuestId);
            return ApiResult.Success("", model);
        }

        /// <summary>
        /// 获取合同分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEducationContractPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = new RB_Education_Contract_ViewModel()
            {
                OrderId = base.ParmJObj.GetInt("OrderId"),
                ContractNo = base.ParmJObj.GetStringValue("ContractNo"),
                CType = (ContractTypeEnum)base.ParmJObj.GetInt("CType"),
                Status = base.ParmJObj.GetInt("Status"),
                StudentName = base.ParmJObj.GetStringValue("StudentName"),
                IsAduit=base.ParmJObj.GetInt("IsAduit")
            };
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.CreateBy = userInfo.Id;
            var list = educationContractModule.GetEducationContractPageListModule(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.CType,
                CTypeName = x.CType.ToName(),
                IsStuSign=!string.IsNullOrEmpty(x.Sign)?1:0,
                x.OrderId,
                x.GuestId,
                x.ContractNo,
                x.StudentName,
                x.SchoolName,
                x.CourseName,
                x.CNYCaps,
                x.Money,
                x.Exam,
                x.Sign,
                x.Status,
                x.StatusStr,
                x.IsCompanySeal,
                x.CreateBy,
                x.CreateByName,
                x.CreateByPhoto,
                CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm")
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取合同详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetEducationContractInfo()
        {
            int ContractId = base.ParmJObj.GetInt("ContractId", 0);//合同id
            var model = educationContractModule.GetEducationContractModule(ContractId);
            if (model == null)
            {
                return ApiResult.Failed("合同不存在");
            }
            return ApiResult.Success(data: model);
        }

        /// <summary>
        /// 单独新增合同
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetEducationContractInfo()
        {
            var userInfo = base.UserInfo;
            var dmodel = new RB_Education_Contract_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                CType = (ContractTypeEnum)base.ParmJObj.GetInt("CType"),
                OrderId = base.ParmJObj.GetInt("OrderId"),
                GuestId = base.ParmJObj.GetInt("GuestId"),
                ContractNo = base.ParmJObj.GetStringValue("ContractNo"),
                StudentName = base.ParmJObj.GetStringValue("StudentName"),
                StuBirth = base.ParmJObj.GetDateTime("StuBirth"),
                StuSex = base.ParmJObj.GetInt("StuSex"),
                StuAddress = base.ParmJObj.GetStringValue("StuAddress"),
                StuTel = base.ParmJObj.GetStringValue("StuTel"),
                StuEmail = base.ParmJObj.GetStringValue("StuEmail"),
                ParentName = base.ParmJObj.GetStringValue("ParentName"),
                ParentRelation = base.ParmJObj.GetStringValue("ParentRelation"),
                ParentTel = base.ParmJObj.GetStringValue("ParentTel"),
                ParentEmail = base.ParmJObj.GetStringValue("ParentEmail"),
                UrgentName = base.ParmJObj.GetStringValue("UrgentName"),
                UrgentRelation = base.ParmJObj.GetStringValue("UrgentRelation"),
                UrgentTel = base.ParmJObj.GetStringValue("UrgentTel"),
                UrgentEmail = base.ParmJObj.GetStringValue("UrgentEmail"),
                GuardianIDCard = base.ParmJObj.GetStringValue("GuardianIDCard"),
                CourseName = base.ParmJObj.GetStringValue("CourseName"),
                SchoolName = base.ParmJObj.GetStringValue("SchoolName"),
                SchoolPrincipal = base.ParmJObj.GetStringValue("SchoolPrincipal"),
                StartLevel = base.ParmJObj.GetStringValue("StartLevel"),
                CourseConsultant = base.ParmJObj.GetStringValue("CourseConsultant"),
                Payee = base.ParmJObj.GetStringValue("Payee"),
                FirstClassHours = base.ParmJObj.GetInt("FirstClassHours"),
                FirstCourseFee = base.ParmJObj.GetDecimal("FirstCourseFee"),
                FirstBookFee = base.ParmJObj.GetDecimal("FirstBookFee"),
                FirstClassFee = base.ParmJObj.GetDecimal("FirstClassFee"),
                FirstDiscountMoney = base.ParmJObj.GetDecimal("FirstDiscountMoney"),
                FirstMoney = base.ParmJObj.GetDecimal("FirstMoney"),
                FirstPayDate = base.ParmJObj.GetDateTime("FirstPayDate"),
                SecondClassHours = base.ParmJObj.GetInt("SecondClassHours"),
                SecondCourseFee = base.ParmJObj.GetDecimal("SecondCourseFee"),
                SecondBookFee = base.ParmJObj.GetDecimal("SecondBookFee"),
                SecondClassFee = base.ParmJObj.GetDecimal("SecondClassFee"),
                SecondDiscountMoney = base.ParmJObj.GetDecimal("SecondDiscountMoney"),
                SecondMoney = base.ParmJObj.GetDecimal("SecondMoney"),
                SecondPayDate = base.ParmJObj.GetDateTime("SecondPayDate"),
                ThirdClassHours = base.ParmJObj.GetInt("ThirdClassHours"),
                ThirdCourseFee = base.ParmJObj.GetDecimal("ThirdCourseFee"),
                ThirdBookFee = base.ParmJObj.GetDecimal("ThirdBookFee"),
                ThirdClassFee = base.ParmJObj.GetDecimal("ThirdClassFee"),
                ThirdDiscountMoney = base.ParmJObj.GetDecimal("ThirdDiscountMoney"),
                ThirdMoney = base.ParmJObj.GetDecimal("ThirdMoney"),
                ThirdPayDate = base.ParmJObj.GetDateTime("ThirdPayDate"),
                CNYCaps = base.ParmJObj.GetStringValue("CNYCaps"),
                Money = base.ParmJObj.GetDecimal("Money"),
                Exam = base.ParmJObj.GetStringValue("Exam"),
                IsSupplement = base.ParmJObj.GetInt("IsSupplement"),
            };
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.CreateBy = userInfo.Id;
            dmodel.CreateTime = DateTime.Now;
            dmodel.UpdateBy = userInfo.Id;
            dmodel.UpdateTime = DateTime.Now;
            dmodel.Status = 0;
            bool flag = educationContractModule.SetEducationContractModule(dmodel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 设置合同审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetEducationContractAudit()
        {
            var userInfo = base.UserInfo;
            int ContractId = base.ParmJObj.GetInt("ContractId", 0);//合同id
            int State = base.ParmJObj.GetInt("State", 1);// 状态 1提交审核  2通过  3驳回  4取消  5盖章
            if (ContractId <= 0)
            {
                return ApiResult.ParamIsNull("请传递合同id");
            }
            bool flag = educationContractModule.SetEducationContractAuditModule(ContractId, State, userInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 复制合同
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetEducationContractCopy()
        {
            var userInfo = base.UserInfo;
            int ContractId = base.ParmJObj.GetInt("ContractId", 0);//合同id
            if (ContractId <= 0)
            {
                return ApiResult.ParamIsNull("请传递合同id");
            }
            var model= educationContractModule.GetEducationContractModule(ContractId);
            model.Id = 0;
            model.Group_Id = userInfo.Group_Id;
            model.CreateBy = userInfo.Id;
            model.CreateTime = DateTime.Now;
            model.UpdateBy = userInfo.Id;
            model.UpdateTime = DateTime.Now;
            model.Status = 0;
            bool flag = educationContractModule.SetEducationContractModule(model);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 合同签字
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult SetEducationContractSign()
        {
            int ContractId = base.ParmJObj.GetInt("ContractId", 0);//合同id
            string Sign = base.ParmJObj.GetStringValue("Sign");//签名
            if (ContractId <= 0)
            {
                return ApiResult.ParamIsNull("请传递合同id");
            }
            bool flag = educationContractModule.SetEducationContractSignModule(ContractId, Sign);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 下载pdf
        /// </summary>
        /// <returns></returns>
        public ApiResult GetDownLoadEducationContract()
        {
            string time = DateTime.Now.Ticks.ToString();
            string path = "/upfile/Temporary/" + time + ".pdf";
            string basepath = AppContext.BaseDirectory;
            string tempPath = basepath + "\\upfile\\Temporary\\" + time + ".pdf";
            var ID = base.ParmJObj.GetInt("ContractId");
            var CType = base.ParmJObj.GetInt("CType");
            string curl = Common.Config.UploadSiteUrl + string.Format(@"/Home/EducationContractInfo?ID={0}&CType={1}", ID, CType);
            var flag = new ToPdfHelper().HtmlWJtoPdf(tempPath, curl);
            return ApiResult.Success("", path);
        }
        #endregion
    }
}
