<template>
  <div class="delayed-confirm-overlay" v-if="visible" @click="handleCancel">
    <div class="delayed-confirm-dialog" @click.stop>
      <div class="dialog-header">
        <div class="warning-icon">
          <i class="fas fa-exclamation-triangle"></i>
        </div>
        <h3 class="dialog-title">{{ title }}</h3>
      </div>
      
      <div class="dialog-content">
        <p class="dialog-message">{{ message }}</p>
        <div v-if="showCountdown" class="countdown-section">
          <div class="countdown-circle">
            <svg class="countdown-svg" viewBox="0 0 100 100">
              <circle
                class="countdown-bg"
                cx="50"
                cy="50"
                r="45"
              />
              <circle
                class="countdown-progress"
                cx="50"
                cy="50"
                r="45"
                :style="{ strokeDasharray: circumference, strokeDashoffset: dashOffset }"
              />
            </svg>
            <span class="countdown-text">{{ remainingSeconds }}</span>
          </div>
          <p class="countdown-desc">{{ remainingSeconds }}秒后可以确认操作</p>
        </div>
      </div>
      
      <div class="dialog-actions">
        <button 
          class="dialog-btn dialog-btn-cancel" 
          @click="handleCancel"
        >
          {{ cancelText }}
        </button>
        <button 
          class="dialog-btn dialog-btn-confirm" 
          @click="handleConfirm"
          :disabled="isCountingDown"
          :class="{ 
            danger: type === 'danger',
            disabled: isCountingDown 
          }"
        >
          <i v-if="isCountingDown" class="fas fa-spinner fa-spin"></i>
          {{ isCountingDown ? `等待 ${remainingSeconds}s` : confirmText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onUnmounted } from 'vue'

const props = defineProps({
  title: {
    type: String,
    default: '确认操作'
  },
  message: {
    type: String,
    required: true
  },
  confirmText: {
    type: String,
    default: '确认'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  type: {
    type: String,
    default: 'normal', // 'normal' | 'danger'
  },
  delaySeconds: {
    type: Number,
    default: 5 // 默认5秒延时
  }
})

const emit = defineEmits(['confirm', 'cancel'])

const visible = ref(false)
const isCountingDown = ref(false)
const remainingSeconds = ref(0)
let countdownInterval = null

// SVG圆形进度条相关计算
const circumference = computed(() => 2 * Math.PI * 45)
const dashOffset = computed(() => {
  const progress = (props.delaySeconds - remainingSeconds.value) / props.delaySeconds
  return circumference.value * (1 - progress)
})

const showCountdown = computed(() => isCountingDown.value && remainingSeconds.value > 0)

const show = () => {
  visible.value = true
  startCountdown()
}

const hide = () => {
  visible.value = false
  stopCountdown()
}

const startCountdown = () => {
  isCountingDown.value = true
  remainingSeconds.value = props.delaySeconds
  
  countdownInterval = setInterval(() => {
    remainingSeconds.value--
    
    if (remainingSeconds.value <= 0) {
      isCountingDown.value = false
      stopCountdown()
    }
  }, 1000)
}

const stopCountdown = () => {
  if (countdownInterval) {
    clearInterval(countdownInterval)
    countdownInterval = null
  }
  isCountingDown.value = false
  remainingSeconds.value = 0
}

const handleConfirm = () => {
  if (isCountingDown.value) return
  
  emit('confirm')
  hide()
}

const handleCancel = () => {
  emit('cancel')
  hide()
}

// 清理定时器
onUnmounted(() => {
  stopCountdown()
})

defineExpose({
  show,
  hide
})
</script>

<style scoped>
.delayed-confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(12px);
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease;
}

.delayed-confirm-dialog {
  background: var(--bg-card);
  border-radius: 20px;
  padding: 0;
  width: 90%;
  max-width: 420px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.4);
  animation: slideUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  overflow: hidden;
  border: 1px solid var(--border-light);
}

.dialog-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px 24px 20px;
  background: var(--bg-gradient-1);
  border-bottom: 1px solid var(--border-lighter);
}

.warning-icon {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #ff6b6b, #ff5252);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  box-shadow: 0 8px 20px rgba(255, 107, 107, 0.3);
}

.warning-icon i {
  font-size: 24px;
  color: white;
}

.dialog-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
  text-align: center;
}

.dialog-content {
  padding: 24px;
  text-align: center;
}

.dialog-message {
  font-size: 16px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin: 0 0 24px 0;
  text-align: center;
}

.countdown-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.countdown-circle {
  position: relative;
  width: 80px;
  height: 80px;
}

.countdown-svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.countdown-bg {
  fill: none;
  stroke: var(--border-lighter);
  stroke-width: 4;
}

.countdown-progress {
  fill: none;
  stroke: var(--accent-red);
  stroke-width: 4;
  stroke-linecap: round;
  transition: stroke-dashoffset 1s linear;
}

.countdown-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  font-weight: 700;
  color: var(--accent-red);
}

.countdown-desc {
  font-size: 14px;
  color: var(--text-tertiary);
  margin: 0;
}

.dialog-actions {
  display: flex;
  gap: 12px;
  padding: 20px 24px 30px;
}

.dialog-btn {
  flex: 1;
  padding: 14px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.dialog-btn-cancel {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  border: 2px solid var(--border-light);
}

.dialog-btn-cancel:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.dialog-btn-confirm {
  background: var(--accent-red);
  color: white;
  border: 2px solid transparent;
}

.dialog-btn-confirm:hover:not(.disabled) {
  background: var(--accent-red-hover);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
}

.dialog-btn-confirm.danger {
  background: #ff4444;
}

.dialog-btn-confirm.danger:hover:not(.disabled) {
  background: #ff2222;
  box-shadow: 0 4px 12px rgba(255, 68, 68, 0.4);
}

.dialog-btn-confirm.disabled {
  background: var(--bg-secondary);
  color: var(--text-tertiary);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
  border-color: var(--border-light);
}

@keyframes fadeIn {
  from { 
    opacity: 0; 
  }
  to { 
    opacity: 1; 
  }
}

@keyframes slideUp {
  from { 
    opacity: 0;
    transform: translateY(30px) scale(0.9);
  }
  to { 
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 响应式 */
@media (max-width: 480px) {
  .delayed-confirm-dialog {
    width: 95%;
    max-width: none;
    border-radius: 16px;
  }
  
  .dialog-header {
    padding: 24px 20px 16px;
  }
  
  .warning-icon {
    width: 50px;
    height: 50px;
    margin-bottom: 12px;
  }
  
  .warning-icon i {
    font-size: 20px;
  }
  
  .dialog-title {
    font-size: 18px;
  }
  
  .dialog-content {
    padding: 20px;
  }
  
  .dialog-message {
    font-size: 15px;
  }
  
  .countdown-circle {
    width: 70px;
    height: 70px;
  }
  
  .countdown-text {
    font-size: 20px;
  }
  
  .dialog-actions {
    padding: 16px 20px 24px;
    flex-direction: column;
  }
  
  .dialog-btn {
    width: 100%;
    padding: 12px 20px;
    font-size: 15px;
  }
}
</style>