using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.Domain.SystemManage;
using WaterCloud.Service.SystemManage;

namespace WaterCloud.Web.Areas.SystemManage.Controllers
{
	/// <summary>
	/// 创 建：超级管理员
	/// 日 期：2020-05-21 14:38
	/// 描 述：字段管理控制器类
	/// </summary>
	[Area("SystemManage")]
	public class ModuleFieldsController : BaseController
	{
		public ModuleFieldsService _service { get; set; }
		public ModuleService _moduleService { get; set; }

		#region 获取数据

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetGridJson(Pagination pagination, string moduleId, string keyword)
		{
			pagination.order = "desc";
			pagination.field = "F_Id";
			var data = await _service.GetLookList(pagination, moduleId, keyword);
			return Success(pagination.records, data);
		}

		[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> GetSelectJson(string moduleId)
		{
			var data = (await _service.GetList()).Where(a => a.F_ModuleId == moduleId).ToList();
			List<object> list = new List<object>();
			foreach (var item in data)
			{
				list.Add(new { id = item.F_EnCode, text = item.F_FullName });
			}
			return Content(list.ToJson());
		}

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

		#endregion 获取数据

		#region 提交数据

		[HttpPost]
		[HandlerAjaxOnly]
		public async Task<ActionResult> SubmitForm(ModuleFieldsEntity 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);
			}
		}

		[HttpGet]
		public ActionResult CloneFields()
		{
			return View();
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetCloneFieldsTreeJson()
		{
			var moduledata = await _moduleService.GetList();
			//moduledata = moduledata.Where(a => a.F_Target == "iframe" || a.F_Layers>1).ToList();
			var fieldsdata = await _service.GetList();
			var treeList = new List<TreeGridModel>();
			foreach (ModuleEntity item in moduledata)
			{
				TreeGridModel treeModel = new TreeGridModel();
				treeModel.id = item.F_Id;
				treeModel.title = item.F_FullName;
				treeModel.parentId = item.F_ParentId;
				treeModel.checkArr = "0";
				treeModel.disabled = true;
				//treeModel.self = item;
				treeList.Add(treeModel);
			}
			foreach (ModuleFieldsEntity item in fieldsdata)
			{
				TreeGridModel treeModel = new TreeGridModel();
				treeModel.id = item.F_Id;
				treeModel.title = item.F_EnCode;
				treeModel.parentId = item.F_ModuleId;
				treeModel.checkArr = "0";
				//treeModel.self = item;
				treeList.Add(treeModel);
			}
			return DTreeResult(treeList.TreeList());
		}

		[HttpPost]
		[HandlerAjaxOnly]
		public async Task<ActionResult> SubmitCloneFields(string moduleId, string Ids)
		{
			try
			{
				await _service.SubmitCloneFields(moduleId, Ids);
				return await Success("克隆成功。", "", Ids, DbLogType.Create);
			}
			catch (Exception ex)
			{
				return await Error("克隆失败，" + ex.Message, "", Ids, DbLogType.Create);
			}
		}

		#endregion 提交数据
	}
}