<template>
  <div class="dashboard" v-loading="loading" element-loading-text="加载中...">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-value">{{ stats.users }}</div>
          <div class="stat-label">用户数</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-value">{{ stats.workspaces }}</div>
          <div class="stat-label">工作空间</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-value">{{ stats.conversations }}</div>
          <div class="stat-label">对话数</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-value">{{ stats.messages }}</div>
          <div class="stat-label">消息数</div>
        </el-card>
      </el-col>
    </el-row>

    <el-card style="margin-top: 20px">
      <template #header>
        <span>快捷入口</span>
      </template>
      <el-space wrap>
        <el-button type="primary" @click="$router.push('/users')">用户管理</el-button>
        <el-button @click="$router.push('/workspaces')">工作空间</el-button>
        <el-button @click="$router.push('/conversations')">对话管理</el-button>
      </el-space>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { getStats } from '../api/admin'

const stats = reactive({
  users: 0,
  workspaces: 0,
  conversations: 0,
  messages: 0,
  files: 0
})

const loading = ref(true)

const fetchStats = async () => {
  loading.value = true
  try {
    const res = await getStats()
    Object.assign(stats, res)
  } catch (e) {
    console.error('获取统计失败:', e)
    // 即使失败也显示 0，不阻塞页面
  } finally {
    loading.value = false
  }
}

onMounted(fetchStats)
</script>

<style scoped>
.stat-card {
  text-align: center;
  padding: 20px 0;
}
.stat-value {
  font-size: 32px;
  font-weight: bold;
  color: #409eff;
}
.stat-label {
  margin-top: 8px;
  color: #666;
  font-size: 14px;
}
</style>
