<template>
  <div class="page-container">
    <el-row :gutter="16">
      <el-col :xs="12" :sm="8" :md="6">
        <StatCard title="当前在线" :value="data?.onlineDevices ?? 0" icon="Monitor" color="#3b82f6" />
      </el-col>
      <el-col :xs="12" :sm="8" :md="6">
        <StatCard title="峰值会话" :value="data?.metrics.peakSessions ?? 0" icon="TrendCharts" color="#67c23a" />
      </el-col>
      <el-col :xs="12" :sm="8" :md="6">
        <StatCard title="累计注册" :value="data?.metrics.totalRegistered ?? 0" icon="User" color="#e6a23c" />
      </el-col>
      <el-col :xs="12" :sm="8" :md="6">
        <StatCard title="消息总量" :value="data?.metrics.totalMessages ?? 0" icon="ChatDotRound" color="#f56c6c" />
      </el-col>
    </el-row>

    <el-card shadow="hover" header="运行信息" class="info-card">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="运行状态">
          <el-tag type="success" effect="dark">运行中</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="服务端口">{{ config?.serverPort ?? '—' }}</el-descriptions-item>
        <el-descriptions-item label="启动时间">
          {{ data ? fmtTime(data.serverStartTime) : '—' }}
        </el-descriptions-item>
        <el-descriptions-item label="已运行时长">{{ uptime }}</el-descriptions-item>
        <el-descriptions-item label="在线 / 总数">
          {{ data?.onlineDevices ?? 0 }} / {{ data?.totalDevices ?? 0 }}
        </el-descriptions-item>
        <el-descriptions-item label="WebSocket 端点">
          {{ config?.websocketEndpoint ?? '—' }}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-card shadow="hover" header="信令流量明细" class="info-card">
      <el-descriptions :column="3" border>
        <el-descriptions-item label="注册总数">{{ data?.metrics.totalRegistered ?? 0 }}</el-descriptions-item>
        <el-descriptions-item label="OFFER 总数">{{ data?.metrics.totalOffers ?? 0 }}</el-descriptions-item>
        <el-descriptions-item label="ANSWER 总数">{{ data?.metrics.totalAnswers ?? 0 }}</el-descriptions-item>
        <el-descriptions-item label="拒绝总数">{{ data?.metrics.totalRejected ?? 0 }}</el-descriptions-item>
        <el-descriptions-item label="消息总量">{{ data?.metrics.totalMessages ?? 0 }}</el-descriptions-item>
        <el-descriptions-item label="峰值会话">{{ data?.metrics.peakSessions ?? 0 }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
import { getDashboard, getConfig } from '@/api/admin';
import type { DashboardData, ServerConfig } from '@/api/admin';
import StatCard from '@/components/StatCard.vue';

const data = ref<DashboardData | null>(null);
const config = ref<ServerConfig | null>(null);
const now = ref(Date.now());
let timer: number | undefined;

const uptime = computed(() => {
  if (!data.value) return '—';
  return fmtDuration(now.value - data.value.serverStartTime);
});

function fmtDuration(ms: number) {
  const s = Math.floor(ms / 1000);
  const h = Math.floor(s / 3600);
  const m = Math.floor((s % 3600) / 60);
  const sec = s % 60;
  return `${h} 小时 ${m} 分 ${sec} 秒`;
}

function fmtTime(ts: number) {
  return new Date(ts).toLocaleString('zh-CN');
}

onMounted(async () => {
  const [d, c] = await Promise.all([getDashboard(), getConfig()]);
  data.value = d;
  config.value = c;
  timer = window.setInterval(() => {
    now.value = Date.now();
  }, 1000);
});
onBeforeUnmount(() => {
  if (timer) clearInterval(timer);
});
</script>

<style scoped>
.info-card {
  margin-top: 16px;
}
</style>
