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

const router = useRouter()

const searchText = ref('')
const activeTab = ref(0)

interface Equipment {
  id: number
  name: string
  type: string
  typeLabel: string
  status: 'normal' | 'alarm' | 'offline'
  location: string
  lastData: string
  updateTime: string
}

const mockEquipments: Equipment[] = [
  { 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 statusCfg: Record<string, { label: string; color: string; bg: string }> = {
  normal: { label: '正常', color: '#07C160', bg: '#E8F8EF' },
  alarm: { label: '报警', color: '#EE0A24', bg: '#FDECEC' },
  offline: { label: '离线', color: '#969799', bg: '#F2F3F5' },
}

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="page">
    <!-- NavBar -->
    <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>

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

    <!-- Type Tabs -->
    <van-tabs v-model:active="activeTab" sticky swipeable class="tabs-bar">
      <van-tab v-for="(tab, idx) in typeTabs" :key="idx" :title="tab" />
    </van-tabs>

    <!-- Equipment List -->
    <div class="content">
      <van-empty v-if="filteredEquipments.length === 0" description="暂无监测设备" />

      <div
        v-for="equip in filteredEquipments"
        :key="equip.id"
        class="equip-card"
        @click="goEquipmentInfo(equip.id)"
      >
        <!-- Blue accent bar -->
        <div class="card-accent" />

        <div class="card-body">
          <div class="card-header">
            <span class="card-title">{{ equip.name }}</span>
            <span
              class="status-tag"
              :style="{ color: statusCfg[equip.status].color, background: statusCfg[equip.status].bg }"
            >
              {{ statusCfg[equip.status].label }}
            </span>
          </div>

          <div class="card-info">
            <div class="info-row">
              <van-icon name="label-o" size="14" color="#969799" />
              <span>{{ equip.typeLabel }}</span>
            </div>
            <div class="info-row">
              <van-icon name="location-o" size="14" color="#969799" />
              <span>{{ equip.location }}</span>
            </div>
          </div>

          <div class="card-footer">
            <div class="data-block">
              <span class="data-label">最新数据</span>
              <span class="data-value">{{ equip.lastData }}</span>
            </div>
            <span class="update-time">{{ equip.updateTime }}</span>
          </div>

          <van-icon name="arrow" color="#C8C9CC" class="card-arrow" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background: #F4F7F8;
}

// ── NavBar ──
:deep(.van-nav-bar) {
  background: #1E74FF;
  --van-nav-bar-title-text-color: #fff;
  --van-nav-bar-text-color: #fff;
  --van-nav-bar-icon-color: #fff;
}

// ── Search ──
.search-bar {
  :deep(.van-search__content) {
    background: #fff;
  }
}

// ── Tabs ──
.tabs-bar {
  :deep(.van-tabs__nav) {
    background: #fff;
  }
}

// ── Content ──
.content {
  padding: 8px 12px;
}

// ── Card ──
.equip-card {
  display: flex;
  position: relative;
  margin-bottom: 10px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;

  &:active {
    transform: scale(0.985);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}

.card-accent {
  width: 4px;
  flex-shrink: 0;
  background: #1E74FF;
}

.card-body {
  flex: 1;
  padding: 14px 16px 14px 14px;
  position: relative;
  min-width: 0;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;

  .card-title {
    font-size: 16px;
    font-weight: 600;
    color: #323233;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .status-tag {
    flex-shrink: 0;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
  }
}

.card-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;

  .info-row {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: #646566;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 10px;
  border-top: 1px solid #F2F3F5;

  .data-block {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .data-label {
    font-size: 11px;
    color: #969799;
  }

  .data-value {
    font-size: 13px;
    font-weight: 500;
    color: #323233;
  }

  .update-time {
    font-size: 11px;
    color: #C8C9CC;
  }
}

.card-arrow {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
}
</style>
