<template>
  <div class="security-monitoring">
    <!-- 搜索栏 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="filter-form">
        <el-form-item label="摄像头ID" prop="cameraId">
          <el-input
              v-model="queryParams.cameraId"
              placeholder="请输入摄像头ID（如：CAM2024001）"
              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-card>

    <!-- 数据表格 -->
    <el-card class="device-table">
      <el-table v-loading="loading" :data="cameras" border>
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column label="摄像头ID" align="center" prop="cameraId" />
        <el-table-column label="位置" align="center" prop="location" />
        <el-table-column label="状态" align="center" prop="status" width="80">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="录像存储路径" align="left" prop="storagePath" />
        <el-table-column label="最后触发时间" align="center" prop="lastTriggerTime" />
        <el-table-column label="报警类型" align="center" prop="alarmType" />
      </el-table>

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

<script setup>
import { ref, reactive } from "vue";
import {getEstateManagementSecurityDataList} from "@/api/estateManagement/estateManagement"; // 假设API路径

// 查询参数（两个条件：cameraId、location）
const queryParams = reactive({
  cameraId: "", // 摄像头ID（精确查询）
  location: "", // 安装位置（支持模糊查询）
  pageNum: 1,
  pageSize: 10,
  interfaceName: "安防监控中心"
});

// 表格数据
const cameras = ref([]);
const total = ref(0);
const loading = ref(false);

// 获取安防数据列表
const getList = async () => {
  loading.value = true;
  try {
    const response = await getEstateManagementSecurityDataList(queryParams);
    cameras.value =  [];
    total.value =  0;
  } catch (error) {
    console.error("获取安防数据失败", error);
  } finally {
    loading.value = false;
  }
};

// 搜索方法
const handleQuery = () => {
  queryParams.pageNum = 1; // 重置页码
  getList();
};

// 重置方法
const resetQuery = () => {
  queryParams.cameraId = "";
  queryParams.location = "";
  handleQuery();
};

// 分页事件
const handleSizeChange = (newSize) => {
  queryParams.pageSize = newSize;
  getList();
};

const handleCurrentChange = (newPage) => {
  queryParams.pageNum = newPage;
  getList();
};

// 状态标签类型映射
const getStatusType = (status) => {
  if (status === "在线") return "success";
  if (status === "故障") return "danger";
  return "info";
};

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

<style scoped>
.filter-card {
  margin-bottom: 20px;
}

.el-table .cell {
  white-space: normal; /* 允许长路径换行 */
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
