using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum;
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;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class ClassRoomController : BaseController
    {
        /// <summary>
        /// 教室处理类对象
        /// </summary>
        private readonly ClassRoomModule classRoomModule = new ClassRoomModule();

        #region 教室管理
        /// <summary>
        /// 获取教室页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoomPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                RoomName = base.ParmJObj.GetStringValue("RoomName"),
                Status = (DateStateEnum)base.ParmJObj.GetInt("Status"),
            };
            var list = classRoomModule.GetClassRoomPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoomList()
        {
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                RoomName = base.ParmJObj.GetStringValue("RoomName")
            };
            var list = classRoomModule.GetClassRoomListRepository(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改教室
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetClassRoom()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Room_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = UserInfo.Id;
            extModel.UpdateBy = UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            bool flag = classRoomModule.SetClassRoomModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取教室实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoom()
        {
            var RoomId = base.ParmJObj.GetInt("RoomId", 0);
            var extModel = classRoomModule.GetClassRoomModule(RoomId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除教室
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveClassRoom()
        {
            var RoomId = base.ParmJObj.GetInt("RoomId", 0);
            var Status = base.ParmJObj.GetInt("Status");
            var flag = classRoomModule.RemoveClassRoomModule(RoomId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion


        #region  甲鹤上课管理系统- 教室注册

        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public ApiResult GetDropClassRoomList()
        {
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = 100000
            };
            var list = classRoomModule.GetClassRoomListRepository(query);
            var result = list.Select(x => new
            {
                x.School_Id,
                x.RoomId,
                x.RoomName,
                RoomPicList = string.IsNullOrWhiteSpace(x.RoomPic) ? new List<string>() : JsonHelper.DeserializeObject<List<string>>(x.RoomPic),
                x.SeatNum,
                x.Group_Id

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


        /// <summary>
        /// 添加修改教室
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public ApiResult SetNoLoginClassRoom()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Room_ViewModel>(RequestParm.Msg.ToString());
            if (extModel.RoomId == 0)
            {
                extModel.CreateTime = DateTime.Now;
                extModel.CreateBy = 1;
            }
            extModel.UpdateBy = 1;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = extModel.Group_Id == 0 ? 100000 : extModel.Group_Id;
            if (extModel.RoomPicList != null && extModel.RoomPicList.Any())
            {
                extModel.RoomPic = Common.Plugin.JsonHelper.Serialize(extModel.RoomPicList);
            }
            else
            {
                extModel.RoomPic = "";
            }
            bool flag = classRoomModule.SetClassRoomModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }




        
        #endregion
    }
}
