<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="输入设备名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="安装位置" prop="location">
        <el-input
            v-model="queryParams.location"
            placeholder="输入安装位置"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="devices" border>
      <el-table-column label="序号" type="index" align="center" width="50" />
      <el-table-column label="设备ID" align="center" prop="id" />
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="安装位置" align="center" prop="location" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="IP地址" align="center" prop="ipAddress" />
      <el-table-column label="屏幕尺寸" align="center" prop="screenSize" />
      <el-table-column label="最后在线时间" align="center" prop="lastOnlineTime" />
    </el-table>

    <!-- 分页器 -->
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="DeviceManagement">
import { ref, reactive } from "vue";
import {driverInfoList} from "@/api/driverInfo/driverInfo";

// 定义响应式变量
const queryRef = ref(null);
const devices = ref([]); // 设备数据
const loading = ref(false); // 加载状态
const showSearch = ref(true); // 是否显示搜索栏
const total = ref(0); // 总记录数

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: '',
  location: '',
  interfaceName: "信息引导及发布"
});

// 获取设备列表
const getList = async () => {
  try {
    loading.value = true;
    const response = await driverInfoList(queryParams);
    devices.value =  []; // 确保数据格式正确
    total.value =  0; // 确保总记录数正确
  } catch (error) {
    console.error('获取数据失败:', error);
  } finally {
    loading.value = false;
  }
};

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1; // 重置到第一页
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  queryRef.value.resetFields(); // 重置表单字段
  handleQuery(); // 重新查询
};

// 根据状态返回标签类型
const getStatusType = (status) => {
  if (status === "在线") return "success";
  if (status === "离线") return "warning";
  if (status === "故障") return "danger";
  return "info";
};

// 初始化加载数据
getList();
</script>

<style scoped>
.el-table .cell {
  white-space: nowrap;
}
</style>
