using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.Domain.ContentManage;
using WaterCloud.Service.ContentManage;

namespace WaterCloud.Web.Areas.ContentManage.Controllers
{
	/// <summary>
	/// 创 建：超级管理员
	/// 日 期：2020-06-09 19:42
	/// 描 述：新闻管理控制器类
	/// </summary>
	[Area("ContentManage")]
	[AllowAnonymous]
	public class ArticleNewsController : BaseController
	{
		//属性注入示例
		public ArticleNewsService _service { get; set; }

		[HttpGet]
		public override ActionResult Form()
		{
			//控制器视图传值示例
			if (_service.currentuser.UserId == null)
			{
				return View();
			}
			var keyValue = HttpContext.Request.Query["keyValue"].ToString();
			ViewBag.UserName = _service.currentuser.UserName;
			ViewBag.Content = _service.GetForm(keyValue).Result.ToJson();
			return View();
		}

		[HttpGet]
		public override ActionResult Details()
		{
			//控制器视图传值示例
			if (_service.currentuser.UserId == null)
			{
				return View();
			}
			var keyValue = HttpContext.Request.Query["keyValue"].ToString();
			ViewBag.Content = _service.GetForm(keyValue).Result.ToJson();
			return View();
		}

		#region 获取数据

		[HandlerAjaxOnly]
		[IgnoreAntiforgeryToken]
		public async Task<ActionResult> GetGridJson(SoulPage<ArticleNewsEntity> pagination, string keyword, string CategoryId)
		{
			if (string.IsNullOrEmpty(pagination.field))
			{
				pagination.field = "F_Id";
				pagination.order = "desc";
			}
			var data = await _service.GetLookList(pagination, keyword, CategoryId);
			return Content(pagination.setData(data).ToJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetListJson(string keyword)
		{
			var data = await _service.GetList(keyword);
			return Content(data.ToJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetFormJson(string keyValue)
		{
			var data = await _service.GetForm(keyValue);
			return Content(data.ToJson());
		}

		#endregion 获取数据

		#region 提交数据

		[HttpPost]
		[HandlerAjaxOnly]
		public async Task<ActionResult> SubmitForm(ArticleNewsEntity entity, string keyValue)
		{
			try
			{
				await _service.SubmitForm(entity, keyValue);
				return await Success("操作成功。", "", keyValue);
			}
			catch (Exception ex)
			{
				return await Error(ex.Message, "", keyValue);
			}
		}

		[HttpPost]
		[HandlerAjaxOnly]
		[HandlerAuthorize]
		public async Task<ActionResult> DeleteForm(string keyValue)
		{
			try
			{
				await _service.DeleteForm(keyValue);
				return await Success("操作成功。", "", keyValue, DbLogType.Delete);
			}
			catch (Exception ex)
			{
				return await Error(ex.Message, "", keyValue, DbLogType.Delete);
			}
		}

		#endregion 提交数据
	}
}