<template>
  <Teleport to="body">
    <dialog ref="dialogRef" class="confirm-overlay" @click.self="handleCancel" @close="handleCancel">
      <Transition name="confirm-scale-in">
        <div v-if="showContent" class="confirm-dialog">
          <header>
            <span class="confirm-icon">{{ icon }}</span>
            {{ title }}
          </header>
          <div class="confirm-body">
            <p v-html="message"></p>
            <p v-if="hint" class="confirm-hint">{{ hint }}</p>
          </div>
          <footer>
            <button type="button" class="confirm-btn-cancel" @click="handleCancel">{{ cancelText }}</button>
            <button
              type="button"
              :class="danger ? 'confirm-btn-danger' : 'confirm-btn-primary'"
              @click="handleConfirm"
            >{{ confirmText }}</button>
          </footer>
        </div>
      </Transition>
    </dialog>
  </Teleport>
</template>

<script setup>
import { ref, watch } from 'vue'
import { lockScroll, unlockScroll } from '../utils/scrollLock'

const props = defineProps({
  open: Boolean,
  title: { type: String, default: '确认操作' },
  message: { type: String, default: '' },
  hint: { type: String, default: '' },
  icon: { type: String, default: '⚠️' },
  confirmText: { type: String, default: '确认' },
  cancelText: { type: String, default: '取消' },
  danger: { type: Boolean, default: false },
})

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

const dialogRef = ref(null)
const showContent = ref(false)

watch(
  () => props.open,
  (val) => {
    if (!dialogRef.value) return
    if (val && !dialogRef.value.open) {
      showContent.value = true
      dialogRef.value.showModal()
      lockScroll()
    } else if (!val && dialogRef.value.open) {
      showContent.value = false
      dialogRef.value.close()
      unlockScroll()
    }
  },
)

function handleConfirm() {
  showContent.value = false
  try { dialogRef.value?.close() } catch {}
  emit('confirm')
}

function handleCancel() {
  showContent.value = false
  try { dialogRef.value?.close() } catch {}
  unlockScroll()
  emit('cancel')
}
</script>

<style scoped>
.confirm-overlay {
  position: fixed;
  inset: 0;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483647;
  border: none;
  padding: 0;
  overflow: hidden;
  border-radius: 14px;
}
.confirm-overlay::backdrop {
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(3px);
}

.confirm-dialog {
  background: #fff;
  border-radius: 14px;
  padding: 24px 28px;
  min-width: 400px;
  max-width: 90vw;
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.25);
  border: none;
  overflow: visible;
  max-height: calc(100vh - 80px);
}

.confirm-dialog header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5;
}

.confirm-icon {
  font-size: 20px;
  flex-shrink: 0;
}

.confirm-body {
  font-size: 15px;
  color: #606266;
  line-height: 1.65;
  margin-bottom: 8px;
}

.confirm-body :deep(strong) {
  color: #f56c6c;
  font-weight: 700;
}

.confirm-hint {
  font-size: 13px !important;
  color: #f56c6c !important;
  background: #fef0f0;
  padding: 10px 14px;
  border-radius: 8px;
  border-left: 3px solid #f56c6c;
  margin-top: 10px;
}

.confirm-dialog footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid #ebeef5;
}

.confirm-dialog footer button {
  padding: 9px 22px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.25s ease;
  border: none;
}

.confirm-btn-cancel {
  background: #fff;
  color: #606266;
  border: 1px solid #dcdfe6 !important;
}
.confirm-btn-cancel:hover {
  background: #f5f7fa;
  border-color: #c0c4cc !important;
}

.confirm-btn-danger {
  background: linear-gradient(135deg, #f56c6c 0%, #e74c3c 100%) !important;
  color: #fff !important;
}
.confirm-btn-danger:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 18px rgba(245, 108, 108, 0.35);
}

.confirm-btn-primary {
  background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%) !important;
  color: #fff !important;
}
.confirm-btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 18px rgba(99, 102, 241, 0.35);
}

/* 动画 */
.confirm-scale-in-enter-active {
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.confirm-scale-in-leave-active {
  transition: all 0.2s ease;
}
.confirm-scale-in-enter-from {
  opacity: 0;
  transform: scale(0.85) translateY(15px);
}
.confirm-scale-in-leave-to {
  opacity: 0;
  transform: scale(0.95) translateY(-8px);
}
</style>
