<!-- components/ConfirmDialog.vue -->
<template>
  <Teleport to="body">
    <Transition name="dialog-fade">
      <div v-if="visible" class="dialog-overlay" @click="handleOverlayClick">
        <Transition name="dialog-scale">
          <div v-if="visible" class="dialog-container" @click.stop>
            <div class="dialog-content">
              <!-- 图标 -->
              <div class="dialog-icon" :class="`dialog-icon-${type}`">
                <svg v-if="type === 'warning'" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
                <svg v-else-if="type === 'danger'" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
                <svg v-else viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </div>

              <!-- 标题 -->
              <h3 class="dialog-title">{{ title }}</h3>

              <!-- 消息 -->
              <p v-if="message" class="dialog-message">{{ message }}</p>

              <!-- 按钮组 -->
              <div class="dialog-actions">
                <button
                  class="dialog-btn dialog-btn-cancel"
                  @click="handleCancel"
                >
                  {{ cancelText }}
                </button>
                <button
                  class="dialog-btn dialog-btn-confirm"
                  :class="`dialog-btn-${type}`"
                  @click="handleConfirm"
                >
                  {{ confirmText }}
                </button>
              </div>
            </div>
          </div>
        </Transition>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  type: {
    type: String,
    default: 'warning', // 'warning' | 'danger' | 'info'
    validator: (value) => ['warning', 'danger', 'info'].includes(value)
  },
  title: {
    type: String,
    required: true
  },
  message: {
    type: String,
    default: ''
  },
  confirmText: {
    type: String,
    default: '确定'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  closeOnClickOverlay: {
    type: Boolean,
    default: true
  }
})

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

const visible = ref(false)

function show() {
  visible.value = true
}

function close() {
  visible.value = false
  emit('close')
}

function handleConfirm() {
  emit('confirm')
  close()
}

function handleCancel() {
  emit('cancel')
  close()
}

function handleOverlayClick() {
  if (props.closeOnClickOverlay) {
    handleCancel()
  }
}

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

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 99998;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.dialog-container {
  position: relative;
  z-index: 99999;
  width: 100%;
  max-width: 400px;
}

.dialog-content {
  background: #fff;
  border-radius: 20px;
  padding: 32px 28px 24px;
  box-shadow:
    0 20px 25px -5px rgba(0, 0, 0, 0.1),
    0 10px 10px -5px rgba(0, 0, 0, 0.04);
  text-align: center;
}

/* 图标 */
.dialog-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-icon svg {
  width: 32px;
  height: 32px;
}

.dialog-icon-warning {
  background: #fef3c7;
  color: #f59e0b;
}

.dialog-icon-danger {
  background: #fee2e2;
  color: #ef4444;
}

.dialog-icon-info {
  background: #dbeafe;
  color: #3b82f6;
}

/* 标题 */
.dialog-title {
  font-size: 20px;
  font-weight: 700;
  color: #111827;
  margin: 0 0 12px;
  line-height: 1.3;
}

/* 消息 */
.dialog-message {
  font-size: 15px;
  color: #6b7280;
  line-height: 1.6;
  margin: 0 0 24px;
}

/* 按钮组 */
.dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.dialog-btn {
  flex: 1;
  height: 44px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
  outline: none;
}

.dialog-btn-cancel {
  background: #f3f4f6;
  color: #374151;
}

.dialog-btn-cancel:hover {
  background: #e5e7eb;
}

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

.dialog-btn-confirm {
  color: #fff;
}

.dialog-btn-warning {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
}

.dialog-btn-warning:hover {
  box-shadow: 0 6px 16px rgba(245, 158, 11, 0.4);
  transform: translateY(-1px);
}

.dialog-btn-danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.dialog-btn-danger:hover {
  box-shadow: 0 6px 16px rgba(239, 68, 68, 0.4);
  transform: translateY(-1px);
}

.dialog-btn-info {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.dialog-btn-info:hover {
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
  transform: translateY(-1px);
}

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

/* 动画 */
.dialog-fade-enter-active,
.dialog-fade-leave-active {
  transition: opacity 0.25s ease;
}

.dialog-fade-enter-from,
.dialog-fade-leave-to {
  opacity: 0;
}

.dialog-scale-enter-active {
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.dialog-scale-leave-active {
  transition: all 0.2s ease;
}

.dialog-scale-enter-from {
  opacity: 0;
  transform: scale(0.9) translateY(-20px);
}

.dialog-scale-leave-to {
  opacity: 0;
  transform: scale(0.95);
}

/* 响应式 */
@media (max-width: 640px) {
  .dialog-content {
    padding: 28px 24px 20px;
  }

  .dialog-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 16px;
  }

  .dialog-icon svg {
    width: 28px;
    height: 28px;
  }

  .dialog-title {
    font-size: 18px;
  }

  .dialog-message {
    font-size: 14px;
  }

  .dialog-btn {
    height: 40px;
    font-size: 14px;
  }
}
</style>
