<template>
  <div class="crawl-info">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>爬虫统计信息</span>
          <el-button type="primary" size="small" @click="fetchCrawlStats" :loading="loading">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </template>

      <el-table :data="crawlStats" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="source" label="爬虫来源" width="200" />
        <el-table-column prop="count" label="本次获取数量" width="120" sortable />
        <el-table-column label="最近更新时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.latestUpdate) }}
          </template>
        </el-table-column>
        <el-table-column label="最新工程时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.latestPublishDate) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.error ? 'danger' : (row.count > 0 ? 'success' : 'info')">
              {{ row.error ? '出错' : (row.count > 0 ? '正常' : '无数据') }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="错误信息" min-width="200">
          <template #default="{ row }">
            <span v-if="row.error" style="color: #f56c6c">{{ row.error }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="small"
              @click="crawlSingleSource(row.source)"
              :loading="crawlingSources.has(row.source)"
              :disabled="crawlingSources.has(row.source)"
            >
              <el-icon><Refresh /></el-icon>
              更新
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="summary" v-if="crawlStats.length > 0">
        <el-descriptions :column="3" border>
          <el-descriptions-item label="爬虫来源总数">
            {{ crawlStats.length }}
          </el-descriptions-item>
          <el-descriptions-item label="本次获取总数">
            {{ totalCount }}
          </el-descriptions-item>
          <el-descriptions-item label="有数据来源">
            {{ activeSources }}
          </el-descriptions-item>
          <el-descriptions-item label="出错来源">
            {{ errorSources }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'

interface CrawlStat {
  source: string
  count: number
  latestUpdate: string | null
  latestPublishDate: string | null
  error: string | null
}

const crawlStats = ref<CrawlStat[]>([])
const loading = ref(false)
const crawlingSources = ref<Set<string>>(new Set())

const totalCount = computed(() => {
  return crawlStats.value.reduce((sum, item) => sum + item.count, 0)
})

const activeSources = computed(() => {
  return crawlStats.value.filter(item => item.count > 0).length
})

const errorSources = computed(() => {
  return crawlStats.value.filter(item => item.error).length
})

const formatDate = (dateStr: string | null) => {
  if (!dateStr) return '-'
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const fetchCrawlStats = async () => {
  loading.value = true
  try {
    const res = await axios.get('/api/bids/crawl-info-stats')
    crawlStats.value = res.data
  } catch (error) {
    console.error('Failed to fetch crawl stats:', error)
    ElMessage.error('获取爬虫统计信息失败')
  } finally {
    loading.value = false
  }
}

const crawlSingleSource = async (sourceName: string) => {
  crawlingSources.value.add(sourceName)
  try {
    ElMessage.info(`正在更新 ${sourceName}...`)
    const res = await axios.post(`/api/crawler/crawl/${encodeURIComponent(sourceName)}`)
    
    if (res.data.success) {
      ElMessage.success(`${sourceName} 更新成功，获取 ${res.data.count} 条数据`)
    } else {
      ElMessage.error(`${sourceName} 更新失败: ${res.data.error || '未知错误'}`)
    }
    
    // 刷新统计数据
    await fetchCrawlStats()
  } catch (error) {
    console.error('Failed to crawl single source:', error)
    ElMessage.error(`${sourceName} 更新失败`)
  } finally {
    crawlingSources.value.delete(sourceName)
  }
}

onMounted(() => {
  fetchCrawlStats()
})
</script>

<style scoped>
.crawl-info {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.summary {
  margin-top: 20px;
}
</style>
