using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Utility;

namespace APT.PF.WebApiControllers.Api.PF
{
    /// <summary>
    /// 表单自定义设置
    /// </summary>
    [Route("api/PF/PageCustom")]
    [APT.Infrastructure.Api.RootOrg]
    public class PageCustomController : AuthorizeApiController<T_PF_PAGE_CUSTOM>
    {

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_PF_PAGE_CUSTOM>> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        /// 

        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_PAGE_CUSTOM>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_PF_PAGE_CUSTOM> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_PF_PAGE_CUSTOM> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitOrderPaged(null, pageFilter);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Delete")]
        public JsonActionResult<bool> Delete(string id)
        {
            return SafeExecute<bool>(() =>
            {
                var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
                formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageCustom, id);
                this.DeleteEntity<T_PF_PAGE_CUSTOM>(id);
                return true;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_PF_PAGE_CUSTOM entity)
        {
            return SafeExecute<bool>(() =>
            {
                this.UpdateEntity(entity);
                var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
                formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageCustom, entity.ID.ToString());
                return true;
            });
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet, Route("BatchDelete")]
        public JsonActionResult<bool> BatchDelete(string ids)
        {
            return SafeExecute<bool>(() =>
            {
                var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
                formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageCustom, ids);
                string[] arrays = string.IsNullOrEmpty(ids) ? null : ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (arrays != null && arrays.Length > 0)
                    this.DeleteEntity<T_PF_PAGE_CUSTOM>(t => arrays.Contains(t.ID.ToString()));
                return true;
            });
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_PF_PAGE_CUSTOM> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }


        /// <summary>
        /// 获取自定义页配置信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetCustomPageConfigInfo")]
        public JsonActionResult<CustomPageModel> GetCustomPageConfigInfo([FromBody] KeywordFilter filter)
        {
            return SafeExecute<CustomPageModel>(() =>
            {
                string code = filter.Keyword;
                filter.OrgType = FilterOrgTypeEnum.仅本组织;
                var form = this.GetEntityByRedis<T_PF_FORM>(code, filter.OrgId ?? Guid.Empty);
                if (form == null)
                    return null;
                CustomPageModel result = new CustomPageModel();
                result.Nav_Form = form;
                var pageCustoms = this.GetEntitiesByRedis<T_PF_PAGE_CUSTOM>(null,new BaseFilter(filter.GetOrgId()), form.ID.ToString());
                result.Nav_PageCustom = pageCustoms.FirstOrDefault();
                return result;
            });
        }



    }
}
