<template>
  <div class="document-card">
    <div class="document-card__icon" :class="`document-card__icon--${format}`">
      <FileTextOutlined v-if="format === 'word'" />
      <TableOutlined v-else-if="format === 'excel'" />
      <FilePdfOutlined v-else-if="format === 'pdf'" />
      <FileOutlined v-else />
    </div>
    <div class="document-card__info">
      <div class="document-card__name">{{ filename }}</div>
      <div class="document-card__meta">{{ formattedSize }} · {{ formatLabel }}</div>
    </div>
    <a
      class="document-card__download"
      :href="downloadUrl"
      target="_blank"
      rel="noopener noreferrer"
      download
      @click.stop
    >
      <DownloadOutlined />
    </a>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import {
  FileOutlined,
  FileTextOutlined,
  FilePdfOutlined,
  TableOutlined,
  DownloadOutlined,
} from '@ant-design/icons-vue'
import { getDynamicBaseURL } from '@/api/base'
import type { IDocumentMeta } from '@/api/documents'

interface Props {
  document: IDocumentMeta
}

const props = defineProps<Props>()

const filename = computed(() => props.document.filename)
const format = computed(() => props.document.format)

const formatLabel = computed(() => {
  const map: Record<string, string> = {
    word: 'Word',
    excel: 'Excel',
    pdf: 'PDF',
  }
  return map[format.value] || format.value
})

const formattedSize = computed(() => {
  const size = props.document.size
  if (!size) return '未知大小'
  const units = ['B', 'KB', 'MB', 'GB']
  let value = size
  let unitIdx = 0
  while (value >= 1024 && unitIdx < units.length - 1) {
    value /= 1024
    unitIdx++
  }
  return `${value.toFixed(unitIdx === 0 ? 0 : 1)} ${units[unitIdx]}`
})

const downloadUrl = computed(() => {
  const base = getDynamicBaseURL()
  const url = props.document.download_url || `/api/v1/documents/download/${props.document.id}`
  if (!base || url.startsWith('http')) return url
  return `${base}${url}`
})
</script>

<style scoped>
.document-card {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  max-width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
}

.document-card:hover {
  background: var(--bg-tertiary);
  border-color: var(--border-hover);
}

.document-card__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  font-size: var(--font-lg);
  flex-shrink: 0;
}

.document-card__icon--word {
  background: rgba(37, 99, 235, 0.1);
  color: #2563eb;
}

.document-card__icon--excel {
  background: rgba(22, 163, 74, 0.1);
  color: #16a34a;
}

.document-card__icon--pdf {
  background: rgba(220, 38, 38, 0.1);
  color: #dc2626;
}

.document-card__info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.document-card__name {
  font-size: var(--font-sm);
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.document-card__meta {
  font-size: var(--font-xs);
  color: var(--text-tertiary);
}

.document-card__download {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
  text-decoration: none;
  flex-shrink: 0;
  cursor: pointer;
}

.document-card__download:hover {
  color: var(--text-primary);
  background: var(--bg-tertiary);
}
</style>
