<template>
  <div class="container">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="page-title">
        <h2>自动感知拓扑图</h2>
        <p class="page-subtitle">自动发现网络设备，构建实时网络拓扑</p>
      </div>
      <div class="page-actions">
        <a-button type="primary" :loading="isScanning" @click="handleScan">
          <template #icon>
            <icon-refresh v-if="isScanning" class="animate-spin" />
            <icon-play-circle v-else />
          </template>
          {{ isScanning ? '扫描中...' : '启动扫描' }}
        </a-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <a-row :gutter="16" class="stats-row">
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-primary">
              <icon-apps />
            </div>
            <div class="stats-info">
              <div class="stats-title">发现设备</div>
              <div class="stats-value">156</div>
              <div class="stats-desc text-success">
                <icon-arrow-rise />
                较上次 +2
              </div>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-success">
              <icon-wifi />
            </div>
            <div class="stats-info">
              <div class="stats-title">在线设备</div>
              <div class="stats-value">148</div>
              <div class="stats-desc">在线率 94.8%</div>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-warning">
              <icon-exclamation-circle />
            </div>
            <div class="stats-info">
              <div class="stats-title">新增设备</div>
              <div class="stats-value">2</div>
              <div class="stats-desc">待确认</div>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-default">
              <icon-clock-circle />
            </div>
            <div class="stats-info">
              <div class="stats-title">上次扫描</div>
              <div class="stats-value">14:30</div>
              <div class="stats-desc">12分钟前</div>
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>

    <!-- 扫描进度和配置 -->
    <a-row :gutter="16" class="mt-6 scan-row">
      <a-col :xs="24" :lg="16" class="scan-col">
        <a-card :bordered="false" class="scan-card">
          <template #title>
            <div class="card-title">
              <span>扫描进度</span>
              <a-tag v-if="isScanning" color="orange" bordered>扫描中</a-tag>
            </div>
          </template>
          <div v-if="isScanning" class="scanning-content">
            <div class="flex items-center gap-4 mb-4">
              <icon-refresh class="h-5 w-5 animate-spin text-primary" />
              <div class="flex-1">
                <p class="text-sm font-medium">正在扫描网段 10.0.0.0/16</p>
                <p class="text-xs text-muted">已发现 89 个设备</p>
              </div>
            </div>
            <a-progress :percent="45" :stroke-width="8" />
            <a-row :gutter="16" class="mt-4 text-center">
              <a-col :span="6">
                <div class="text-lg font-semibold">45%</div>
                <div class="text-xs text-muted">扫描进度</div>
              </a-col>
              <a-col :span="6">
                <div class="text-lg font-semibold">89</div>
                <div class="text-xs text-muted">已发现</div>
              </a-col>
              <a-col :span="6">
                <div class="text-lg font-semibold">2</div>
                <div class="text-xs text-muted">新设备</div>
              </a-col>
              <a-col :span="6">
                <div class="text-lg font-semibold">5:23</div>
                <div class="text-xs text-muted">预计剩余</div>
              </a-col>
            </a-row>
          </div>
          <div v-else class="scan-complete-content">
            <div class="flex items-center gap-4 mb-4">
              <icon-check-circle class="h-5 w-5 text-success" />
              <div class="flex-1">
                <p class="text-sm font-medium">上次全网扫描已完成</p>
                <p class="text-xs text-muted">2024-01-15 14:30:00 - 耗时 12分钟</p>
              </div>
            </div>
            <a-row :gutter="16" class="stats-grid">
              <a-col :span="6" class="text-center">
                <div class="text-lg font-semibold">156</div>
                <div class="text-xs text-muted">总设备</div>
              </a-col>
              <a-col :span="6" class="text-center">
                <div class="text-lg font-semibold text-success">148</div>
                <div class="text-xs text-muted">在线</div>
              </a-col>
              <a-col :span="6" class="text-center">
                <div class="text-lg font-semibold text-muted">6</div>
                <div class="text-xs text-muted">离线</div>
              </a-col>
              <a-col :span="6" class="text-center">
                <div class="text-lg font-semibold text-warning">2</div>
                <div class="text-xs text-muted">新发现</div>
              </a-col>
            </a-row>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :lg="8" class="scan-col">
        <a-card :bordered="false" title="扫描配置" class="scan-card">
          <div class="config-section">
            <div class="config-item">
              <p class="config-label">扫描网段</p>
              <div class="config-value-list">
                <a-tag color="arcoblue" bordered>10.0.0.0/16</a-tag>
                <a-tag color="arcoblue" bordered>192.168.0.0/16</a-tag>
              </div>
            </div>
            <div class="config-item">
              <p class="config-label">发现协议</p>
              <div class="config-tags">
                <a-tag color="arcoblue" bordered>SNMP</a-tag>
                <a-tag color="arcoblue" bordered>ICMP</a-tag>
                <a-tag color="arcoblue" bordered>ARP</a-tag>
                <a-tag color="arcoblue" bordered>CDP</a-tag>
                <a-tag color="arcoblue" bordered>LLDP</a-tag>
              </div>
            </div>
            <div class="config-item">
              <p class="config-label">自动扫描</p>
              <div class="flex items-center justify-between">
                <span class="text-sm">每 6 小时</span>
                <a-switch :model-value="true" type="round" />
              </div>
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>

    <!-- 扫描历史 -->
    <a-card class="mt-6" :bordered="false" title="扫描历史">
      <a-table
        :data="scanHistory"
        :columns="historyColumns"
        :pagination="false"
        row-key="time"
      >
        <template #newDevices="{ record }">
          <span v-if="record.newDevices > 0" class="text-warning">+{{ record.newDevices }}</span>
          <span v-else class="text-muted">0</span>
        </template>
        <template #status="{ record }">
          <a-tag :color="record.status === 'success' ? 'green' : 'orange'" bordered>
            {{ record.status === 'success' ? '完成' : '部分完成' }}
          </a-tag>
        </template>
      </a-table>
    </a-card>

    <!-- 发现的设备 -->
    <a-card class="mt-6" :bordered="false">
      <template #title>
        <div class="table-header">
          <span>发现的设备</span>
          <a-space>
            <a-select v-model="deviceTypeFilter" placeholder="全部类型" style="width: 120px">
              <a-option value="">全部类型</a-option>
              <a-option value="switch">交换机</a-option>
              <a-option value="router">路由器</a-option>
              <a-option value="server">服务器</a-option>
              <a-option value="unknown">未知设备</a-option>
            </a-select>
            <a-select v-model="deviceStatusFilter" placeholder="全部状态" style="width: 120px">
              <a-option value="">全部状态</a-option>
              <a-option value="online">在线</a-option>
              <a-option value="offline">离线</a-option>
              <a-option value="new">新发现</a-option>
            </a-select>
          </a-space>
        </div>
      </template>
      <a-table
        :data="discoveredDevices"
        :columns="deviceColumns"
        row-key="ip"
      >
        <template #status="{ record }">
          <a-tag v-if="record.status === 'online'" color="green" bordered>在线</a-tag>
          <a-tag v-else-if="record.status === 'offline'" color="red" bordered>离线</a-tag>
          <a-tag v-else color="orange" bordered>新发现</a-tag>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import {
  IconRefresh,
  IconCheckCircle,
  IconSettings,
  IconDesktop,
  IconWifi,
  IconExclamationCircle,
  IconPlayCircle,
  IconSafe,
  IconApps,
  IconClockCircle,
  IconArrowRise,
} from '@arco-design/web-vue/es/icon'
import type { TableColumnData } from '@arco-design/web-vue/es/table/interface'

