<script setup lang="ts">
import PageCard from '@/components/PageCard.vue'
import { DATASET_TYPE_MAP, STORAGE_MAP, TRAIN_METHOD_MAP, TRAIN_TYPE_MAP } from '@/constants'
import type { DatasetItem, FineTuneTask } from '@/types'

defineProps<{
  task: FineTuneTask | null
  dataset: DatasetItem | null
  baseModelName: string
}>()

function formatDateTime(value?: string) {
  if (!value) return '-'
  const date = new Date(value)
  if (Number.isNaN(date.getTime())) return value
  return date.toLocaleString('zh-CN', { hour12: false })
}
</script>

<template>
  <PageCard class="task-overview" title="任务信息" subtitle="模型、数据集与运行配置">
    <div class="overview-layout" aria-label="训练任务信息">
      <el-descriptions class="task-descriptions task-profile" :column="2" border>
        <el-descriptions-item label="任务 ID">{{ task?.id ?? '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDateTime(task?.create_time) }}</el-descriptions-item>
        <el-descriptions-item label="基座模型">{{ baseModelName }}</el-descriptions-item>
        <el-descriptions-item label="输出模型">{{ task?.output_model_name || '暂未生成' }}</el-descriptions-item>
        <el-descriptions-item label="训练方式">
          {{ task?.train_type ? (TRAIN_TYPE_MAP[task.train_type] || task.train_type) : '未配置' }}
        </el-descriptions-item>
        <el-descriptions-item label="训练方法">
          {{ task?.train_method ? (TRAIN_METHOD_MAP[task.train_method] || task.train_method) : '未配置' }}
        </el-descriptions-item>
        <el-descriptions-item label="训练数据集" class-name="dataset-profile">
          {{ dataset?.name || (task?.train_dataset_id ? '正在加载' : '未配置') }}
        </el-descriptions-item>
        <el-descriptions-item label="数据类型">
          {{ dataset ? (DATASET_TYPE_MAP[dataset.type] || dataset.type || '未配置') : '未配置' }}
        </el-descriptions-item>
        <el-descriptions-item label="数据条数">
          {{ dataset?.count?.toLocaleString('zh-CN') ?? '未配置' }}{{ dataset?.count != null ? ' 条' : '' }}
        </el-descriptions-item>
        <el-descriptions-item label="数据大小">{{ dataset?.size || '未配置' }}</el-descriptions-item>
        <el-descriptions-item label="训练开始时间" class-name="runtime-panel">
          {{ formatDateTime(task?.create_time) }}
        </el-descriptions-item>
        <el-descriptions-item label="训练时长">{{ task?.train_duration || '未配置' }}</el-descriptions-item>
        <el-descriptions-item label="存储位置">
          {{ STORAGE_MAP[dataset?.storage_type || ''] || dataset?.storage_type || '未配置' }}
        </el-descriptions-item>
        <el-descriptions-item label="使用 GPU">
          {{ task?.gpus?.length ? task.gpus.join('、') : '未配置' }}
        </el-descriptions-item>
      </el-descriptions>
    </div>
  </PageCard>
</template>

<style scoped lang="scss">
.task-overview {
  margin-bottom: 0;
  border: 1px solid #e4e7ed !important;
  border-radius: 8px !important;
  box-shadow: none !important;
}
.overview-layout { width: 100%; }
.task-descriptions :deep(.el-descriptions__label) {
  width: 132px;
  color: #606266;
  font-weight: 500;
  background: #f7f8fa !important;
}
.task-descriptions :deep(.el-descriptions__content) {
  color: #303133;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.task-descriptions :deep(.el-descriptions__cell) { padding: 12px 16px !important; }

@media (max-width: 700px) {
  .task-descriptions :deep(.el-descriptions__body),
  .task-descriptions :deep(.el-descriptions__table),
  .task-descriptions :deep(.el-descriptions__tbody),
  .task-descriptions :deep(.el-descriptions__row),
  .task-descriptions :deep(.el-descriptions__cell) { display: block; width: 100%; box-sizing: border-box; }
  .task-descriptions :deep(.el-descriptions__label) { width: 100%; border-bottom: 0 !important; }
}
</style>
