using Learun.Application.Base.SystemModule;
using Learun.Application.OA;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.EducationalAdministration;
using Learun.Application.TwoDevelopment.Permission;
using Learun.Application.WorkFlow;
using Learun.Util;
using Learun.Util.Operat;

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Learun.Application.TwoDevelopment.AdmissionsPlatform;
using Learun.Application.TwoDevelopment.ReceiveSendFeeManagement;

namespace Learun.Application.Web.Controllers
{
    [HandlerLogin(FilterMode.Ignore)]
    public class OnlineRegistrateController : MvcControllerBase
    {
        private CdMajorIBLL cdMajorIBLL = new CdMajorBLL();
        private DataItemIBLL dataItemIBLL = new DataItemBLL();
        private CompanyIBLL companyIBLL = new CompanyBLL();
        private AP_OnlineUserInfoIBLL onlineUserInfoIBLL = new AP_OnlineUserInfoBLL();
        private AP_OnlineStudentInfoIBLL onlineStudentInfoIBLL = new AP_OnlineStudentInfoBLL();
        private AP_CompanyPaymentAccountIBLL aP_CompanyPaymentAccountIBLL = new AP_CompanyPaymentAccountBLL();
        private FinaChargesStandardIBLL finaChargesStandardIBLL = new FinaChargesStandardBLL();
        private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
        private DIC_PROVINCEIBLL dIC_PROVINCEIBLL = new DIC_PROVINCEBLL();
        private DataSourceIBLL dataSourceIBLL = new DataSourceBLL();


