using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using Common;
using Domain;
using Service;

namespace WebSite.Controllers
{
    public class RoomTypeChannelsController : BaseController
    {
        #region Properties

        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(RoomTypeChannelsController));
        private const int AUTHORITY_RoomType = 20;
        public IRoomTypeChannelsManager RoomTypeChannelsManager { get; set; }
        public IRoomTypeManager RoomTypeManager { get; set; }

        #endregion

        [Authorize]
        public ActionResult Index(int roomTypeID)
        {
            return View(roomTypeID);
        }
        /// <summary>
        /// 获取当前房型的电视频道
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="order"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult LoadAllByPage(int page, int rows, string order, string sort, int roomTypeID)
        {
            long total = 0;
            var list = this.RoomTypeChannelsManager.LoadAllByPage(out total, page, rows, order, sort, roomTypeID);
            var result = list.Select(r => new
            {
                r.ID,
                RoomTypeID = r.RoomType.ID,
                RoomTypeName = r.RoomType.Name,
                r.Code,
                r.Name,
                r.Sort
            });
            return Json(new { total = total, rows = result });
        }

        [Authorize]
        public ActionResult LoadAll(int roomTypeID)
        {
            var list = this.RoomTypeChannelsManager.LoadAll(roomTypeID);
            var result = list.Select(r => new
            {
                r.ID,
                r.Code,
                Name = r.Code + "-" + r.Name
            });
            return Json(new { rows = result });
        }

        [Authorize]
        public ActionResult Edit(int id, int roomTypeID)
        {
            if (id != 0)
            {
                return View(RoomTypeChannelsManager.Get(id));
            }
            RoomType roomType = RoomTypeManager.Get(roomTypeID);
            return View(new RoomTypeChannels { ID = 0, RoomType = roomType });//"~/Views/RoomTypeChannels/Edit.ascx",
        }

        [Authorize]
        public ActionResult Save(string jsonData, int roomTypeID)
        {
            RoomTypeChannels entity = Newtonsoft.Json.JsonConvert.DeserializeObject<RoomTypeChannels>(jsonData);
            RoomType roomType = RoomTypeManager.Get(roomTypeID);
            entity.RoomType = roomType;
            if (entity.ID == 0)
            {
                RoomTypeChannelsManager.Save(entity);
                SaveSystemLog(AUTHORITY_RoomType, HttpContext.InnerLanguage("New"), entity.Name);
            }
            else
            {
                RoomTypeChannels roomTypeChannel = RoomTypeChannelsManager.Get(entity.ID);
                roomTypeChannel.Code = entity.Code;
                roomTypeChannel.Name = entity.Name;
                roomTypeChannel.Sort = entity.Sort;
                RoomTypeChannelsManager.Update(roomTypeChannel);
                SaveSystemLog(AUTHORITY_RoomType, HttpContext.InnerLanguage("Edit"), entity.Name);
            }
            return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SaveSuccess") });
        }

        [Authorize]
        public ActionResult Delete(IList<int> idList)
        {
            try
            {
                if (idList != null && idList.Count > 0)
                {
                    RoomTypeChannelsManager.Delete(idList.Cast<object>().ToList());
                    string logDetail = String.Join("，", idList);
                    SaveSystemLog(AUTHORITY_RoomType, HttpContext.InnerLanguage("Delete"), logDetail);
                }
                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("DeletedSuccess") });
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("DeleteFailedBecause") + ex.Message });
            }
        }
    }
}
