<script setup lang="ts">
/**
 * 监测设备列表页
 *
 * 展示监测设备列表，支持搜索和类型筛选，
 * 点击卡片跳转设备详情，可进入地图监控和监测详情。
 */
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

/** 搜索关键词 */
const searchText = ref('')

/** 当前激活的 Tab */
const activeTab = ref(0)

/** 模拟监测设备数据 */
const mockEquipments = [
  { id: 1, name: '流量监测仪 A-01', type: 'flow_meter', typeLabel: '流量计', status: 'normal', location: '城北供水管网1号节点', lastData: '125.6 m³/h', updateTime: '2025-06-15 08:30' },
  { id: 2, name: '压力传感器 P-03', type: 'pressure_sensor', typeLabel: '压力传感器', status: 'alarm', location: '高新区主管网3号泵站', lastData: '0.85 MPa', updateTime: '2025-06-15 08:25' },
  { id: 3, name: '水质监测仪 W-07', type: 'water_quality', typeLabel: '水质监测仪', status: 'normal', location: '老城区饮用水源口', lastData: 'pH 7.2 / 浊度 0.5NTU', updateTime: '2025-06-15 08:28' },
  { id: 4, name: '液位计 L-02', type: 'liquid_level', typeLabel: '液位计', status: 'offline', location: '东区蓄水池2号', lastData: '3.2 m', updateTime: '2025-06-14 22:00' },
  { id: 5, name: '流量监测仪 A-05', type: 'flow_meter', typeLabel: '流量计', status: 'normal', location: '商业区供水管网12号', lastData: '98.3 m³/h', updateTime: '2025-06-15 08:32' },
  { id: 6, name: '水质监测仪 W-02', type: 'water_quality', typeLabel: '水质监测仪', status: 'alarm', location: '南城污水处理厂出水口', lastData: 'COD 85mg/L (超标)', updateTime: '2025-06-15 08:20' },
  { id: 7, name: '压力传感器 P-07', type: 'pressure_sensor', typeLabel: '压力传感器', status: 'normal', location: '城西加压站', lastData: '0.62 MPa', updateTime: '2025-06-15 08:31' },
  { id: 8, name: '液位计 L-05', type: 'liquid_level', typeLabel: '液位计', status: 'offline', location: '北区污水井5号', lastData: '1.8 m', updateTime: '2025-06-14 18:00' },
]

/** 状态映射 */
const statusMap: Record<string, string> = {
  normal: '正常',
  alarm: '报警',
  offline: '离线',
}

const statusColorMap: Record<string, string> = {
  normal: 'success',
  alarm: 'danger',
  offline: '#999',
}

/** 类型 Tab 映射 */
const typeTabs = ['全部', '流量计', '压力传感器', '水质监测仪', '液位计']
const typeKeys = ['', 'flow_meter', 'pressure_sensor', 'water_quality', 'liquid_level']

/** 筛选后的列表 */
const filteredEquipments = computed(() => {
  let list = mockEquipments
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(e =>
      e.name.toLowerCase().includes(kw) ||
      e.typeLabel.toLowerCase().includes(kw) ||
      e.location.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value > 0 && typeKeys[activeTab.value]) {
    list = list.filter(e => e.type === typeKeys[activeTab.value])
  }
  return list
})

/** 跳转设备详情 */
function goEquipmentInfo(id: number) {
  router.push(`/equipmentInfo?id=${id}`)
}

/** 跳转地图监控 */
function goMapMonitoring() {
  router.push('/mapMonitoring')
}
</script>

<template>
  <div class="equipment-page">
    <van-nav-bar
      title="监测设备"
      left-arrow fixed placeholder
      @click-left="router.back()"
    >
      <template #right>
        <van-icon name="map-marked" size="20" @click="goMapMonitoring" />
      </template>
    </van-nav-bar>

    <van-search v-model="searchText" placeholder="搜索设备名称、类型、位置" shape="round" />

    <van-tabs v-model:active="activeTab" sticky scrollspy>
      <van-tab v-for="(tab, idx) in typeTabs" :key="idx" :title="tab" />
    </van-tabs>

    <div class="equipment-list">
      <van-empty v-if="filteredEquipments.length === 0" description="暂无监测设备" />
      <van-card
        v-for="equip in filteredEquipments"
        :key="equip.id"
        :title="equip.name"
        :desc="`类型: ${equip.typeLabel}`"
        @click="goEquipmentInfo(equip.id)"
      >
        <template #tags>
          <van-tag :color="statusColorMap[equip.status]" text-color="#fff" size="medium" v-if="equip.status !== 'normal'">
            {{ statusMap[equip.status] }}
          </van-tag>
          <van-tag type="success" size="medium" v-else>
            {{ statusMap[equip.status] }}
          </van-tag>
        </template>
        <template #footer>
          <div class="equipment-meta">
            <span class="meta-location">{{ equip.location }}</span>
            <span class="meta-data">最新数据: {{ equip.lastData }}</span>
            <span class="meta-time">更新时间: {{ equip.updateTime }}</span>
          </div>
        </template>
      </van-card>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.equipment-page {
  min-height: 100vh;
  background: var(--color-bg-page);

  :deep(.van-nav-bar) {
    background: var(--color-primary);
    --van-nav-bar-title-text-color: #fff;
    --van-nav-bar-text-color: #fff;
    --van-nav-bar-icon-color: #fff;
  }
}

.equipment-list {
  padding: 0 8px;

  :deep(.van-card) {
    margin: 8px;
    border-radius: 10px;
    background: var(--color-bg-card);
  }

  :deep(.van-tag) {
    margin-right: 4px;
  }
}

.equipment-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 12px;
  color: var(--color-text-secondary);

  .meta-location {
    color: var(--color-text-regular);
  }

  .meta-data {
    font-weight: 500;
  }

  .meta-time {
    color: var(--color-text-placeholder);
  }
}
</style>
