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

      <el-form-item label="设备状态" prop="deviceStatus">
        <el-select v-model="queryParams.deviceStatus" placeholder="请选择状态" style="width: 180px;">
          <el-option label="全部" value="" />
          <el-option label="在线" value="Online" />
          <el-option label="离线" value="Offline" />
        </el-select>
      </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="doorDevices" 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="location" />
      <el-table-column label="门状态" align="center" prop="doorStatus" />
      <el-table-column label="设备状态" align="center">
        <template #default="scope">
          <el-tag :type="getDeviceStatusType(scope.row.deviceStatus)">
            {{ scope.row.deviceStatus === "Online" ? "在线" : "离线" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最后事件时间" align="center" prop="lastEventTime" />
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="操作" align="center" prop="actions" />
      <el-table-column label="处理状态" align="center" prop="processStatus" />
    </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="DoorDevices">
import { listAccessControl } from "@/api/accessControl/accessControl";
import { ref, reactive } from "vue";

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

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  location: '',
  deviceStatus: '',
  interfaceName: "门禁设备"
});

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

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

// 重置按钮操作
function resetQuery() {
  queryRef.value.resetFields(); // 重置表单字段
  handleQuery(); // 重新查询
}
// 根据设备状态返回标签类型
function getDeviceStatusType(status) {
  if (status === "Online") return "success"; // 在线
  if (status === "Offline") return "danger"; // 离线
  return "info"; // 默认
}
// 初始化加载数据
getList();
</script>
<style scoped>
.el-table .cell {
  white-space: nowrap;
}
</style>