// 扫描状态
const isScanning = ref(false)

// 设备筛选
const deviceTypeFilter = ref('')
const deviceStatusFilter = ref('')

// 启动扫描
const handleScan = () => {
  isScanning.value = true
  setTimeout(() => {
    isScanning.value = false
  }, 3000)
}

// 发现的设备数据
const discoveredDevices = ref([
  {
    ip: '10.0.0.1',
    mac: '00:1A:2B:3C:4D:01',
    hostname: 'Core-SW-01',
    vendor: 'Cisco Systems',
    type: '交换机',
    status: 'online',
    lastSeen: '刚刚',
    method: 'SNMP',
  },
  {
    ip: '10.0.0.2',
    mac: '00:1A:2B:3C:4D:02',
    hostname: 'Core-SW-02',
    vendor: 'Cisco Systems',
    type: '交换机',
    status: 'online',
    lastSeen: '刚刚',
    method: 'SNMP',
  },
  {
    ip: '10.0.1.1',
    mac: '00:1A:2B:3C:4D:10',
    hostname: 'Router-01',
    vendor: 'Cisco Systems',
    type: '路由器',
    status: 'online',
    lastSeen: '1分钟前',
    method: 'ICMP+ARP',
  },
  {
    ip: '10.0.2.50',
    mac: '00:50:56:AB:CD:01',
    hostname: 'ESXi-01',
    vendor: 'VMware, Inc.',
    type: '服务器',
    status: 'online',
    lastSeen: '刚刚',
    method: 'SNMP',
  },
  {
    ip: '10.0.3.100',
    mac: '00:00:5E:00:01:01',
    hostname: 'Unknown',
    vendor: 'Dell Technologies',
    type: '未知',
    status: 'new',
    lastSeen: '5分钟前',
    method: 'ARP',
  },
  {
    ip: '10.0.2.200',
    mac: '00:1A:2B:3C:4D:FF',
    hostname: 'Printer-01',
    vendor: 'HP Inc.',
    type: '打印机',
    status: 'offline',
    lastSeen: '2小时前',
    method: 'ICMP',
  },
])

