import { Tag } from 'antd'
import { useEffect, useState } from 'react'

export function DeadlineCountdown({
  deadlineAt,
  showExpired: _showExpired = false,
}: {
  deadlineAt: string
  showExpired?: boolean
}) {
  const [now, setNow] = useState(() => Date.now())

  useEffect(() => {
    const timer = window.setInterval(() => setNow(Date.now()), 1000)
    return () => window.clearInterval(timer)
  }, [])

  const remainingMs = new Date(deadlineAt).getTime() - now
  if (remainingMs <= 0) {
    return <Tag color="red">已超时</Tag>
  }
  const totalSeconds = Math.floor(remainingMs / 1000)
  const minutes = Math.floor(totalSeconds / 60)
  const seconds = totalSeconds % 60
  const label = minutes > 0 ? `${minutes} 分 ${seconds} 秒` : `${seconds} 秒`
  const color = remainingMs < 10 * 60 * 1000 ? 'orange' : 'blue'
  return <Tag color={color}>剩余 {label}</Tag>
}

export function formatTimeoutPolicyLabel(policy?: string) {
  if (policy === 'cancel_release') return '超时取消退押金'
  if (policy === 'cancel_deduct') return '超时取消扣押金'
  return '超时退回大厅'
}
