<template>
  <div class="uti-card">
    <!-- 文件名行 -->
    <div class="uti-row">
      <span class="uti-name" :title="task.fileName">{{ task.fileName }}</span>
      <span class="uti-size">{{ formatFileSize(task.fileSize) }}</span>
    </div>

    <!-- 向量化状态 -->
    <div class="uti-statuses">
      <span :class="['uti-tag', vectorStatus.color]">
        <LoadingOutlined v-if="vectorStatus.animating" class="spin" />
        <CheckCircleOutlined v-else-if="vectorStatus.status === 'success'" />
        <CloseCircleOutlined v-else-if="vectorStatus.status === 'error'" />
        <ClockCircleOutlined v-else />
        {{ vectorStatus.label }}
      </span>

      <button
        v-if="vectorStatus.status === 'error' && vectorStatus.message"
        class="uti-detail"
        @click="toggleError"
      >详情</button>

      <button
        v-if="vectorStatus.status === 'error'"
        class="uti-retry"
        @click="handleRetry"
        :disabled="isRetrying"
      >
        <LoadingOutlined v-if="isRetrying" spin />
        <ReloadOutlined v-else />
        重试
      </button>
    </div>

    <!-- 错误消息 -->
    <div v-if="showError && vectorStatus.message" class="uti-error">
      {{ vectorStatus.message }}
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {
  FileTextOutlined, LoadingOutlined, CheckCircleOutlined,
  CloseCircleOutlined, ClockCircleOutlined, ReloadOutlined
} from '@ant-design/icons-vue'

defineProps({
  task: { type: Object, required: true },
  vectorStatus: { type: Object, required: true }
})

const emit = defineEmits(['retry'])

const isRetrying = ref(false)

const showError = ref(false)

function toggleError() { showError.value = !showError.value }

async function handleRetry() {
  if (isRetrying.value) return

  isRetrying.value = true

  try {
    emit('retry', {
      taskId: task.id,
      fileName: task.fileName
    })

    await new Promise(resolve => setTimeout(resolve, 1000))
  } finally {
    setTimeout(() => {
      isRetrying.value = false
    }, 1000)
  }
}

function formatFileSize(bytes) {
  if (!bytes || bytes === 0) return '0B'
  const units = ['B', 'KB', 'MB', 'GB']
  const i = bytes > 1024 ? Math.floor(Math.log(bytes) / Math.log(1024)) : 0
  return parseFloat((bytes / Math.pow(1024, i)).toFixed(1)) + units[i]
}
</script>

<style scoped>
@import '../../design-tokens.css';

.uti-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  transition: border-color var(--transition-fast);
}

.uti-card:hover {
  border-color: var(--border-medium);
}

.uti-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.uti-name {
  font-size: var(--font-md);
  font-weight: var(--weight-semibold);
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.uti-size {
  flex-shrink: 0;
  font-size: var(--font-xs);
  color: var(--text-disabled);
}

.uti-statuses {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.uti-tag {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 1px 8px;
  border-radius: var(--radius-sm);
  font-size: var(--font-xs);
  font-weight: var(--weight-medium);
  line-height: 1.7;
  white-space: nowrap;
}

.uti-tag.pending { background: var(--bg-container); color: var(--text-disabled); }
.uti-tag.info { background: #E8F4FD; color: #1971C2; }
.uti-tag.success { background: #EBFBEE; color: #2B8A3E; }
.uti-tag.error { background: #FFF5F5; color: #C92A2A; }

.uti-detail {
  margin-left: auto;
  background: none;
  border: 1px solid var(--border-light);
  color: var(--text-disabled);
  cursor: pointer;
  font-size: var(--font-xs);
  padding: 0 6px;
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
  line-height: 1.7;
}

.uti-detail:hover {
  border-color: var(--text-secondary);
  color: var(--text-secondary);
}

.uti-retry {
  margin-left: auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: white;
  cursor: pointer;
  font-size: var(--font-xs, 12px);
  padding: 2px 10px;
  border-radius: var(--radius-sm, 4px);
  transition: all 0.2s ease;
  line-height: 1.7;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-weight: 500;
}

.uti-retry:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.35);
}

.uti-retry:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.uti-error {
  margin-top: 4px;
  padding: 6px 8px;
  background: var(--bg-error);
  border-radius: var(--radius-sm);
  font-size: var(--font-xs);
  color: var(--color-error);
  line-height: 1.5;
  word-break: break-all;
}

.spin { animation: uti-spin 1s linear infinite; }
@keyframes uti-spin { to { transform: rotate(360deg); } }
</style>
