<template>
  <div>
    <h1 class="page-title">每日简报</h1>

    <el-card class="dark-card">
      <el-table :data="briefs" v-loading="loading">
        <el-table-column prop="brief_date" label="日期" width="150" />
        <el-table-column prop="total_articles" label="原始文章数" width="130" />
        <el-table-column prop="unique_articles" label="去重后文章数" width="140" />
        <el-table-column label="分类数">
          <template #default="{ row }">
            {{ Object.keys(row.by_category || {}).length }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="$router.push(`/briefs/${row.brief_date}`)">查看</el-button>
            <el-button size="small" type="primary" @click="regenerate(row.brief_date)">重新生成</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { datacleanApi } from '@/api'

const loading = ref(false)
const briefs = ref([])

const loadBriefs = async () => {
  loading.value = true
  try {
    briefs.value = await datacleanApi.getBriefs({ limit: 50 })
  } catch (err) {
    ElMessage.error(err.message)
  } finally {
    loading.value = false
  }
}

const regenerate = async (date) => {
  try {
    await datacleanApi.regenerateBrief(date)
    ElMessage.success('简报重新生成成功')
    loadBriefs()
  } catch (err) {
    ElMessage.error(err.message)
  }
}

onMounted(loadBriefs)
</script>
