<template>
  <div class="devices-page">
    <ElCard shadow="never">
      <template #header>
        <div class="card-header">
          <span>AMT 设备列表</span>
          <div class="header-actions">
            <ElTag v-if="isCheckingStatus" type="info" size="small" style="margin-right: 10px">
              <el-icon class="is-loading"><Refresh /></el-icon>
              检测中...
            </ElTag>
            <ElInput
              v-model="searchKeyword"
              placeholder="搜索 IP 地址"
              style="width: 200px; margin-right: 10px"
              clearable
              @clear="handleSearch"
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </ElInput>
            <ElButton type="primary" :icon="Refresh" @click="handleRefresh">刷新</ElButton>
          </div>
        </div>
      </template>
      
      <ElTable :data="devices" v-loading="loading" stripe style="width: 100%">
        <ElTableColumn prop="ipAddress" label="IP 地址" width="140" />
        <ElTableColumn label="AMT 版本" width="100">
          <template #default="{ row }">
            {{ row.majorVersion }}.{{ row.minorVersion }}
          </template>
        </ElTableColumn>
        <ElTableColumn label="配置状态" width="100">
          <template #default="{ row }">
            <ElTag :type="getStateTagType(row.provisioningState)" size="small">
              {{ getProvisioningStateText(row.provisioningState) }}
            </ElTag>
          </template>
        </ElTableColumn>
        <ElTableColumn label="AMT状态" width="90">
          <template #default="{ row }">
            <ElTag :type="row.amtOnline ? 'success' : 'info'" size="small">
              {{ row.amtOnline ? '在线' : '离线' }}
            </ElTag>
          </template>
        </ElTableColumn>
        <ElTableColumn label="系统状态" width="90">
          <template #default="{ row }">
            <ElTag :type="row.osOnline ? 'success' : 'danger'" size="small">
              {{ row.osOnline ? '运行中' : '已关机' }}
            </ElTag>
          </template>
        </ElTableColumn>
        <ElTableColumn prop="hostname" label="主机名" min-width="120" />
        <ElTableColumn label="发现时间" width="160">
          <template #default="{ row }">
            {{ formatDateTime(row.discoveredAt) }}
          </template>
        </ElTableColumn>
        <ElTableColumn label="操作" width="380" fixed="right">
          <template #default="{ row }">
            <ElButton type="success" size="small" @click="handleRemoteDesktop(row)" :disabled="!row.osOnline">
              远程桌面
            </ElButton>
            <ElDropdown trigger="click" @command="(cmd: string) => handlePowerCommand(cmd, row)" style="margin-left: 8px">
              <ElButton type="warning" size="small">
                电源管理 <el-icon class="el-icon--right"><ArrowDown /></el-icon>
              </ElButton>
              <template #dropdown>
                <ElDropdownMenu>
                  <ElDropdownItem command="power-on" :icon="VideoPlay">开机</ElDropdownItem>
                  <ElDropdownItem command="power-off" :icon="VideoPause">关机</ElDropdownItem>
                  <ElDropdownItem command="restart" :icon="RefreshRight">重启</ElDropdownItem>
                  <ElDropdownItem divided command="force-off" :icon="CircleClose">强制关机</ElDropdownItem>
                  <ElDropdownItem command="force-restart" :icon="Refresh">强制重启</ElDropdownItem>
                </ElDropdownMenu>
              </template>
            </ElDropdown>
            <ElButton type="primary" size="small" @click="handleViewHardware(row)" style="margin-left: 8px">
              硬件配置
            </ElButton>
            <ElButton type="danger" size="small" @click="handleDelete(row)">
              删除
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>
    </ElCard>

    <!-- 硬件信息弹窗 -->
    <HardwareInfoModal v-model="showHardwareModal" :device-id="selectedDeviceId" />

    <!-- 远程桌面弹窗 -->
    <RemoteDesktopModal v-model="showRemoteDesktopModal" :device="selectedDevice" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, ArrowDown, VideoPlay, VideoPause, RefreshRight, CircleClose } from '@element-plus/icons-vue'
import { deviceApi, powerApi } from '@/api/amt'
import HardwareInfoModal from './modules/hardware-info-modal.vue'
import RemoteDesktopModal from './modules/remote-desktop-modal.vue'

defineOptions({ name: 'AmtDevices' })

const devices = ref<any[]>([])
const loading = ref(false)
const isCheckingStatus = ref(false)
const searchKeyword = ref('')
const showHardwareModal = ref(false)
const showRemoteDesktopModal = ref(false)
const selectedDeviceId = ref(0)
const selectedDevice = ref<any>(null)

let statusCheckInterval: number | null = null

onMounted(() => {
  fetchDevices()
  startStatusCheck()
})

