<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 mockTasks = [
  { id: 1, name: '管网巡检-城北片区', facility: '供水管网', status: 'pending', planDate: '2025-06-15', inspector: '张工', address: '城北工业大道1-50号' },
  { id: 2, name: '阀门巡检-高新区', facility: '阀门井', status: 'in_progress', planDate: '2025-06-15', inspector: '李工', address: '高新区创新路' },
  { id: 3, name: '水表巡检-老城区', facility: '水表', status: 'completed', planDate: '2025-06-14', inspector: '王工', address: '老城区中山路' },
  { id: 4, name: '泵站巡检-东区', facility: '泵站', status: 'pending', planDate: '2025-06-16', inspector: '赵工', address: '东区滨河路88号' },
  { id: 5, name: '消防栓巡检-商业区', facility: '消防栓', status: 'in_progress', planDate: '2025-06-15', inspector: '孙工', address: '商业区解放路' },
]

/** 状态映射 */
const statusMap: Record<string, string> = {
  pending: '待巡检',
  in_progress: '巡检中',
  completed: '已完成',
}

const statusColorMap: Record<string, 'primary' | 'success' | 'warning' | 'danger'> = {
  pending: 'warning',
  in_progress: 'primary',
  completed: 'success',
}

/** 筛选后的任务列表 */
const filteredTasks = computed(() => {
  let list = mockTasks
  // 搜索筛选
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(t =>
      t.name.toLowerCase().includes(kw) ||
      t.facility.toLowerCase().includes(kw) ||
      t.address.toLowerCase().includes(kw)
    )
  }
  // 状态筛选
  if (activeTab.value === 1) {
    list = list.filter(t => t.status === 'pending')
  } else if (activeTab.value === 2) {
    list = list.filter(t => t.status === 'in_progress')
  } else if (activeTab.value === 3) {
    list = list.filter(t => t.status === 'completed')
  }
  return list
})

/** 跳转到任务详情 */
function goDetail(id: number) {
  router.push(`/inspection/detail?id=${id}`)
}
</script>

<template>
  <div class="inspection-page">
    <van-nav-bar title="巡检任务" left-arrow fixed placeholder @click-left="router.back()" />

    <van-search v-model="searchText" placeholder="搜索任务名称、设施、地址" shape="round" />

    <van-tabs v-model:active="activeTab" sticky>
      <van-tab title="全部" />
      <van-tab title="待巡检" />
      <van-tab title="巡检中" />
      <van-tab title="已完成" />
    </van-tabs>

    <div class="task-list">
      <van-empty v-if="filteredTasks.length === 0" description="暂无巡检任务" />
      <van-card
        v-for="task in filteredTasks"
        :key="task.id"
        :title="task.name"
        :desc="`设施: ${task.facility}`"
        @click="goDetail(task.id)"
      >
        <template #tags>
          <van-tag :type="statusColorMap[task.status]" size="medium">
            {{ statusMap[task.status] }}
          </van-tag>
        </template>
        <template #footer>
          <div class="task-meta">
            <span>计划日期: {{ task.planDate }}</span>
            <span>巡检人: {{ task.inspector }}</span>
          </div>
          <div class="task-address">{{ task.address }}</div>
        </template>
      </van-card>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.inspection-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;
  }
}

.task-list {
  padding: 0 8px;

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

.task-meta {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: var(--color-text-secondary);
}

.task-address {
  margin-top: 4px;
  font-size: 12px;
  color: var(--color-text-placeholder);
}
</style>
