using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 流程委托
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WFDelegateController : ControllerBase
    {
        WFDelegateApp delegateApp;

        public WFDelegateController(WFDelegateApp delegateApp)
        {
            this.delegateApp = delegateApp;
        }

        /// <summary>
        /// 获取管理分页列表
        /// workflow/delegate/page
        /// </summary>
        /// <param name="paginationInputDto">分页参数</param>
        /// <param name="queryParams">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<WFDelegateRule>>>> LoadPage([FromQuery] PageReq pageReq, [FromQuery] WFDelegateRule queryParams)
        {
            var pageInfo = await delegateApp.GetPageList(pageReq, queryParams);

            return new Response<PageInfo<List<WFDelegateRule>>>
            {
                Result = pageInfo,
                Message = "success"
            };
        }

        /// <summary>
        /// 获取委托表单信息
        /// workflow/delegate/{id}
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<WFDelegateDto>> Get(string id)
        {
            WFDelegateDto nWFDelegateDto = new WFDelegateDto();

            nWFDelegateDto.entity = await delegateApp.GetEntity(id);
            nWFDelegateDto.list = new List<string>();
            var list = await delegateApp.GetRelationList(id);
            foreach (var item in list)
            {
                nWFDelegateDto.list.Add(item.SchemeInfoId);
            }
            return new Response<WFDelegateDto>
            {
                Result = nWFDelegateDto,
                Message = "success"
            };
        }

        /// <summary>
        /// 获取我的委托人（发起委托）
        /// workflow/delegate/users/{code}
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<SysUser>>> LoadMyUserList(string code)
        {
            var data = await delegateApp.GetMyUserList(code);

            return new Response<List<SysUser>>
            {
                Result = data,
                Message = "success"
            };
        }


        /// <summary>
        /// 新增委托
        /// workflow/delegate
        /// </summary>
        /// <param name="dto">提交参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Add(WFDelegateDto dto)
        {
            var result = new Response<bool>();
            try
            {
                result = await delegateApp.SaveEntity(string.Empty, dto.entity, dto.list.ToArray());
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 更新委托
        /// workflow/delegate/{id}
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="dto">提交参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateForm(string id, WFDelegateDto dto)
        {
            var result = new Response<bool>();
            try
            {
                result = await delegateApp.SaveEntity(id, dto.entity, dto.list.ToArray());
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;           
        }

        /// <summary>
        /// 删除委托
        /// workflow/delegate/{id}
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteEntity(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await delegateApp.DeleteEntity(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 更新状态
        /// workflow/delegate/{id}/{state}
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateSate(string id, int state)
        {
            var result = new Response<bool>();
            try
            {
                result = await delegateApp.UpdateState(id, state);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
    }
}
