using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Reserve;
using Edu.Module.Duty;
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.Duty
{
    /// <summary>
    /// 访客约课控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class VisitorReserveController : BaseController
    {
        /// <summary>
        /// 访客约课处理类对象
        /// </summary>
        private readonly VisitorReserveModule visitorReserveModule = AOP.AOPHelper.CreateAOPObject<VisitorReserveModule>();

        #region 访客约课

        /// <summary>
        /// 获取访客约课分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetVisitorReservePage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Visitor_Reserve_Extend()
            {
                Group_Id = base.UserInfo.Group_Id,
                StartClassDate=base.ParmJObj.GetStringValue("StartClassDate"),
                EndClassDate=base.ParmJObj.GetStringValue("EndClassDate"),
                TeacherId=base.ParmJObj.GetInt("TeacherId"),
                ClassRoomId=base.ParmJObj.GetInt("ClassRoomId"),
                Q_ReserveStatus=base.ParmJObj.GetStringValue("Q_ReserveStatus"),
                VisitorName=base.ParmJObj.GetStringValue("VisitorName"),
                Id=base.ParmJObj.GetInt("Id"),
                IsQueryAll=base.ParmJObj.GetInt("IsQueryAll"),
                Visitor_Id=base.ParmJObj.GetInt("Visitor_Id"),
                ReserveClassId=base.ParmJObj.GetInt("ReserveClassId"),
            };
            if (query.IsQueryAll != 1)
            {
               // query.CreateBy = base.UserInfo.Id;
            }
            var list = visitorReserveModule.GetVisitorReservePageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
            }
            pageModel.PageData = list;
            pageModel.Count = rowsCount;
            return ApiResult.Success(data: pageModel);
        }


        /// <summary>
        /// 获取访客约课列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetVisitorReserveList()
        {
            var query = new RB_Visitor_Reserve_Extend()
            {
                Group_Id = base.UserInfo.Group_Id,
            };
            var list = visitorReserveModule.GetVisitorReserveListModule(query);
            foreach (var item in list)
            {

            }
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 新增修改访客约课
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetVisitorReserve()
        {
            var model = new RB_Visitor_Reserve_Extend()
            {
                Id = base.ParmJObj.GetInt("Id"),
                Visitor_Id = base.ParmJObj.GetInt("Visitor_Id"),
                Remark = base.ParmJObj.GetStringValue("Remark"),
                ReserveClassId = base.ParmJObj.GetInt("ReserveClassId"),
                ClassDate=base.ParmJObj.GetDateTime("ClassDate"),
                ClassTime = base.ParmJObj.GetStringValue("ClassTime"),
                TeacherId=base.ParmJObj.GetInt("TeacherId"),
                ClassRoomId=base.ParmJObj.GetInt("ClassRoomId"),
                ClassContent = base.ParmJObj.GetStringValue("ClassContent"),
                EndTime=base.ParmJObj.GetStringValue("EndTime"),
                TrialLessonId=base.ParmJObj.GetInt("TrialLessonId"),
            };
            if (string.IsNullOrEmpty(model.ClassTime))
            {
                return ApiResult.ParamIsNull("请选择预约时间段!");
            }
            model.ReserveStatus = 0;
            model.Group_Id = base.UserInfo.Group_Id;
            model.School_Id = base.UserInfo.School_Id;
            model.CreateBy = base.UserInfo.Id;
            model.CreateTime = System.DateTime.Now;
            model.UpdateBy = base.UserInfo.Id;
            model.UpdateTime = System.DateTime.Now;
            model.Status = Common.Enum.DateStateEnum.Normal;
            bool retult = visitorReserveModule.SetVisitorReserveModule(model,out string message);
            return retult ? ApiResult.Success() : ApiResult.Failed(message: message);
        }

        /// <summary>
        /// 根据编号获取访客约课详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetVisitorReserve()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var model = visitorReserveModule.GetVisitorReserveModule(Id);
            return ApiResult.Success(data: model);
        }

        /// <summary>
        /// 删除访客约课
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveVisitorReserve()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var flag = visitorReserveModule.RemoveVisitorReserveModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 更新访客状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult UpdateVisitorReserveStatus()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var ReserveStatus = base.ParmJObj.GetInt("ReserveStatus", 0);
            string Remark = base.ParmJObj.GetStringValue("Remark");
            var flag = visitorReserveModule.UpdateVisitorReserveStatusModule(Id, ReserveStatus, Remark);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

        #region 约课班级

        /// <summary>
        /// 获取约课班级分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetReserveClassPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Reserve_Class_Extend()
            {
                Group_Id = base.UserInfo.Group_Id,
                StartClassDate=base.ParmJObj.GetStringValue("StartClassDate"),
                EndClassDate=base.ParmJObj.GetStringValue("EndClassDate"),
                TeacherId=base.ParmJObj.GetInt("TeacherId"),
                ClassRoomId=base.ParmJObj.GetInt("ClassRoomId"),
                LessonName=base.ParmJObj.GetStringValue("LessonName"),
            };
            var list = visitorReserveModule.GetReserveClassPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";

                }
            }
            pageModel.PageData = list;
            pageModel.Count = rowsCount;
            return ApiResult.Success(data: pageModel);
        }


        /// <summary>
        /// 获取约课班级列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetReserveClassList()
        {
            var query = new RB_Reserve_Class_Extend()
            {
                Group_Id = base.UserInfo.Group_Id,
            };
            var list = visitorReserveModule.GetReserveClassListModule(query);
            foreach (var item in list)
            {

            }
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 新增修改约课班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetReserveClass()
        {
            var model = new RB_Reserve_Class_Extend()
            {
                ReserveClassId = base.ParmJObj.GetInt("ReserveClassId"),
                TeacherId = base.ParmJObj.GetInt("TeacherId"),
                ClassDate = base.ParmJObj.GetDateTime("ClassDate"),
                ClassTime = base.ParmJObj.GetStringValue("ClassTime"),
                ClassRoomId = base.ParmJObj.GetInt("ClassRoomId"),
                ClassContent = base.ParmJObj.GetStringValue("ClassContent"),
                EndTime=base.ParmJObj.GetStringValue("EndTime"),
                TrialLessonId=base.ParmJObj.GetInt("TrialLessonId"),
            };
            if (string.IsNullOrEmpty(model.ClassTime))
            {
                return ApiResult.ParamIsNull("请选择预约时间!");
            }
            model.Group_Id = base.UserInfo.Group_Id;
            model.School_Id = base.UserInfo.School_Id;
            model.CreateBy = base.UserInfo.Id;
            model.CreateTime = System.DateTime.Now;
            model.UpdateBy = base.UserInfo.Id;
            model.UpdateTime = System.DateTime.Now;
            model.Status = Common.Enum.DateStateEnum.Normal;
            bool retult = visitorReserveModule.SetReserveClassModule(model,out string message);
            return retult ? ApiResult.Success() : ApiResult.Failed(message: message);
        }

        /// <summary>
        /// 根据编号获取约课班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetReserveClass()
        {
            var ReserveClassId = base.ParmJObj.GetInt("ReserveClassId", 0);
            var model = visitorReserveModule.GetReserveClassModule(ReserveClassId);
            return ApiResult.Success(data: model);
        }

        /// <summary>
        /// 根据编号删除约课班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveReserveClass()
        {
            var ReserveClassId = base.ParmJObj.GetInt("ReserveClassId", 0);
            var flag = visitorReserveModule.RemoveReserveClassModule(ReserveClassId,base.UserInfo,out string message);
            return flag ? ApiResult.Success() : ApiResult.Failed(message: message);
        }
        #endregion
    }
}
