<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">
    <!-- NavBar -->
    <van-nav-bar
      title="巡检任务"
      left-arrow
      fixed
      placeholder
      @click-left="router.back()"
    />

    <!-- Search -->
    <div class="search-wrapper">
      <van-search
        v-model="searchText"
        placeholder="搜索任务名称、设施、地址"
        shape="round"
        background="transparent"
      />
    </div>

    <!-- Tabs -->
    <van-tabs
      v-model:active="activeTab"
      sticky
      :duration="0.3"
      swipeable
      color="#1E74FF"
      title-active-color="#1E74FF"
      title-inactive-color="#646566"
      line-width="20"
      line-height="3"
    >
      <van-tab title="全部" />
      <van-tab title="待巡检" />
      <van-tab title="巡检中" />
      <van-tab title="已完成" />
    </van-tabs>

    <!-- Task List -->
    <div class="task-list">
      <van-empty
        v-if="filteredTasks.length === 0"
        image="search"
        description="暂无巡检任务"
      />
      <div
        v-for="task in filteredTasks"
        :key="task.id"
        class="task-card"
        @click="goDetail(task.id)"
      >
        <div class="task-card__header">
          <h4 class="task-card__title">{{ task.name }}</h4>
          <van-tag :type="statusColorMap[task.status]" size="medium" round>
            {{ statusMap[task.status] }}
          </van-tag>
        </div>
        <div class="task-card__body">
          <div class="task-card__info">
            <span class="info-item">
              <van-icon name="cluster-o" size="14" />
              {{ task.facility }}
            </span>
            <span class="info-item">
              <van-icon name="calendar-o" size="14" />
              {{ task.planDate }}
            </span>
          </div>
          <div class="task-card__info">
            <span class="info-item">
              <van-icon name="user-o" size="14" />
              {{ task.inspector }}
            </span>
          </div>
        </div>
        <div class="task-card__footer">
          <van-icon name="location-o" size="12" color="#969799" />
          <span class="address-text">{{ task.address }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

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

  // NavBar — brand blue bg, white text
  :deep(.van-nav-bar) {
    background: #1E74FF;
  }
  :deep(.van-nav-bar__title) {
    color: #fff;
    font-size: 18px;
    font-weight: 600;
  }
  :deep(.van-nav-bar__text),
  :deep(.van-nav-bar__arrow) {
    color: #fff;
  }
  :deep(.van-nav-bar .van-icon) {
    color: #fff;
  }
}

// Search — rounded, white bg
.search-wrapper {
  padding: 8px 12px 0;
  background: #F4F7F8;

  :deep(.van-search__content) {
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }
}

// Tabs — brand blue indicator
:deep(.van-tabs) {
  background: #fff;
}

:deep(.van-tabs__wrap) {
  padding: 0 8px;
}

:deep(.van-tab) {
  font-size: 14px;
}

// Task List
.task-list {
  padding: 8px 12px 16px;
}

.task-card {
  background: #fff;
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: box-shadow 0.15s ease;

  &:active {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }
}

.task-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.task-card__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-primary);
  flex: 1;
  margin-right: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-card__body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.task-card__info {
  display: flex;
  gap: 16px;
  font-size: 13px;
  color: var(--color-text-regular);
}

.info-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.task-card__footer {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-top: 8px;
  border-top: 1px solid var(--color-border-light);
}

.address-text {
  font-size: 12px;
  color: var(--color-text-placeholder);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
