<template>
  <div v-if="visible" class="modal-overlay" @click="handleOverlayClick">
    <div class="modal" :class="[animationClass, modalTypeClass]">
      <!-- 图标 -->
      <div class="modal-icon">
        <div class="icon-circle" :class="iconCircleClass">
          <span class="icon-symbol" :class="iconSymbolClass">{{ iconSymbol }}</span>
        </div>
      </div>
      
      <!-- 标题 -->
      <div class="modal-title" v-if="title">{{ title }}</div>
      
      <!-- 消息内容 -->
      <div class="modal-message">{{ message }}</div>
      
      <!-- 按钮 -->
      <div class="modal-actions">
        <button class="modal-btn" :class="buttonClass" @click="confirm">{{ confirmText }}</button>
      </div>
    </div>
  </div>
</template>

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

const props = defineProps({
  title: {
    type: String,
    default: 'Notice'
  },
  message: {
    type: String,
    required: true
  },
  type: {
    type: String,
    default: 'error', // 'error' | 'warning' | 'success' | 'info'
    validator: (value) => ['error', 'warning', 'success', 'info'].includes(value)
  },
  confirmText: {
    type: String,
    default: 'OK'
  },
  clickToClose: {
    type: Boolean,
    default: true
  },
  duration: {
    type: Number,
    default: null // 自动关闭时间（毫秒）
  }
})

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

const visible = ref(false)
const animationClass = ref('')

// 根据类型计算图标和样式
const iconSymbol = computed(() => {
  const symbols = {
    error: '✕',
    warning: '⚠',
    success: '✓',
    info: 'i'
  }
  return symbols[props.type] || symbols.error
})

const modalTypeClass = computed(() => `modal-${props.type}`)

const iconCircleClass = computed(() => `icon-circle-${props.type}`)

const iconSymbolClass = computed(() => `icon-symbol-${props.type}`)

const buttonClass = computed(() => `modal-btn-${props.type}`)

const show = () => {
  visible.value = true
  animationClass.value = 'modal-enter'
  
  // 如果设置了自动关闭时间
  if (props.duration && props.duration > 0) {
    setTimeout(() => {
      close()
    }, props.duration)
  }
}

const close = () => {
  animationClass.value = 'modal-leave'
  setTimeout(() => {
    visible.value = false
    emit('close')
  }, 200)
}

const confirm = () => {
  emit('confirm')
  close()
}

const handleOverlayClick = () => {
  if (props.clickToClose) {
    close()
  }
}

// 组件挂载时显示
onMounted(() => {
  show()
})

// 暴露方法给父组件
defineExpose({
  show,
  close
})
</script>

<style scoped>
/* 基础模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 20px;
}

.modal {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 25px rgba(0, 0, 0, 0.1), 0 10px 10px rgba(0, 0, 0, 0.04);
  width: 100%;
  max-width: 320px;
  padding: 24px 20px 20px;
  text-align: center;
  position: relative;
}

/* 图标区域 */
.modal-icon {
  margin-bottom: 16px;
  display: flex;
  justify-content: center;
}

.icon-circle {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: iconPulse 0.6s ease-out;
  border: 3px solid;
}

.icon-symbol {
  font-size: 24px;
  font-weight: bold;
  line-height: 1;
}

/* 标题 */
.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #1F2937;
  margin-bottom: 12px;
  line-height: 1.4;
}

/* 消息内容 */
.modal-message {
  font-size: 14px;
  color: #6B7280;
  line-height: 1.5;
  margin-bottom: 20px;
  word-wrap: break-word;
}

/* 按钮区域 */
.modal-actions {
  display: flex;
  justify-content: center;
}

.modal-btn {
  border: none;
  border-radius: 8px;
  padding: 10px 32px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 80px;
  color: white;
}

.modal-btn:active {
  transform: scale(0.98);
}

/* Error 类型样式 */
.modal-error .icon-circle-error {
  background-color: #FEE2E2;
  border-color: #FECACA;
}

.modal-error .icon-symbol-error {
  color: #DC2626;
}

.modal-error .modal-btn-error {
  background-color: #DC2626;
}

.modal-error .modal-btn-error:hover {
  background-color: #B91C1C;
}

.modal-error .modal-btn-error:active {
  background-color: #991B1B;
}

/* Warning 类型样式 */
.modal-warning .icon-circle-warning {
  background-color: #FEF3C7;
  border-color: #FDE68A;
}

.modal-warning .icon-symbol-warning {
  color: #D97706;
}

.modal-warning .modal-btn-warning {
  background-color: #D97706;
}

.modal-warning .modal-btn-warning:hover {
  background-color: #B45309;
}

.modal-warning .modal-btn-warning:active {
  background-color: #92400E;
}

/* Success 类型样式 */
.modal-success .icon-circle-success {
  background-color: #D1FAE5;
  border-color: #A7F3D0;
}

.modal-success .icon-symbol-success {
  color: #059669;
}

.modal-success .modal-btn-success {
  background-color: #059669;
}

.modal-success .modal-btn-success:hover {
  background-color: #047857;
}

.modal-success .modal-btn-success:active {
  background-color: #065F46;
}

/* Info 类型样式 */
.modal-info .icon-circle-info {
  background-color: #DBEAFE;
  border-color: #BFDBFE;
}

.modal-info .icon-symbol-info {
  color: #2563EB;
}

.modal-info .modal-btn-info {
  background-color: #2563EB;
}

.modal-info .modal-btn-info:hover {
  background-color: #1D4ED8;
}

.modal-info .modal-btn-info:active {
  background-color: #1E40AF;
}

/* 动画效果 */
.modal-enter {
  animation: modalEnter 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-leave {
  animation: modalLeave 0.2s ease-in;
}

@keyframes modalEnter {
  0% {
    opacity: 0;
    transform: scale(0.7) translateY(-10px);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes modalLeave {
  0% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
  100% {
    opacity: 0;
    transform: scale(0.9) translateY(-5px);
  }
}

@keyframes iconPulse {
  0% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .modal {
    max-width: 280px;
    padding: 20px 16px 16px;
  }
  
  .icon-circle {
    width: 48px;
    height: 48px;
  }
  
  .icon-symbol {
    font-size: 20px;
  }
  
  .modal-title {
    font-size: 16px;
  }
  
  .modal-message {
    font-size: 13px;
  }
}
</style>
