<template>
  <div class="broadcast-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（如：BRD-001）"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="所属区域" prop="area">
          <el-input
              v-model="queryParams.area"
              placeholder="请输入区域（如：1号楼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="deviceList" 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="deviceName" />
        <el-table-column label="设备类型" align="center" prop="deviceType" />
        <el-table-column label="所属区域" align="center" prop="area" />
        <el-table-column label="运行状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status === 'green' ? '正常' : scope.row.status === 'red' ? '故障' : '警告' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="音量值" align="right" prop="volume">
          <template #default="scope">
            <span>{{ scope.row.volume }} dB</span>
          </template>
        </el-table-column>
        <el-table-column label="故障代码" align="center" prop="faultCode">
          <template #default="scope">
            <span v-if="scope.row.faultCode" style="color: #ff4d4f">{{ scope.row.faultCode }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="最后检测时间" align="center" prop="lastCheckTime" />
      </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 { getBroadcastDeviceList } from "@/api/publicBroadcasting/publicBroadcasting"; // 假设API路径

// 查询参数（两个条件：deviceId、area）
const queryParams = reactive({
  deviceId: "", // 设备ID（精确查询）
  area: "", // 所属区域（支持模糊查询）
  pageNum: 1,
  pageSize: 10,
  interfaceName: "广播设备管理"
});

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

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

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

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

// 状态标签类型映射
const getStatusType = (status) => {
  switch (status) {
    case 'green': return 'success';
    case 'red': return 'danger';
    case 'yellow': return 'warning';
    default: return '';
  }
};

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

/* 状态标签样式调整 */
.el-tag.success {
  background-color: #52c41a !important;
  color: white;
}

.el-tag.danger {
  background-color: #ff4d4f !important;
  color: white;
}

.el-tag.warning {
  background-color: #faad14 !important;
  color: white;
}
</style>