        /// <summary>
        /// 选择专业
        /// </summary>
        /// <returns></returns>
        public ActionResult MajorInfo(string userId, bool isReselect = false)
        {
            //是否是重选专业
            ViewBag.UserId = userId;
            ViewBag.IsReselect = isReselect;
            //获取专业信息
            var majorInfoModel = new MajorInfoModel();
            var aaa = cdMajorIBLL.GetListByDeptNo("").GroupBy(x => x.RecruitObject).Select(x => new RecruitInfoModel()
            {
                RecruitObject = x.Key,
                RecruitObjectText = dataItemIBLL.GetDetailList("RecruitObject").FirstOrDefault(a => a.F_ItemValue == x.Key)?.F_ItemName,
                SchoolInfoList = x.GroupBy(y => y.F_SchoolId).Select(y => new SchoolInfoModel()
                {
                    ShoolId = y.Key,
                    Shool = companyIBLL.GetEntity(y.Key)?.F_FullName,
                    MajorInfoList = y.Select(xx => new CdMajorEntity()
                    {
                        MajorNo = xx.MajorNo,
                        MajorName = xx.MajorName
                    }).OrderBy(xx => xx.MajorNo).ToList()
                }).ToList()
            }).OrderBy(x => x.RecruitObject).ToList();
            majorInfoModel.RecruitInfoList = aaa;

            return View(majorInfoModel);
        }
        /// <summary>
        /// 专业介绍
        /// </summary>
        /// <returns></returns>
        public ActionResult MajorIntroduce(string majorNo, string userId, bool isReselect = false)
        {
            //是否是重选专业
            ViewBag.UserId = userId;
            ViewBag.IsReselect = isReselect;
            var model = cdMajorIBLL.GetCdMajorEntityByMajorNo(majorNo);
            if (!string.IsNullOrEmpty(model.RecruitObject))
            {
                model.RecruitObjectText = dataItemIBLL.GetDetailList("RecruitObject").FirstOrDefault(a => a.F_ItemValue == model.RecruitObject)?.F_ItemName;
            }
            if (!string.IsNullOrEmpty(model.F_SchoolId))
            {
                model.SchoolName = companyIBLL.GetEntity(model.F_SchoolId)?.F_FullName;
            }
            //获取头像地址
            if (!string.IsNullOrEmpty(model.Photo))
            {
                var annexesFileEntity = annexesFileIBLL.GetEntityByFolderId(model.Photo);
                if (annexesFileEntity != null)
                {
                    model.Photo = "/" + annexesFileEntity.F_FilePath.Substring(annexesFileEntity.F_FilePath.IndexOf("Resource"));
                }
                else
                {
                    model.Photo = "";
                }
            }
            return View(model);
        }
        /// <summary>
        /// 填写信息
        /// </summary>
        /// <returns></returns>
        public ActionResult SignUp(string majorNo)
        {
            var model = cdMajorIBLL.GetCdMajorEntityByMajorNo(majorNo);
            if (!string.IsNullOrEmpty(model.RecruitObject))
            {
                model.RecruitObjectText = dataItemIBLL.GetDetailList("RecruitObject").FirstOrDefault(a => a.F_ItemValue == model.RecruitObject)?.F_ItemName;
            }
            if (!string.IsNullOrEmpty(model.F_SchoolId))
            {
                model.SchoolName = companyIBLL.GetEntity(model.F_SchoolId)?.F_FullName;
            }
            ViewBag.Sex = dataItemIBLL.GetDetailList("usersex");
            return View(model);
        }
        /// <summary>
        /// 报名成功
        /// </summary>
        /// <returns></returns>
        public ActionResult SignUpSuccess(string userId)
        {
            ViewBag.UserId = userId;
            return View();
        }
        /// <summary>
        /// 缴费
        /// </summary>
        /// <returns></returns>
        public ActionResult PaymentMoney(string userId, bool isLogin = false)
        {
            //是否是登录后点击的缴费
            ViewBag.IsLogin = isLogin;
            //基础信息
            var onlineUserInfo = onlineUserInfoIBLL.GetEntity(userId);
            if (onlineUserInfo != null)
            {
                onlineUserInfo.SexId = dataItemIBLL.GetDetailList("usersex").FirstOrDefault(a => a.F_ItemValue == onlineUserInfo.SexId)?.F_ItemName;
                var osinfo = onlineStudentInfoIBLL.GetEntityByUserId(onlineUserInfo.Id);
                onlineUserInfo.SchoolId = osinfo?.SchoolId;
                onlineUserInfo.MajorId = osinfo?.MajorId;
                onlineUserInfo.SchoolName = companyIBLL.GetEntity(onlineUserInfo.SchoolId)?.F_FullName;
                onlineUserInfo.MajorName = cdMajorIBLL.GetCdMajorEntityByMajorNo(onlineUserInfo.MajorId)?.MajorName;

                //费用信息
                var semesterAndYear = Common.GetSemesterAndYear();
                var finaChargesStandardEntities = finaChargesStandardIBLL.GetFinaChargesStandardListByMajorNoOfNotAll(osinfo?.MajorId, semesterAndYear.AcademicYearShort, semesterAndYear.Semester, semesterAndYear.AcademicYearShort.Substring(0, 2));
                if (finaChargesStandardEntities.Any())
                {
                    ViewBag.feeList = finaChargesStandardEntities;
                    ViewBag.feeTotal = finaChargesStandardEntities.Select(x => x.Standard).Sum();
                }

            }
            //支付方式
            ViewBag.AP_CompanyPaymentAccount = new AP_CompanyPaymentAccountEntity();
            var AP_CompanyPaymentAccountData = aP_CompanyPaymentAccountIBLL.GetList(null).FirstOrDefault(x => x.IsEnabled == true);
            if (AP_CompanyPaymentAccountData != null)
            {
                AP_CompanyPaymentAccountData.CompayId = companyIBLL.GetEntity(AP_CompanyPaymentAccountData.CompayId)?.F_FullName;
                ViewBag.AP_CompanyPaymentAccount = AP_CompanyPaymentAccountData;
            }
            return View(onlineUserInfo);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 登记表
        /// </summary>
        /// <returns></returns>
        public ActionResult RegistrateForm(string userId)
        {
            var ouinfo = new AP_OnlineUserInfoEntity();
            var onlineUserInfoEntity = onlineUserInfoIBLL.GetEntity(userId);
            if (onlineUserInfoEntity != null)
            {
                //获取头像地址
                if (!string.IsNullOrEmpty(onlineUserInfoEntity.PhotoUrl))
                {
                    var annexesFileEntity = annexesFileIBLL.GetEntityByFolderId(onlineUserInfoEntity.PhotoUrl);
                    if (annexesFileEntity != null)
                    {
                        onlineUserInfoEntity.PhotoUrl = "/" + annexesFileEntity.F_FilePath.Substring(annexesFileEntity.F_FilePath.IndexOf("Resource"));
                    }
                    else
                    {
                        onlineUserInfoEntity.PhotoUrl = "";
                    }
                }
                //学生信息
                var onlineStudentInfoEntity = onlineUserInfoIBLL.GetAP_OnlineStudentInfoEntity(userId);
                if (onlineStudentInfoEntity != null)
                {
                    onlineUserInfoEntity.AP_OnlineStudentInfoEntity = onlineStudentInfoEntity;
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentInfoEntity = new AP_OnlineStudentInfoEntity();
                }
                //简历
                var onlineStudentResumeInfo = onlineUserInfoIBLL.GetAP_OnlineStudentResumeInfoList(userId);
                if (onlineStudentResumeInfo.Any())
                {
                    onlineUserInfoEntity.AP_OnlineStudentResumeInfoList = onlineStudentResumeInfo.ToList();
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentResumeInfoList = new List<AP_OnlineStudentResumeInfoEntity>();
                }
                //家庭成员
                var onlineStudentFamilyInfo = onlineUserInfoIBLL.GetAP_OnlineStudentFamilyInfoList(userId);
                if (onlineStudentFamilyInfo.Any())
                {
                    onlineUserInfoEntity.AP_OnlineStudentFamilyInfoList = onlineStudentFamilyInfo.ToList();
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentFamilyInfoList = new List<AP_OnlineStudentFamilyInfoEntity>();
                }
                ouinfo = onlineUserInfoEntity;
            }

            return View(ouinfo);
        }
        /// <summary>
        /// 登记表完善
        /// </summary>
        /// <returns></returns>
        public ActionResult RegistrateFormEdit(string userId)
        {
            var ouinfo = new AP_OnlineUserInfoEntity();
            ViewBag.Url = "";
            var onlineUserInfoEntity = onlineUserInfoIBLL.GetEntity(userId);
            if (onlineUserInfoEntity != null)
            {
                //获取头像地址
                if (!string.IsNullOrEmpty(onlineUserInfoEntity.PhotoUrl))
                {
                    var annexesFileEntity = annexesFileIBLL.GetEntityByFolderId(onlineUserInfoEntity.PhotoUrl);
                    if (annexesFileEntity != null)
                    {
                        ViewBag.Url = "/" + annexesFileEntity.F_FilePath.Substring(annexesFileEntity.F_FilePath.IndexOf("Resource"));
                    }
                }
                //学生信息
                var onlineStudentInfoEntity = onlineUserInfoIBLL.GetAP_OnlineStudentInfoEntity(userId);
                if (onlineStudentInfoEntity != null)
                {
                    onlineUserInfoEntity.AP_OnlineStudentInfoEntity = onlineStudentInfoEntity;
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentInfoEntity = new AP_OnlineStudentInfoEntity();
                }
                //简历
                var onlineStudentResumeInfo = onlineUserInfoIBLL.GetAP_OnlineStudentResumeInfoList(userId);
                if (onlineStudentResumeInfo.Any())
                {
                    onlineUserInfoEntity.AP_OnlineStudentResumeInfoList = onlineStudentResumeInfo.ToList();
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentResumeInfoList = new List<AP_OnlineStudentResumeInfoEntity>();
                }
                //家庭成员
                var onlineStudentFamilyInfo = onlineUserInfoIBLL.GetAP_OnlineStudentFamilyInfoList(userId);
                if (onlineStudentFamilyInfo.Any())
                {
                    onlineUserInfoEntity.AP_OnlineStudentFamilyInfoList = onlineStudentFamilyInfo.ToList();
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentFamilyInfoList = new List<AP_OnlineStudentFamilyInfoEntity>();
                }
                ouinfo = onlineUserInfoEntity;
            }

            return View(ouinfo);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ActionResult PasswordEdit(string userId)
        {
            var ouinfo = new AP_OnlineUserInfoEntity();
            var onlineUserInfoEntity = onlineUserInfoIBLL.GetEntity(userId);
            if (onlineUserInfoEntity != null)
            {
                //获取头像地址
                if (!string.IsNullOrEmpty(onlineUserInfoEntity.PhotoUrl))
                {
                    var annexesFileEntity = annexesFileIBLL.GetEntity(onlineUserInfoEntity.PhotoUrl);
                    if (annexesFileEntity != null)
                    {
                        onlineUserInfoEntity.PhotoUrl = "/" + annexesFileEntity.F_FilePath.Substring(annexesFileEntity.F_FilePath.IndexOf("Resource"));
                    }
                    else
                    {
                        onlineUserInfoEntity.PhotoUrl = "";
                    }
                }
                //学生信息
                var onlineStudentInfoEntity = onlineUserInfoIBLL.GetAP_OnlineStudentInfoEntity(userId);
                if (onlineStudentInfoEntity != null)
                {
                    onlineUserInfoEntity.AP_OnlineStudentInfoEntity = onlineStudentInfoEntity;
                }
                else
                {
                    onlineUserInfoEntity.AP_OnlineStudentInfoEntity = new AP_OnlineStudentInfoEntity();
                }

                ouinfo = onlineUserInfoEntity;
            }
            return View(ouinfo);
        }
        /// <summary>
        /// 使用向导
        /// </summary>
        /// <returns></returns>
        public ActionResult UseGuid() {
            return View();
        }
        #region 提交数据

        /// <summary>
        /// 获取数据源数据
        /// </summary>
        /// <param name="code">数据源编号</param>
        /// <param name="strWhere">sql查询条件语句</param>
        /// <param name="queryJson">数据源请求条件字串</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDataSourceMap(string code, string where)
        {
            var data = dataSourceIBLL.GetDataTable(code, where);
            return JsonResult(data);
        }

        /// <summary>
        /// 获取数据源数据
        /// </summary>
        /// <param name="code">数据源编号</param>
        /// <param name="strWhere">sql查询条件语句</param>
        /// <param name="queryJson">数据源请求条件字串</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDataItemMap(string code)
        {
            var data = dataItemIBLL.GetDetailList(code, null);
            return JsonResult(data);
        }

        /// <summary>
        /// 保存【填写信息】
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SaveOnlineRegistrate(string strEntity)
        {
            var entity = strEntity.ToObject<AP_OnlineUserInfoEntity>();
            var aa = onlineUserInfoIBLL.GetEntityByMobile(entity.Mobile);
            if (aa != null)
            {
                return Fail("该手机号已报名!");
            }
            onlineUserInfoIBLL.SaveOnlineRegistrate(entity);
            var aaa = onlineUserInfoIBLL.GetEntityByMobile(entity.Mobile);

            return Success("保存成功!", aaa);
        }

        /// <summary>
        /// 登录
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Login(string userName, string passward)
        {
            var entity = onlineUserInfoIBLL.GetEntityByMobile(userName);
            if (entity == null)
            {
                return Fail("该手机号不存在!");
            }
            if (entity.Password != passward)
            {
                return Fail("密码错误!");
            }

            return Success("登录成功!", entity);
        }

        /// <summary> 
        /// 完善资料 
        /// <param name="keyValue">主键</param> 
        /// <summary> 
        /// <returns></returns> 
        [HttpPost]
        [AjaxOnly]
        public ActionResult SaveRegistrateFormEdit(string keyValue, string strEntity, string strOnlineStudentResumeList, string strOnlineStudentFamilyList)
        {
            AP_OnlineUserInfoEntity entity = strEntity.ToObject<AP_OnlineUserInfoEntity>();
            entity.CreateDate = DateTime.Now;

            AP_OnlineStudentInfoEntity studentEntity = strEntity.ToObject<AP_OnlineStudentInfoEntity>();
            studentEntity.Id = entity.OSIId;
            studentEntity.CreateDate = DateTime.Now;

            List<AP_OnlineStudentResumeInfoEntity> onlineStudentResumeList = strOnlineStudentResumeList.ToObject<List<AP_OnlineStudentResumeInfoEntity>>();
            List<AP_OnlineStudentFamilyInfoEntity> onlineStudentFamilyList = strOnlineStudentFamilyList.ToObject<List<AP_OnlineStudentFamilyInfoEntity>>();
            onlineUserInfoIBLL.SaveRegistrateForm(keyValue, entity, studentEntity, onlineStudentResumeList, onlineStudentFamilyList);
            return Success("保存成功!");
        }

        /// <summary>
        /// 确定重选专业
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SaveReselectMajor(string userId, string majorNo)
        {
            onlineUserInfoIBLL.SaveReselectMajor(userId, majorNo);
            return Success("保存成功!");
        }

        /// <summary>
        /// 提交修改密码
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SavePasswordEdit(string userId, string oldPassword, string newPassword)
        {
            var ouientity = onlineUserInfoIBLL.GetEntity(userId);
            if (ouientity == null)
            {
                return Fail("学生不存在!");
            }
            if (oldPassword != ouientity.Password)
            {
                return Fail("旧密码不正确!");
            }
            if (string.IsNullOrEmpty(newPassword))
            {
                return Fail("新密码为空!");
            }
            ouientity.Password = newPassword;
            onlineUserInfoIBLL.SaveEntity(ouientity.Id, ouientity);
            return Success("保存成功!");
        }

        #endregion

        #region 模型

        /// <summary>
        /// 模型【选择专业】
        /// </summary>
        public class MajorInfoModel
        {
            public List<RecruitInfoModel> RecruitInfoList { get; set; }
        }
        public class RecruitInfoModel
        {
            /// <summary>
            /// 招生对象
            /// </summary>
            public string RecruitObject { get; set; }
            public string RecruitObjectText { get; set; }
            /// <summary>
            /// 校区
            /// </summary>
            public List<SchoolInfoModel> SchoolInfoList { get; set; }

        }
        public class SchoolInfoModel
        {
            /// <summary>
            /// 校区
            /// </summary>
            public string Shool { get; set; }
            /// <summary>
            /// 校区主键
            /// </summary>
            public string ShoolId { get; set; }
            /// <summary>
            /// 专业
            /// </summary>
            public List<CdMajorEntity> MajorInfoList { get; set; }
        }

        #endregion





    }
}