<template>
  <div class="energy-monitoring">
    <!-- 搜索栏 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="filter-form">
        <el-form-item label="计量点ID" prop="meterId">
          <el-input
              v-model="queryParams.meterId"
              placeholder="请输入计量点ID（如：EM2024001）"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="楼栋号" prop="buildingNo">
          <el-input
              v-model="queryParams.buildingNo"
              placeholder="请输入楼栋号（如：1栋）"
              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="energyRecords" border>
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column label="计量点ID" align="center" prop="meterId" />
        <el-table-column label="楼栋号" align="center" prop="buildingNo" />
        <el-table-column label="上月读数" align="right" prop="lastMonthReading" />
        <el-table-column label="本月读数" align="right" prop="currentMonthReading" />
        <el-table-column label="异常用量预警值" align="right" prop="warningValue" />
        <el-table-column label="同比变化率" align="center" prop="yoyChange" >
          <template #default="scope">
            <el-tag :type="getChangeType(scope.row.yoyChange)">
              {{ scope.row.yoyChange }}
            </el-tag>
          </template>
        </el-table-column>
      </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 {getEstateManagementEnergyDataList} from "@/api/estateManagement/estateManagement"; // 假设API路径

// 查询参数（两个条件：meterId、buildingNo）
const queryParams = reactive({
  meterId: "", // 计量点ID（精确查询）
  buildingNo: "", // 楼栋号（支持模糊查询）
  pageNum: 1,
  pageSize: 10,
  interfaceName: "能源监测台"
});

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

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

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

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

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

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

// 同比变化率标签类型映射
const getChangeType = (changeRate) => {
  const rate = parseFloat(changeRate);
  if (rate > 10) return "danger"; // 超过10%显示红色警告
  if (rate > 5) return "warning"; // 超过5%显示黄色警告
  return "success"; // 低于5%显示绿色正常
};

// 初始化加载
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>
