<template>
  <div class="dashboard">
    <h2 class="page-title">总览</h2>
    <el-row :gutter="16" class="stats-row">
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="stat-item">
            <span class="stat-label">在线房间数</span>
            <span class="stat-value">{{ stats.onlineRooms }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="stat-item">
            <span class="stat-label">今日总局数</span>
            <span class="stat-value">{{ stats.todaySessions }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="stat-item">
            <span class="stat-label">总玩家数</span>
            <span class="stat-value">{{ stats.totalPlayers }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <div class="stat-item">
            <span class="stat-label">平均得分</span>
            <span class="stat-value">{{ stats.avgScore }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-card class="playing-card">
      <template #header>
        <div class="card-header">
          <span>进行中的游戏</span>
          <div class="card-header-actions">
            <el-tag v-if="autoRefresh" type="info" size="small">每 5 秒自动刷新</el-tag>
            <el-button type="primary" link @click="loadPlaying">刷新</el-button>
          </div>
        </div>
      </template>
      <el-table :data="playingList" stripe>
        <el-table-column prop="roomId" label="房间号" width="120" />
        <el-table-column prop="currentScore" label="当前分数" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <StatusTag :status="row.status" />
          </template>
        </el-table-column>
      </el-table>
      <el-empty v-if="playingList.length === 0 && !loading" description="暂无进行中的游戏" />
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { getStatsOverview } from '../api/stats'
import { getRooms } from '../api/rooms'
import StatusTag from '../components/common/StatusTag.vue'

const stats = ref({
  onlineRooms: 0,
  todaySessions: 0,
  totalPlayers: 0,
  avgScore: '-',
})
const playingList = ref([])
const loading = ref(false)
const autoRefresh = ref(true)
let refreshTimer = null

async function loadStats() {
  try {
    const res = await getStatsOverview()
    const d = res.data ?? {}
    stats.value = {
      onlineRooms: d.activeRooms ?? d.onlineRooms ?? 0,
      todaySessions: d.todaySessions ?? 0,
      totalPlayers: d.totalRooms ?? d.totalPlayers ?? 0,
      avgScore: typeof d.avgScore === 'number' ? d.avgScore : (d.avgScore ?? '-'),
    }
  } catch {
    // 接口未实现时保持默认
  }
}

async function loadPlaying() {
  loading.value = true
  try {
    const res = await getRooms()
    const rooms = res.data ?? []
    playingList.value = rooms.filter((r) => r.status === 'playing')
  } catch {
    playingList.value = []
  } finally {
    loading.value = false
  }
}

function startAutoRefresh() {
  if (refreshTimer) return
  refreshTimer = setInterval(() => {
    loadStats()
    loadPlaying()
  }, 5000)
}

function stopAutoRefresh() {
  if (refreshTimer) {
    clearInterval(refreshTimer)
    refreshTimer = null
  }
}

onMounted(() => {
  loadStats()
  loadPlaying()
  startAutoRefresh()
})

onUnmounted(() => {
  stopAutoRefresh()
})
</script>

<style scoped>
.page-title {
  margin-bottom: 20px;
  font-size: 20px;
}
.stats-row {
  margin-bottom: 20px;
}
.stat-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.stat-label {
  color: #666;
  font-size: 14px;
}
.stat-value {
  font-size: 24px;
  font-weight: 600;
}
.playing-card {
  margin-top: 20px;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
</style>
