/*******************************************************************************
 * Copyright © 2020 WaterCloud.Framework 版权所有
 * Author: WaterCloud
 * Description: WaterCloud快速开发平台
 * Website：
*********************************************************************************/

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
{
	[Area("SystemManage")]
	public class ItemsTypeController : BaseController
	{
		public ItemsTypeService _service { get; set; }

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetTreeSelectJson()
		{
			var data = await _service.GetList();
			data = data.Where(a => a.F_Layers == 1).ToList();
			var treeList = new List<TreeSelectModel>();
			foreach (ItemsEntity item in data)
			{
				TreeSelectModel treeModel = new TreeSelectModel();
				treeModel.id = item.F_Id;
				treeModel.text = item.F_FullName;
				treeModel.parentId = item.F_ParentId;
				treeList.Add(treeModel);
			}
			return Content(treeList.TreeSelectJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetTreeJson()
		{
			var data = await _service.GetList();
			var treeList = new List<TreeViewModel>();
			foreach (ItemsEntity item in data)
			{
				TreeViewModel tree = new TreeViewModel();
				bool hasChildren = data.Count(t => t.F_ParentId == item.F_Id) == 0 ? false : true;
				tree.id = item.F_Id;
				tree.text = item.F_FullName;
				tree.value = item.F_EnCode;
				tree.parentId = item.F_ParentId;
				tree.isexpand = true;
				tree.complete = true;
				tree.hasChildren = hasChildren;
				treeList.Add(tree);
			}
			return Content(treeList.TreeViewJson());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetTreeGridJson(string keyword)
		{
			var data = await _service.GetLookList();
			if (!string.IsNullOrEmpty(keyword))
			{
				data = data.TreeWhere(t => t.F_FullName.Contains(keyword));
			}
			var treeList = new List<TreeGridModel>();
			foreach (ItemsEntity item in data)
			{
				TreeGridModel treeModel = new TreeGridModel();
				treeModel.id = item.F_Id;
				treeModel.title = item.F_FullName;
				treeModel.parentId = item.F_ParentId;
				//treeModel.self = item;
				treeList.Add(treeModel);
			}
			return DTreeResult(treeList.TreeList());
		}

		[HttpGet]
		[HandlerAjaxOnly]
		public async Task<ActionResult> GetGridJson(string keyword)
		{
			var data = await _service.GetLookList();
			if (!string.IsNullOrEmpty(keyword))
			{
				data = data.TreeWhere(t => t.F_FullName.Contains(keyword));
			}
			return Success(data.Count, data);
		}

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

		[HttpPost]
		[HandlerAjaxOnly]
		public async Task<ActionResult> SubmitForm(ItemsEntity itemsEntity, string keyValue)
		{
			try
			{
				if (itemsEntity.F_ParentId == "0")
				{
					itemsEntity.F_Layers = 1;
				}
				else
				{
					itemsEntity.F_Layers = (await _service.GetForm(itemsEntity.F_ParentId)).F_Layers + 1;
				}
				await _service.SubmitForm(itemsEntity, keyValue);
				return await Success("操作成功。", "", keyValue);
			}
			catch (Exception ex)
			{
				return await Error(ex.Message, "", keyValue);
			}
		}

		[HttpPost]
		[HandlerAjaxOnly]
		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);
			}
		}
	}
}