<template>
  <div class="page-container">
    <el-card shadow="hover">
      <div class="toolbar">
        <div>
          <h2 class="page-title">设备管理</h2>
          <span class="text-muted">共 {{ devices.length }} 台已注册设备</span>
        </div>
        <div class="toolbar-actions">
          <el-select v-model="filterType" placeholder="全部类型" clearable style="width: 150px">
            <el-option label="主控端" value="CONTROLLER" />
            <el-option label="被控端" value="CONTROLLED" />
          </el-select>
          <el-switch v-model="autoRefresh" active-text="自动刷新" />
          <el-button :icon="'Refresh'" :loading="loading" @click="load">刷新</el-button>
        </div>
      </div>

      <el-table :data="devices" stripe>
        <el-table-column prop="deviceId" label="设备 ID" min-width="200" />
        <el-table-column label="类型" width="120">
          <template #default="{ row }">
            <el-tag v-if="row.deviceType === 'CONTROLLER'" type="success">主控端</el-tag>
            <el-tag v-else-if="row.deviceType === 'CONTROLLED'" type="warning">被控端</el-tag>
            <el-tag v-else type="info">未知</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <span class="status-dot" :class="row.online ? 'online' : 'offline'"></span>
            <el-tag :type="row.online ? 'success' : 'danger'" effect="dark" size="small">
              {{ row.online ? '在线' : '离线' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="上线时间" min-width="180">
          <template #default="{ row }">{{ fmtTime(row.connectedAt) }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" :icon="'CopyDocument'" @click="copyId(row.deviceId)">
              复制 ID
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import { getDevices } from '@/api/admin';
import type { DeviceInfo } from '@/api/admin';
import { ElMessage } from 'element-plus';

const devices = ref<DeviceInfo[]>([]);
const loading = ref(false);
const filterType = ref<string>('');
const autoRefresh = ref(true);
let timer: number | undefined;

async function load() {
  loading.value = true;
  try {
    devices.value = await getDevices(filterType.value || undefined);
  } finally {
    loading.value = false;
  }
}

function startTimer() {
  if (timer) clearInterval(timer);
  if (autoRefresh.value) timer = window.setInterval(load, 5000);
}

watch(autoRefresh, startTimer);
watch(filterType, load);

function copyId(id: string) {
  navigator.clipboard
    ?.writeText(id)
    .then(() => ElMessage.success('已复制设备 ID'))
    .catch(() => ElMessage.error('复制失败'));
}

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

onMounted(() => {
  load();
  startTimer();
});
onBeforeUnmount(() => {
  if (timer) clearInterval(timer);
});
</script>

<style scoped>
.page-title {
  margin: 0 0 4px;
  font-size: 20px;
}
.toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}
.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}
.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}
.status-dot.online {
  background: #67c23a;
}
.status-dot.offline {
  background: #f56c6c;
}
</style>
