<template>
  <div class="facility-management">
    <!-- 搜索栏 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="filter-form">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input
              v-model="queryParams.deviceId"
              placeholder="请输入设备ID（如：DEV2024001）"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="设备类型" prop="deviceType">
          <el-select
              v-model="queryParams.deviceType"
              placeholder="请选择设备类型"
              style="width: 180px;"
          >
            <el-option label="全部" value="" />
            <el-option label="电梯" value="电梯" />
            <el-option label="水泵" value="水泵" />
            <el-option label="配电箱" value="配电箱" />
            <el-option label="空调" value="空调" />
            <el-option label="消防设备" value="消防设备" />
            <el-option label="监控设备" value="监控设备" />
          </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-card>

    <!-- 数据表格 -->
    <el-card class="device-table">
      <el-table v-loading="loading" :data="facilities" border>
        <el-table-column label="序号" type="index" align="center"  />
        <el-table-column label="设备ID" align="center" prop="deviceId"  />
        <el-table-column label="名称" align="center" prop="name"  />
        <el-table-column label="安装位置" align="center" prop="location"  />
        <el-table-column label="品牌型号" align="center" prop="brandModel"  />
        <el-table-column label="维保周期" align="center" prop="maintenanceCycle"  />
        <el-table-column label="当前状态" align="center" prop="status" >
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="上次维保日期" align="center" prop="lastMaintenanceDate"  />
      </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 {getEstateManagementFacilityDataList} from "@/api/estateManagement/estateManagement"; // 假设API路径

// 查询参数
const queryParams = reactive({
  deviceId: "", // 设备ID（支持精确查询）
  deviceType: "", // 设备类型（电梯/水泵/配电箱等）
  pageNum: 1, // 当前页码
  pageSize: 10, // 每页条数
  interfaceName: "设施设备台账"
});

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

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

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

// 重置方法
const resetQuery = () => {
  queryParams.deviceId = "";
  queryParams.deviceType = "";
  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 "warning";
  if (status === "维修中") return "danger";
  return "info";
};

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

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

.el-table .cell {
  white-space: nowrap;
}

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