<template>
  <div class="document-archive-actions" :class="statusClass">
    <span class="document-archive-status" aria-live="polite">
      <span class="document-archive-status-dot" aria-hidden="true"></span>
      {{ statusText }}
    </span>

    <div class="document-archive-buttons">
      <button
        class="document-archive-button document-archive-button-preview"
        type="button"
        :disabled="!isReady"
        :title="isReady ? '预览PDF' : 'PDF未生成，暂不能预览'"
        @click="emit('preview')"
      >
        <span class="document-archive-icon" aria-hidden="true">PDF</span>
        预览PDF
      </button>
      <button
        class="document-archive-button document-archive-button-download"
        type="button"
        :disabled="!isReady"
        :title="isReady ? '下载PDF' : 'PDF未生成，暂不能下载'"
        @click="emit('download')"
      >
        <span class="document-archive-icon" aria-hidden="true">↓</span>
        下载PDF
      </button>
      <button
        class="document-archive-button document-archive-button-regenerate"
        type="button"
        title="重新生成PDF归档"
        @click="emit('regenerate')"
      >
        <span class="document-archive-icon" aria-hidden="true">↻</span>
        重新生成
      </button>
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue";

const props = defineProps({
  status: {
    type: String,
    default: "未生成"
  }
});

const emit = defineEmits(["preview", "download", "regenerate"]);

const normalizedStatusType = computed(() => {
  const value = String(props.status ?? "").trim();
  if (!value || value === "未生成" || value === "missing") return "missing";
  if (value === "已归档" || value === "ready" || value === "archived") return "ready";
  if (value === "归档失败" || value === "failed" || value === "error") return "failed";
  return "missing";
});

const statusText = computed(() => {
  if (normalizedStatusType.value === "ready") return "已归档";
  if (normalizedStatusType.value === "failed") return "归档失败";
  return "未生成";
});

const isReady = computed(() => normalizedStatusType.value === "ready");

const statusClass = computed(() => ({
  "document-archive-actions-archived": normalizedStatusType.value === "ready",
  "document-archive-actions-failed": normalizedStatusType.value === "failed",
  "document-archive-actions-pending": normalizedStatusType.value === "missing"
}));
</script>
