<template>
  <ScreenLayout :show-back-btn="true" back-text="返回待机" back-target="/idle">
    <div class="page-system-info">
      <h1 class="page-title">系统信息</h1>

      <div class="info-card">
        <div class="info-section">
          <h2 class="section-title">设备信息</h2>
          <div class="info-list">
            <div class="info-row">
              <span class="info-label">设备编号</span>
              <span class="info-value">{{ systemInfo.deviceId }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">机器人名称</span>
              <span class="info-value">{{ systemInfo.robotName }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">本地IP</span>
              <span class="info-value">{{ systemInfo.ipAddress }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">MAC地址</span>
              <span class="info-value">{{ systemInfo.macAddress }}</span>
            </div>
          </div>
        </div>

        <div class="info-section">
          <h2 class="section-title">版本信息</h2>
          <div class="info-list">
            <div class="info-row">
              <span class="info-label">前端版本</span>
              <span class="info-value">{{ systemInfo.frontendVersion }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">后端版本</span>
              <span class="info-value">{{ systemInfo.backendVersion }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">屏幕分辨率</span>
              <span class="info-value">{{ systemInfo.screenResolution }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">最后更新</span>
              <span class="info-value">{{ systemInfo.lastUpdate }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">运行时长</span>
              <span class="info-value">{{ systemInfo.uptime }}</span>
            </div>
          </div>
        </div>

        <div class="info-section">
          <h2 class="section-title">服务状态</h2>
          <div class="services-grid">
            <div
              v-for="service in systemInfo.services"
              :key="service.name"
              class="service-item"
              :class="{ online: service.status === 'online', offline: service.status !== 'online' }"
            >
              <div class="service-status"></div>
              <span class="service-name">{{ service.name }}</span>
              <span class="service-latency">{{ service.latency }}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="actions">
        <button class="btn-refresh" @click="handleRefresh">
          刷新页面
        </button>
      </div>
    </div>
  </ScreenLayout>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import * as api from '@/api/screen'
import ScreenLayout from '@/layouts/ScreenLayout.vue'

const systemInfo = ref({
  deviceId: '加载中...',
  robotName: '-',
  ipAddress: '-',
  macAddress: '-',
  frontendVersion: '-',
  backendVersion: '-',
  screenResolution: '-',
  lastUpdate: '-',
  uptime: '-',
  services: []
})

const fetchSystemInfo = async () => {
  try {
    systemInfo.value = await api.getSystemInfo()
  } catch (error) {
    console.error('Failed to fetch system info:', error)
  }
}

const handleRefresh = () => {
  window.location.reload()
}

onMounted(() => {
  fetchSystemInfo()

  // 更新分辨率
  systemInfo.value.screenResolution = `${window.screen.width}×${window.screen.height}`
})
</script>

<style scoped>
.page-system-info {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
  overflow-y: auto;
}

.page-title {
  font-size: 32px;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
  margin: 0 0 24px;
}

.info-card {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 0 16px;
}

.info-section {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-light);
}

.info-list {
  display: flex;
  flex-direction: column;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.info-row:not(:last-child) {
  border-bottom: 1px solid var(--border-light);
}

.info-label {
  font-size: 15px;
  color: var(--text-muted);
}

.info-value {
  font-size: 15px;
  color: var(--text-primary);
  font-weight: 500;
}

.services-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.service-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  background: var(--bg-page);
  border-radius: var(--radius-md);
}

.service-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.service-item.online .service-status {
  background: var(--success);
}

.service-item.offline .service-status {
  background: var(--danger);
}

.service-name {
  flex: 1;
  font-size: 14px;
  color: var(--text-primary);
}

.service-latency {
  font-size: 12px;
  color: var(--text-muted);
}

.actions {
  margin-top: 24px;
}

.btn-refresh {
  padding: 14px 40px;
  font-size: 16px;
  color: var(--text-secondary);
  background: var(--bg-card);
  border: 2px solid var(--border-light);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-refresh:hover {
  border-color: var(--primary);
  color: var(--primary);
}
</style>
