<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { Page } from '@vben/common-ui';
import { Card, Descriptions, Spin } from 'ant-design-vue';
import { getSystemInfoApi, type IndexApi } from '#/api';

defineOptions({ name: 'Dashboard' });

const loading = ref(false);
const systemInfo = ref<IndexApi.SystemInfo>({});

const fetchSystemInfo = async () => {
  loading.value = true;
  try {
    const res = await getSystemInfoApi();
    // 支持 code 为 0 或 200 的成功响应
    if (res && (res.code === 0 || res.code === 200)) {
      // 根据实际返回格式，数据在 res.data 中
      systemInfo.value = res.data || {};
    }
  } catch (error) {
    // 静默处理错误
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchSystemInfo();
});
</script>

<template>
  <Page title="系统概览">
    <div style="display: flex; flex-direction: column; gap: 16px;">
      <Card title="系统信息">
        <Spin :spinning="loading">
          <Descriptions :column="2" bordered>
            <Descriptions.Item label="操作系统">
              {{ systemInfo.os || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="PHP版本">
              {{ systemInfo.php || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="Web服务器">
              {{ systemInfo.server || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="MySQL版本">
              {{ systemInfo.mysql || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="框架版本">
              {{ systemInfo.framework_version || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="上传限制">
              {{ systemInfo.upload_max || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="最大执行时间">
              {{ systemInfo.max_execution_time || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="运行目录">
              {{ systemInfo.runtime_path || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="磁盘总空间" :span="2">
              {{ systemInfo.disk_total_space || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="磁盘可用空间">
              {{ systemInfo.disk_free_space || '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="磁盘使用率">
              {{ systemInfo.disk_usage || '-' }}
            </Descriptions.Item>
          </Descriptions>
        </Spin>
      </Card>

      <Card title="数据统计">
        <Spin :spinning="loading">
          <Descriptions :column="2" bordered>
            <Descriptions.Item label="学校数量">
              {{ systemInfo.statistics?.school_count ?? '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="教室数量">
              {{ systemInfo.statistics?.classroom_count ?? '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="班级数量">
              {{ systemInfo.statistics?.class_count ?? '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="学生数量">
              {{ systemInfo.statistics?.student_count ?? '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="教师数量">
              {{ systemInfo.statistics?.teacher_count ?? '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="预订数量">
              {{ systemInfo.statistics?.booking_count ?? '-' }}
            </Descriptions.Item>
            <Descriptions.Item label="已选座位数量" :span="2">
              {{ systemInfo.statistics?.selected_seat_count ?? '-' }}
            </Descriptions.Item>
          </Descriptions>
        </Spin>
      </Card>
    </div>
  </Page>
</template>

