﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tydeus.Business;
using Tydeus.Common;
using Tydeus.Model;
using Tydeus.UI.Filters;
using Tydeus.UI.Services;

namespace Tydeus.UI.Areas.Teacher.Controllers
{
    /// <summary>
    /// 教师主控制器
    /// </summary>
    [EnhancedAuthorize]
    public class HomeController : Controller
    {
        #region Field
        /// <summary>
        /// 教师业务对象
        /// </summary>
        private TeacherBusiness teacherBusiness;
        #endregion //Field

        #region Constructor
        public HomeController()
        {
            this.teacherBusiness = new TeacherBusiness();
        }
        #endregion //Constructor

        #region Action
        /// <summary>
        /// 教师主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditProfile()
        {
            var user = PageService.GetCurrentUser(User.Identity.Name);
            
            Tydeus.Model.Teacher teacher = teacherBusiness.GetByCode(user.UserName);

            return View(teacher);
        }

        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult EditProfile(Tydeus.Model.Teacher model)
        {
            if (ModelState.IsValid)
            {
                ErrorCode result = this.teacherBusiness.Update(model);

                if (result == ErrorCode.Success)
                {
                    TempData["Message"] = "编辑信息成功";
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "编辑信息失败");
                    ModelState.AddModelError("", result.DisplayName());
                }
            }

            return View(model);
        }
        #endregion //Action
    }
}