<script setup lang="ts">

withDefaults(
  defineProps<{
    show: boolean;
    title: string;
    message: string;
    confirmText?: string;
    cancelText?: string;
    danger?: boolean;
  }>(),
  {
    confirmText: "确认",
    cancelText: "取消",
    danger: false,
  },
);

const emit = defineEmits<{
  confirm: [];
  cancel: [];
}>();

function onOverlayKeydown(event: KeyboardEvent) {
  if (event.key === "Escape") {
    emit("cancel");
  }
}
</script>

<template>
  <Teleport to="body">
    <div
      v-if="show"
      class="confirm-overlay"
      role="presentation"
      @click.self="emit('cancel')"
      @keydown="onOverlayKeydown"
    >
      <section
        class="confirm-shell"
        role="alertdialog"
        aria-modal="true"
        :aria-label="title"
      >
        <header class="confirm-header">
          <h2>{{ title }}</h2>
        </header>

        <p class="confirm-message">{{ message }}</p>

        <footer class="confirm-footer">
          <button
            class="confirm-btn confirm-btn-cancel"
            type="button"
            @click="emit('cancel')"
          >
            {{ cancelText }}
          </button>
          <button
            class="confirm-btn"
            :class="danger ? 'confirm-btn-danger' : 'confirm-btn-primary'"
            type="button"
            @click="emit('confirm')"
          >
            {{ confirmText }}
          </button>
        </footer>
      </section>
    </div>
  </Teleport>
</template>

<style scoped>
.confirm-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  background: var(--app-overlay);
  backdrop-filter: blur(8px);
}

.confirm-shell {
  width: min(400px, calc(100vw - 48px));
  overflow: hidden;
  border: 1px solid var(--app-border-strong);
  border-radius: 12px;
  background: var(--app-surface);
  box-shadow:
    0 24px 70px var(--app-shadow),
    0 2px 10px color-mix(in srgb, var(--app-shadow) 46%, transparent);
}

.confirm-header {
  display: flex;
  align-items: center;
  min-height: 48px;
  padding: 0 20px;
  border-bottom: 1px solid var(--app-border);
}

.confirm-header h2 {
  margin: 0;
  color: var(--app-text);
  font-size: 14px;
  font-weight: 700;
}

.confirm-message {
  margin: 0;
  padding: 20px;
  color: var(--app-text-soft);
  font-size: 13px;
  line-height: 1.6;
}

.confirm-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--app-border);
  background: var(--app-panel-bg);
}

.confirm-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 72px;
  height: 32px;
  padding: 0 16px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    color 0.15s ease;
}

.confirm-btn-cancel {
  background: var(--app-hover);
  color: var(--app-text-soft);
}

.confirm-btn-cancel:hover {
  background: var(--app-border);
  color: var(--app-text);
}

.confirm-btn-primary {
  background: var(--app-accent);
  color: #fff;
}

.confirm-btn-primary:hover {
  opacity: 0.88;
}

.confirm-btn-danger {
  background: #e53e3e;
  color: #fff;
}

.confirm-btn-danger:hover {
  background: #c53030;
}
</style>
