<template>
  <Teleport to="body">
    <Transition name="shared-confirm">
      <div
        v-if="open"
        class="shared-confirm-mask"
        role="presentation"
        @click.self="handleMaskClose"
      >
        <section
          class="shared-confirm-card"
          :class="cardClasses"
          role="alertdialog"
          aria-modal="true"
          :aria-labelledby="titleId"
          @click.stop
        >
          <span v-if="badge" class="shared-confirm-badge" :class="badgeTone">
            {{ badge }}
          </span>
          <h4 :id="titleId">{{ title }}</h4>
          <p v-if="description">{{ description }}</p>

          <div v-if="$slots.default" class="shared-confirm-body">
            <slot></slot>
          </div>

          <div class="shared-confirm-actions">
            <button
              type="button"
              class="shared-confirm-btn cancel"
              :disabled="busy"
              @click="handleCancel"
            >
              {{ cancelText }}
            </button>
            <button
              type="button"
              class="shared-confirm-btn confirm"
              :class="confirmTone"
              :disabled="busy"
              @click="$emit('confirm')"
            >
              <i v-if="confirmIcon" :class="busy ? 'mdi mdi-loading mdi-spin' : confirmIcon"></i>
              <span>{{ busy ? busyText : confirmText }}</span>
            </button>
          </div>
        </section>
      </div>
    </Transition>
  </Teleport>
</template>

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

const props = defineProps({
  open: { type: Boolean, default: false },
  badge: { type: String, default: '' },
  badgeTone: { type: String, default: 'info' },
  title: { type: String, required: true },
  description: { type: String, default: '' },
  cancelText: { type: String, default: '取消' },
  confirmText: { type: String, default: '确认' },
  busyText: { type: String, default: '处理中...' },
  confirmTone: { type: String, default: 'primary' },
  confirmIcon: { type: String, default: '' },
  busy: { type: Boolean, default: false },
  closeOnMask: { type: Boolean, default: true },
  size: { type: String, default: 'default' },
  actionsAlign: { type: String, default: 'end' }
})

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

const titleId = computed(() => `shared-confirm-title-${instance?.uid || 'dialog'}`)
const cardClasses = computed(() => [
  `shared-confirm-card--${props.size}`,
  `shared-confirm-actions-${props.actionsAlign}`
])

function handleMaskClose() {
  if (!props.closeOnMask || props.busy) {
    return
  }

  emit('close')
}

function handleCancel() {
  if (props.busy) {
    return
  }

  emit('cancel')
  emit('close')
}
</script>

<style scoped>
.shared-confirm-mask {
  position: fixed;
  inset: 0;
  z-index: 10020;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.32);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.shared-confirm-card {
  width: min(480px, 100%);
  display: grid;
  gap: 14px;
  padding: 24px;
  border: 1px solid rgba(var(--theme-primary-rgb, 58, 124, 165), 0.14);
  border-radius: 8px;
  background:
    radial-gradient(circle at top left, rgba(var(--theme-primary-rgb, 58, 124, 165), 0.10), transparent 36%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(247, 250, 252, 0.98));
  box-shadow: 0 28px 56px rgba(15, 23, 42, 0.18);
}

.shared-confirm-badge {
  display: inline-flex;
  width: fit-content;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
}

.shared-confirm-badge.info {
  background: var(--theme-primary-soft);
  color: var(--theme-primary-active);
}

.shared-confirm-badge.warning {
  background: var(--warning-soft);
  color: var(--warning-active);
}

.shared-confirm-badge.danger {
  background: var(--danger-soft);
  color: var(--danger-hover);
}

.shared-confirm-card h4 {
  margin: 0;
  color: #0f172a;
  font-size: 22px;
  line-height: 1.35;
  font-weight: 800;
}

.shared-confirm-card p {
  margin: 0;
  color: #5b6b83;
  font-size: 14px;
  line-height: 1.7;
}

.shared-confirm-body {
  display: grid;
  gap: 10px;
}

.shared-confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.shared-confirm-card.shared-confirm-actions-start .shared-confirm-actions {
  justify-content: flex-start;
}

.shared-confirm-card.shared-confirm-actions-center .shared-confirm-actions {
  justify-content: center;
}

.shared-confirm-card.shared-confirm-actions-end .shared-confirm-actions {
  justify-content: flex-end;
}

.shared-confirm-btn {
  min-width: 140px;
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 16px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 800;
  transition: transform 160ms ease, box-shadow 160ms ease, border-color 160ms ease, background 160ms ease;
}

.shared-confirm-btn.cancel {
  border: 1px solid #d7e0ea;
  background: rgba(255, 255, 255, 0.92);
  color: #475569;
}

.shared-confirm-btn.confirm {
  border: 1px solid transparent;
  color: #fff;
}

.shared-confirm-btn.confirm.primary {
  background: linear-gradient(135deg, var(--theme-primary), var(--theme-primary-active));
  box-shadow: 0 12px 24px rgba(var(--theme-primary-rgb, 58, 124, 165), 0.20);
}

.shared-confirm-btn.confirm.danger {
  background: linear-gradient(135deg, var(--danger), var(--danger-hover));
  box-shadow: 0 12px 24px rgba(var(--danger-rgb), 0.22);
}

.shared-confirm-btn.cancel:hover:not(:disabled) {
  border-color: rgba(var(--theme-primary-rgb, 58, 124, 165), 0.3);
  color: var(--theme-primary-active);
}

.shared-confirm-btn.confirm:hover:not(:disabled) {
  transform: translateY(-1px);
}

.shared-confirm-btn:disabled {
  cursor: not-allowed;
  opacity: 0.68;
  box-shadow: none;
  transform: none;
}

.shared-confirm-enter-active,
.shared-confirm-leave-active {
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.shared-confirm-enter-from,
.shared-confirm-leave-to {
  opacity: 0;
}

.shared-confirm-enter-from .shared-confirm-card,
.shared-confirm-leave-to .shared-confirm-card {
  transform: translateY(8px) scale(0.98);
}

.shared-confirm-card--compact {
  width: min(360px, 100%);
  gap: 8px;
  padding: 16px;
  border-radius: 6px;
  background: #fff;
}

.shared-confirm-card--compact h4 {
  font-size: 15px;
  line-height: 1.35;
}

.shared-confirm-card--compact p {
  font-size: 12px;
  line-height: 1.55;
}

.shared-confirm-card--compact .shared-confirm-actions {
  gap: 6px;
}

.shared-confirm-card--compact .shared-confirm-btn {
  min-width: 76px;
  min-height: 30px;
  padding: 0 10px;
  border-radius: 4px;
  font-size: 12px;
}

@media (max-width: 720px) {
  .shared-confirm-mask {
    padding: 18px;
  }

  .shared-confirm-card {
    padding: 20px;
    border-radius: 8px;
  }

  .shared-confirm-card h4 {
    font-size: 19px;
  }

  .shared-confirm-actions {
    flex-direction: column;
  }

  .shared-confirm-btn {
    width: 100%;
  }
}
</style>
