using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Service;

namespace WebSite.Controllers
{
    public class FaultRecordsController : BaseController
    {
		private const int AUTHORITY_FautRecords = 14;

		private const int AUTHORITY_Repair = 15;

		public IFaultRecordsManager FaultRecordsManager { get; set; }

        public ActionResult Index()
        {
            return View();
        }

		[Authorize]
		public ActionResult LoadAllByPage(int page, int rows, string order, string sort)
		{
			long total = 0;

            var list = FaultRecordsManager.LoadAllByPage(out total, page, rows, order, sort, CurrentHotelID);

			var result = list.Select(r => new 
			{ 
				r.ID,
				r.Status,
				r.IsCancel,
				r.Number,
				r.RoomNumber,
				FaultTypeID = r.FaultType.ID,
				FaultTypeCode = r.FaultType.Code,
				FaultType = r.FaultType.Name,
				FaultTime = r.FaultTime.HasValue ? r.FaultTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
				r.Description,
				r.RegisteredPerson,
				r.RegisteredTime,
				r.RepairPerson,
				RepairTime = r.RepairTime.HasValue ? r.RepairTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
				r.RepairContent,
				r.CancelPerson,
				CancelTime = r.CancelTime.HasValue ? r.CancelTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
				r.CancelReason
			});

			return Json(new { total = total, rows = result });
		}

		[Authorize]
		public ActionResult GenerateFaultNumber()
		{
			return Json(new { IsSuccess = true, Data = new { Number = FaultRecordsManager.GenerateFaultNumber() } }, JsonRequestBehavior.AllowGet);
		}

		[Authorize]
		public ActionResult Delete(IList<int> idList)
		{
			FaultRecordsManager.Delete(idList.Cast<object>().ToList());

			return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("DeleteSuccess") });
		}

		[Authorize]
		public ActionResult Save(string jsonData)
		{
			FaultRecord entity = Newtonsoft.Json.JsonConvert.DeserializeObject<FaultRecord>(jsonData);

			if (entity.ID == 0)
			{
				entity.RegisteredPerson = User.Identity.Name;
				entity.RegisteredTime = DateTime.Now;
                entity.HotelID = CurrentHotelID;
				FaultRecordsManager.Save(entity);

                SaveSystemLog(AUTHORITY_FautRecords, HttpContext.InnerLanguage("FailureRegistration"), String.Format(HttpContext.InnerLanguage("FaultID")+"：{0}", entity.Number));
			}
			else 
			{
				FaultRecord fault = FaultRecordsManager.Get(entity.ID);
				if (fault != null)
				{
					fault.FaultTime = entity.FaultTime;
					fault.RoomNumber = entity.RoomNumber;
					fault.FaultType = entity.FaultType;
					fault.Description = entity.Description;
					FaultRecordsManager.Update(fault);

                    SaveSystemLog(AUTHORITY_FautRecords, HttpContext.InnerLanguage("FailureEdit"), String.Format(HttpContext.InnerLanguage("FaultID") + "：{0}", entity.Number));
				}
				else
				{
					return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SaveFailed")});
				}
			}

			return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SaveSuccess") });
		}

		[Authorize]
		public ActionResult Cancel(int id, DateTime cancelTime, string cancelReason)
		{
			FaultRecord fault = FaultRecordsManager.Get(id);

			if (fault != null)
			{
				fault.IsCancel = true;
				fault.CancelPerson = User.Identity.Name;
				fault.CancelTime = cancelTime;
				fault.CancelReason = cancelReason;
				FaultRecordsManager.Update(fault);

                SaveSystemLog(AUTHORITY_FautRecords, HttpContext.InnerLanguage("Logout"), String.Format(HttpContext.InnerLanguage("FaultID") + "：{0}", fault.Number));

				return Json(new { IsSuccess = true, Message =  HttpContext.InnerLanguage("LogoutSuccess")});
			}

			return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("LogoutFailed")});
		}

		[Authorize]
		public ActionResult Repair(int id, DateTime repairTime, string repairPerson, string repairContent)
		{
			FaultRecord fault = FaultRecordsManager.Get(id);

			if (fault != null)
			{
				fault.Status = 1;
				fault.RepairTime = repairTime;
				fault.RepairPerson = repairPerson;
				fault.RepairContent = repairContent;
				FaultRecordsManager.Update(fault);

				SaveSystemLog(AUTHORITY_Repair, HttpContext.InnerLanguage("RepairRecord"), String.Format(String.Format(HttpContext.InnerLanguage("FaultID") + "：{0}", fault.Number)));

				return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("RegistrationSuccess") });
			}

			return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("RegistrationFailed") });
		}
	}
}
