using Hotline.Share.Dtos.File;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Enums.Order;
using Hotline.Share.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using XF.Utility.EnumExtensions;

namespace Hotline.Share.Dtos.Order
{
    public class AuditSendBackDto
    {
        public string Id { get; set; }

        /// <summary>
        ///  审核结果
        /// </summary>
        public ESendBackAuditState State { get; set; }

        /// <summary>
        /// 审核意见
        /// </summary>
        public string? AuditContent { get; set; }


        /// <summary>
        /// 是否允许再次退回
        /// </summary>
        public bool? IsReturnAgain { get; set; }

    }

    public class BatchAuditSendBackDto : AuditSendBackDto
    {
        public List<string> Ids { get; set; }
    }
    public class SendBackDto : SendBackBaseDto
    {
        /// <summary>
        /// 工单ID
        /// </summary>
        public string OrderId { get; set; }


        /// <summary>
        /// 退回原因
        /// </summary>
        public string? Content { get; set; }

        /// <summary>
        /// 审批意见
        /// </summary>
        public string? AuditContent { get; set; }

        /// <summary>
        /// 工单
        /// </summary>
        public OrderDto Order { get; set; }

        /// <summary>
        /// 退回数据
        /// </summary>
        public PreviousWorkflowDto SendBackData { get; set; }

        /// <summary>
        /// 审批人ID
        /// </summary>
        public string? AuditId { get; set; }

        public string? AuditUser { get; set; }


        /// <summary>
        /// 审批时间
        /// </summary>
        public DateTime? AuditTime { get; set; }

        /// <summary>
        /// 申请部门ID
        /// </summary>
        public string ApplyOrgId { get; set; }

        /// <summary>
        /// 申请部门名称
        /// </summary>
        public string ApplyOrgName { get; set; }

        /// <summary>
        /// 退回部门ID
        /// </summary>
        public string SendBackOrgId { get; set; }

        /// <summary>
        /// 退回部门名称
        /// </summary>
        public string SendBackOrgName { get; set; }

        /// <summary>
        ///  审核结果  0  待审核  1 审核通过  2 审核不通过
        /// </summary>
        public ESendBackAuditState State { get; set; }

        public string StateText => State.GetDescription();
        /// <summary>
        /// 退回节点名称
        /// </summary>
        public string? SendBackStepName { get; set; }

        /// <summary>
        /// 退回节点创建时间
        /// </summary>
        public DateTime? WorkflowStepSendBackCrTime { get; set; }

        /// <summary>
        ///  退回时差
        /// </summary>
        public double SendBackTimeDifference => GetSendBackTimeDifference();

        public double GetSendBackTimeDifference()
        {
            if (WorkflowStepSendBackCrTime.HasValue)
            {
                TimeSpan? timeDifference = CreationTime - WorkflowStepSendBackCrTime;
                return Math.Round(timeDifference.Value.TotalMinutes / 60, 1);
            }
            return 0;
        }

        public bool IsReturnAgainShow => ApplyOrgId != "001" && SendBackOrgId == "001";
        /// <summary>
        /// 附件列表
        /// </summary>
        public List<FileDto> Files { get; set; } = new();
    }
    public class SendBackBaseDto
    {
        public DateTime? LastModificationTime { get; set; }

        public bool IsDeleted { get; set; }

        /// <summary>
        /// 删除时间
        /// </summary>
        public DateTime? DeletionTime { get; set; }


        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        public string Id { get; set; }

        /// <summary>
        /// 组织Id
        /// </summary>
        public string? CreatorOrgId { get; set; }


        public string? CreatorOrgName { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string? CreatorId { get; set; }

        public string? CreatorName { get; set; }
    }
    public record SendBackListDto : PagedKeywordRequest
    {
        /// <summary>
        /// </summary>
        public ESendBackAuditState? State { get; set; }

        /// <summary>
        /// 1 待办  2 已办 3全部
        /// </summary>
        public int AuditState { get; set; }

        /// <summary>
        /// 0 全部  1 我的
        /// </summary>
        public int? DataScope { get; set; }

        /// <summary>
        /// 工单标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 工单编号
        /// </summary>
        public string No { get; set; }

        /// <summary>
        /// 受理类型
        /// </summary>
        public string? AcceptTypeCode { get; set; }

        /// <summary>
        /// 申请部门
        /// </summary>
        public string? ApplyOrgName { get; set; }

        /// <summary>
        /// 期满时间开始
        /// </summary>
        public DateTime? ExpiredTimeStart { get; set; }

        /// <summary>
        /// 期满时间结束
        /// </summary>
        public DateTime? ExpiredTimeEnd { get; set; }

        /// <summary>
        /// 是否省工单
        /// </summary>
        public bool? IsProvince { get; set; }

        /// <summary>
        /// 来源渠道
        /// </summary>
        public string? Channel { get; set; }

        /// <summary>
        /// 受理类型
        /// </summary>
        public string? AcceptType { get; set; }

        /// <summary>
        /// 热点分类关键词
        /// </summary>
        public string? Hotspot { get; set; }

        /// <summary>
        /// 最近派单员
        /// </summary>
        public string? CenterToOrgHandlerName { get; set; }

        /// <summary>
        /// 退回节点
        /// </summary>
        public string? SendBackStepName { get; set; }

        /// <summary>
        /// 退回时差
        /// </summary>
        public int? SendBackTimeDifference { get; set; }

        /// <summary>
        /// 受理人
        /// </summary>
        public string? NameOrNo { get; set; }

        /// <summary>
        /// 一级部门
        /// </summary>
        public string? OrgLevelOneName { get; set; }

        /// <summary>
        /// 受理内容
        /// </summary>
        public string? Content { get; set; }

        /// <summary>
        /// 申请人
        /// </summary>
        public string? ApplyName { get; set; }

        /// <summary>
        /// 申请理由
        /// </summary>
        public string? Opinion { get; set; }
    }
}
