using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Datory;
using GxPress.Common.Tools;
using GxPress.Entity.WorkFlow;
using GxPress.Entity.WorkFlowDto;
using GxPress.Entity.WorkProcessDto;
using GxPress.EnumConst;
using GxPress.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AdminControllers
{
    public partial class AdminFlowController
    {
        /// <summary>
        /// 动作 - 审批
        /// </summary>
        /// <param name="request">审批意见</param>
        /// <returns></returns>
        [HttpPost("action/check")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsCheck([FromBody] CheckRequest request)
        {
            await _flowService.CheckAsync(request.TodoId, request.IsChecked, request.Message);

            return new DefaultResult
            {
                Value = true
            };
        }

        /// <summary>
        /// 动作 - 下载
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/download")]
        [AllowAnonymous]
        public async Task<DefaultStringResult> ActionsDownload([FromBody] ActionRequest request)
        {
            var url = await docService.MakeDoc(request.FlowId, _loginContext.AccountId);
            var result = new DownloadResult
            {
                DownloadUrl = StringUtils.AddDomain(url),
                PreviewUrl = StringUtils.GetPreviewDocUrl(url)
            };
            return new DefaultStringResult
            {
                Value = result.DownloadUrl
            };
        }
        /// <summary>
        /// 动作 - 催办
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/remind")]
        [AllowAnonymous]
        public DefaultResult ActionsRemind([FromBody] ActionRequest request)
        {
            return new DefaultResult
            {
                Value = true
            };
        }

        /// <summary>
        /// 动作 - 上会
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        [HttpPost("action/meeting")]
        [AllowAnonymous]
        public async Task<DefaultResult> ActionsMeeting([FromBody] ActionRequest request)
        {
            //await _flowRepository.UpdateStateAsync(request.FlowId, nameof(FlowState.Meeting));
            await _flowTodoRepository.UpdateAsync(Q.Where(nameof(Entity.WorkFlow.FlowTodo.FlowId), request.FlowId).Where(nameof(Entity.WorkFlow.FlowTodo.UserId), _loginContext.AccountId).Set(nameof(Entity.WorkFlow.FlowTodo.IsDone), true).Set(nameof(Entity.WorkFlow.FlowTodo.DoneType), 3));
            return new DefaultResult
            {
                Value = true
            };
        }
    }
}