// 设备表格列
const deviceColumns: TableColumnData[] = [
  { title: 'IP地址', dataIndex: 'ip', width: 120 },
  { title: 'MAC地址', dataIndex: 'mac', width: 150 },
  { title: '主机名', dataIndex: 'hostname', width: 120 },
  { title: '厂商', dataIndex: 'vendor', width: 140 },
  { title: '设备类型', dataIndex: 'type', width: 100 },
  { title: '状态', dataIndex: 'status', slotName: 'status', width: 100, align: 'center' },
  { title: '最后发现', dataIndex: 'lastSeen', width: 100 },
  { title: '发现方式', dataIndex: 'method', width: 100 },
]

// 扫描历史数据
const scanHistory = ref([
  { time: '2024-01-15 14:30:00', type: '全网扫描', duration: '12分钟', devices: 156, newDevices: 2, status: 'success' },
  { time: '2024-01-15 12:00:00', type: '增量扫描', duration: '3分钟', devices: 154, newDevices: 0, status: 'success' },
  { time: '2024-01-15 08:00:00', type: '全网扫描', duration: '15分钟', devices: 154, newDevices: 1, status: 'success' },
  { time: '2024-01-14 20:00:00', type: '增量扫描', duration: '2分钟', devices: 153, newDevices: 0, status: 'success' },
  { time: '2024-01-14 14:30:00', type: '全网扫描', duration: '18分钟', devices: 153, newDevices: 3, status: 'warning' },
])

// 扫描历史表格列
const historyColumns: TableColumnData[] = [
  { title: '时间', dataIndex: 'time', width: 160 },
  { title: '类型', dataIndex: 'type', width: 100 },
  { title: '耗时', dataIndex: 'duration', width: 80 },
  { title: '设备数', dataIndex: 'devices', width: 80, align: 'center' },
  { title: '新增', dataIndex: 'newDevices', slotName: 'newDevices', width: 80, align: 'center' },
  { title: '状态', dataIndex: 'status', slotName: 'status', width: 100, align: 'center' },
]
</script>

<script lang="ts">
export default {
  name: 'AutoTopology',
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;

  .page-title {
    h2 {
      margin: 0;
      font-size: 20px;
      font-weight: 500;
      color: var(--color-text-1);
    }

    .page-subtitle {
      margin: 8px 0 0;
      font-size: 14px;
      color: var(--color-text-3);
    }
  }

  .page-actions {
    display: flex;
    gap: 8px;
  }
}

.mr-2 {
  margin-right: 8px;
}

.stats-row {
  margin-bottom: 0;
}

.stats-card {
  .stats-content {
    display: flex;
    align-items: center;
    gap: 16px;
  }

  .stats-icon {
    width: 48px;
    height: 48px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;

    &-primary {
      background: rgba(22, 93, 255, 0.1);
      color: #165dff;
    }

    &-success {
      background: rgba(0, 180, 42, 0.1);
      color: #00b42a;
    }

    &-warning {
      background: rgba(255, 125, 0, 0.1);
      color: #ff7d00;
    }

    &-default {
      background: rgba(134, 144, 156, 0.1);
      color: #86909c;
    }
  }

  .stats-info {
    flex: 1;
  }

  .stats-title {
    font-size: 14px;
    color: var(--color-text-3);
    margin-bottom: 4px;
  }

  .stats-value {
    font-size: 24px;
    font-weight: 600;
    color: var(--color-text-1);
    line-height: 1.2;
  }

  .stats-desc {
    font-size: 12px;
    margin-top: 4px;
    display: flex;
    align-items: center;
    gap: 4px;

    &.text-success {
      color: #00b42a;
    }
  }
}

.mt-6 {
  margin-top: 24px;
}

.scan-row {
  display: flex;
  align-items: stretch;
}

.scan-col {
  display: flex;

  :deep(.arco-card) {
    flex: 1;
    display: flex;
    flex-direction: column;

    .arco-card-body {
      flex: 1;
      display: flex;
      flex-direction: column;
    }
  }
}

.scan-card {
  height: 100%;

  :deep(.arco-card-body) {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.scanning-content,
.scan-complete-content {
  padding: 8px 0;
}

.stats-grid {
  background: var(--color-fill-2);
  border-radius: 8px;
  padding: 16px;
}

.config-section {
  .config-item {
    margin-bottom: 16px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .config-label {
    font-size: 12px;
    color: var(--color-text-3);
    margin-bottom: 8px;
  }

  .config-value-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .config-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text-primary {
  color: #165dff;
}

.text-success {
  color: #00b42a;
}

.text-warning {
  color: #ff7d00;
}

.text-muted {
  color: var(--color-text-3);
}

.flex {
  display: flex;
}

.items-center {
  align-items: center;
}

.justify-between {
  justify-content: space-between;
}

.gap-4 {
  gap: 16px;
}

.flex-1 {
  flex: 1;
}

.h-5 {
  height: 20px;
}

.w-5 {
  width: 20px;
}

.font-medium {
  font-weight: 500;
}

.font-semibold {
  font-weight: 600;
}

.text-sm {
  font-size: 14px;
}

.text-xs {
  font-size: 12px;
}

.text-lg {
  font-size: 18px;
}

.text-center {
  text-align: center;
}

.mb-4 {
  margin-bottom: 16px;
}

.animate-spin {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