onUnmounted(() => {
  stopStatusCheck()
})

const fetchDevices = async () => {
  loading.value = true
  try {
    devices.value = await deviceApi.getAllDevices()
  } catch (error) {
    console.error('获取设备列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = async () => {
  if (searchKeyword.value) {
    loading.value = true
    try {
      devices.value = await deviceApi.searchDevices(searchKeyword.value)
    } catch (error) {
      console.error('搜索设备失败:', error)
    } finally {
      loading.value = false
    }
  } else {
    fetchDevices()
  }
}

const handleRefresh = async () => {
  await fetchDevices()
  await checkAllDevicesStatus()
  ElMessage.success('刷新成功')
}

const checkAllDevicesStatus = async () => {
  if (isCheckingStatus.value || devices.value.length === 0) return
  
  isCheckingStatus.value = true
  try {
    const statusList = await deviceApi.checkAllDevicesStatus()
    const statusMap = new Map(statusList.map((s: any) => [s.id, { amtOnline: s.amtOnline, osOnline: s.osOnline }]))
    
    devices.value.forEach(device => {
      if (statusMap.has(device.id)) {
        const status = statusMap.get(device.id)
        device.amtOnline = status.amtOnline
        device.osOnline = status.osOnline
      }
    })
  } catch (error) {
    console.error('检测设备状态失败:', error)
  } finally {
    isCheckingStatus.value = false
  }
}

const startStatusCheck = () => {
  checkAllDevicesStatus()
  statusCheckInterval = window.setInterval(() => {
    checkAllDevicesStatus()
  }, 30000)
}

const stopStatusCheck = () => {
  if (statusCheckInterval) {
    clearInterval(statusCheckInterval)
    statusCheckInterval = null
  }
}

const handleDelete = async (device: any) => {
  try {
    await ElMessageBox.confirm(`确定要删除设备 ${device.ipAddress} 吗？`, '确认删除', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await deviceApi.deleteDevice(device.id)
    fetchDevices()
  } catch (error: any) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

const handleViewHardware = (device: any) => {
  selectedDeviceId.value = device.id
  showHardwareModal.value = true
}

const handleRemoteDesktop = (device: any) => {
  if (!device.osOnline) {
    ElMessage.warning('设备操作系统未运行，无法连接远程桌面')
    return
  }
  selectedDevice.value = device
  showRemoteDesktopModal.value = true
}

const handlePowerCommand = async (command: string, device: any) => {
  const actionMap: Record<string, { api: Function; name: string; confirmMsg: string }> = {
    'power-on': { api: powerApi.powerOn, name: '开机', confirmMsg: '确定要开机吗？' },
    'power-off': { api: powerApi.powerOff, name: '关机', confirmMsg: '确定要关机吗？这将优雅地关闭操作系统。' },
    'restart': { api: powerApi.restart, name: '重启', confirmMsg: '确定要重启吗？这将优雅地重启操作系统。' },
    'force-off': { api: powerApi.forceOff, name: '强制关机', confirmMsg: '确定要强制关机吗？这可能导致数据丢失！' },
    'force-restart': { api: powerApi.forceRestart, name: '强制重启', confirmMsg: '确定要强制重启吗？这可能导致数据丢失！' }
  }

  const action = actionMap[command]
  if (!action) return

  try {
    await ElMessageBox.confirm(`设备: ${device.ipAddress}\n${action.confirmMsg}`, `确认${action.name}`, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: command.includes('force') ? 'warning' : 'info'
    })

    ElMessage.info(`正在执行${action.name}...`)
    
    const response = await action.api(device.id)
    
    if (response.success) {
      ElMessage.success(response.message || `${action.name}命令已发送`)
      setTimeout(() => checkAllDevicesStatus(), 3000)
    } else {
      ElMessage.error(response.error || `${action.name}失败`)
    }
  } catch (error: any) {
    if (error !== 'cancel') {
      ElMessage.error(`${action.name}失败`)
    }
  }
}

const getProvisioningStateText = (state: string) => {
  const stateMap: Record<string, string> = {
    'PRE': '预配置',
    'IN': '配置中',
    'POST': '已配置',
    'UNKNOWN': '未知'
  }
  return stateMap[state] || state
}

const getStateTagType = (state: string) => {
  const typeMap: Record<string, string> = {
    'PRE': 'warning',
    'IN': 'primary',
    'POST': 'success',
    'UNKNOWN': 'info'
  }
  return typeMap[state] || 'info'
}

const formatDateTime = (dateTime: string) => {
  if (!dateTime) return '-'
  return new Date(dateTime).toLocaleString('zh-CN')
}
</script>

<style scoped>
.devices-page {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  font-weight: 500;
}

.header-actions {
  display: flex;
  align-items: center;
}
</style>
