using Hotline.Share.Enums.Order;
using XF.Utility.EnumExtensions;

namespace Hotline.Share.Dtos.JudicialManagement
{
    public class EnforcementOrderListDto
    {
        public string Id { get; set; }
        public string WorkflowId { get; set; }

        /// <summary>
        /// 工单状态
        /// </summary>
        public EOrderStatus Status { get; set; }
        public string StatusText => Status.GetDescription();

        /// <summary>
        /// 过期状态
        /// </summary>
        public EExpiredStatus? ExpiredStatus => CalculateExpiredState();

        /// <summary>
        /// 过期状态
        /// </summary>
        public string ExpiredStatusText => ExpiredStatus?.GetDescription() ?? string.Empty;

        /// <summary>
        /// 工单编码（20220101000001）
        /// </summary>
        public string No { get; set; }

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

        /// <summary>
        /// 来电号码
        /// </summary>
        public string? FromPhone { get; set; }

        /// <summary>
        /// 转接号码（转接来源）
        /// </summary>
        public string? TransferPhone { get; set; }

        /// <summary>
        /// 来电/信人姓名
        /// </summary>
        public string? FromName { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        public string? Contact { get; set; }

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

        /// <summary>
        /// 紧急程度
        /// </summary>
        public EEmergencyLevel EmergencyLevel { get; set; }

        public string Title { get; set; }

        /// <summary>
        /// 热点
        /// </summary>
        public string? HotspotId { get; set; }
        public string? HotspotName { get; set; }
        public string? HotspotSpliceName { get; set; }

        public DateTime CreationTime { get; set; }

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

        /// <summary>
        /// 同步省工单编号和省上传下来的工单都用这个字段
        /// </summary>
        public string? ProvinceNo { get; set; }

        /// <summary>
        /// 实际办理节点名称（会签状态此字段保存最外层会签发起节点名称）
        /// </summary>
        public string? ActualHandleStepName { get; set; }

        /// <summary>
        /// 实际办理时间
        /// </summary>
        public DateTime? ActualHandleTime { get; set; }

        /// <summary>
        /// 超期时间（期满时间）
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 即将超期时间
        /// </summary>
        public DateTime? NearlyExpiredTime { get; set; }

        /// <summary>
        /// 归档时间（暂为流程结束时间，因流程结束自动归档）
        /// </summary>
        public DateTime? FiledTime { get; set; }

        /// <summary>
        /// 工单开始时间(受理/接办时间=流程开启时间)
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 实际办理部门名称
        /// </summary>
        public string? ActualHandleOrgName { get; set; }

        /// <summary>
        /// 实际办理部门编码
        /// </summary>
        public string? ActualHandleOrgCode { get; set; }

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

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

        /// <summary>
        /// 实际办理人名称
        /// </summary>
        public string? ActualHandlerName { get; set; }

        /// <summary>
        /// 是否行政执法类
        /// </summary>
        public bool? IsEnforcementOrder { get; set; }

        /// <summary>
        /// 是否推诿
        /// </summary>
        public bool? IsPassTheBuckOrder { get; set; }

        /// <summary>
        /// 线索是否属实
        /// </summary>
        public bool? IsTheClueTrue { get; set; }

        /// <summary>
        /// 诉事项类型Id
        /// </summary>
        public string? EventTypeId { get; set; }

        /// <summary>
        /// 诉事项类型名称
        /// </summary>
        public string? EventTypeName { get; set; }

        /// <summary>
        /// 诉事项类型名称
        /// </summary>
        public string? EventTypeSpliceName { get; set; }

        /// <summary>
        /// 是否显示修改按钮
        /// </summary>
        public bool IsShowUpdateButton { get; set; }

        public EExpiredStatus? CalculateExpiredState()
        {
            DateTime? dateTime = DateTime.Now;
            if (Status >= EOrderStatus.Filed)
            {
                dateTime = ActualHandleTime;
            }
            //ExpiredStatus
            if (ExpiredTime.HasValue)
            {
                if (dateTime < NearlyExpiredTime)
                {
                    return EExpiredStatus.Normal;
                }
                else if (dateTime > NearlyExpiredTime && dateTime < ExpiredTime)
                {
                    return EExpiredStatus.GoingToExpired;
                }
                else
                {
                    return EExpiredStatus.Expired;
                }
            }
            return null;
        }
    }

    public class EnforcementOrgSatisfactionOrderListDto : EnforcementOrderListDto
    {
        /// <summary>
        /// 回访主表ID
        /// </summary>
        public string VisitId { get; set; }
    }

    public class EnforcementOldOrder
    {
        /// <summary>
        /// 工单Id
        /// </summary>
        public string Id { get; set; }

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

        /// <summary>
        /// 热点
        /// </summary>
        public string HotspotId { get; set; }

        /// <summary>
        /// 办结时间
        /// </summary>
        public DateTime? HandleTime { get; set; }

        /// <summary>
        /// 办理一级部门名称
        /// </summary>
        public string OrgLevelOneCode { get; set; }

        /// <summary>
        /// 实际办理部门
        /// </summary>
        public string ActualHandleOrgCode { get; set; }
    }
}
