<script setup lang="ts">
/**
 * 养护检查列表页
 *
 * 支持搜索、状态筛选（全部/待检查/已检查），
 * 点击卡片查看检查详情并评分。
 * Design: #1E74FF NavBar, #F4F7F8 bg, white cards border-radius 10px, styled tabs.
 */
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

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

/** 当前激活的 Tab（状态筛选） */
const activeTab = ref(0)

/** 模拟检查数据 */
const mockChecks = [
  { id: 1, name: '管网养护检查-城北片区', maintainer: '刘工', checkDate: '2025-06-15', status: 'pending', address: '城北工业大道1-50号', facility: '供水管网', lastScore: 95 },
  { id: 2, name: '阀门养护检查-高新区', maintainer: '陈工', checkDate: '2025-06-15', status: 'checked', address: '高新区创新路', facility: '阀门井', score: 88, checkedBy: '主管张', checkTime: '2025-06-15 17:00' },
  { id: 3, name: '水表养护检查-老城区', maintainer: '杨工', checkDate: '2025-06-14', status: 'pending', address: '老城区中山路', facility: '水表', lastScore: 91 },
  { id: 4, name: '消防栓养护检查-商业区', maintainer: '周工', checkDate: '2025-06-13', status: 'checked', address: '商业区解放路', facility: '消防栓', score: 76, checkedBy: '主管李', checkTime: '2025-06-13 18:00' },
  { id: 5, name: '泵站养护检查-东区', maintainer: '吴工', checkDate: '2025-06-10', status: 'checked', address: '东区滨河路88号', facility: '泵站', score: 93, checkedBy: '主管王', checkTime: '2025-06-10 16:30' },
]

/** 状态映射 */
const statusMap: Record<string, string> = { pending: '待检查', checked: '已检查' }
const statusColorMap: Record<string, 'warning' | 'success'> = { pending: 'warning', checked: 'success' }

/** 筛选后的检查列表 */
const filteredChecks = computed(() => {
  let list = mockChecks
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(c =>
      c.name.toLowerCase().includes(kw) ||
      c.maintainer.toLowerCase().includes(kw) ||
      c.facility.toLowerCase().includes(kw) ||
      c.address.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value === 1) list = list.filter(c => c.status === 'pending')
  else if (activeTab.value === 2) list = list.filter(c => c.status === 'checked')
  return list
})

/** 跳转到检查详情 → maintenanceDetail (养护详情/检查详情共用) */
function goDetail(id: number) {
  router.push(`/maintenance/${id}`)
}
</script>

<template>
  <div class="check-page">
    <!-- NavBar — brand blue -->
    <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-tabs>

    <!-- Check List -->
    <div class="check-list">
      <van-empty
        v-if="filteredChecks.length === 0"
        image="search"
        description="暂无检查任务"
      />
      <div
        v-for="check in filteredChecks"
        :key="check.id"
        class="check-card"
        @click="goDetail(check.id)"
      >
        <!-- Card Header -->
        <div class="check-card__header">
          <h4 class="check-card__title">{{ check.name }}</h4>
          <van-tag :type="statusColorMap[check.status]" size="medium" round>
            {{ statusMap[check.status] }}
          </van-tag>
        </div>

        <!-- Card Body -->
        <div class="check-card__body">
          <div class="check-info">
            <span class="info-item">
              <van-icon name="cluster-o" size="13" />
              {{ check.facility }}
            </span>
            <span class="info-item">
              <van-icon name="user-o" size="13" />
              {{ check.maintainer }}
            </span>
          </div>
          <div class="check-info">
            <span class="info-item">
              <van-icon name="clock-o" size="13" />
              {{ check.checkDate }}
            </span>
            <span v-if="check.score" class="info-item score-highlight">
              <van-icon name="star-o" size="13" />
              评分 {{ check.score }}
            </span>
            <span v-else-if="check.lastScore" class="info-item">
              <van-icon name="star-o" size="13" />
              上次 {{ check.lastScore }}分
            </span>
          </div>
        </div>

        <!-- Card Footer -->
        <div class="check-card__footer">
          <span class="address-text">
            <van-icon name="location-o" size="12" color="#969799" />
            {{ check.address }}
          </span>
          <span v-if="check.checkedBy" class="meta-text">
            检查人: {{ check.checkedBy }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

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

  :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
.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
:deep(.van-tabs) { background: #fff; }
:deep(.van-tabs__wrap) { padding: 0 8px; }
:deep(.van-tab) { font-size: 14px; }

// Check List
.check-list { padding: 8px 12px 16px; }

.check-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); }
}

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

.check-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;
}

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

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

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

.score-highlight {
  color: #1E74FF;
  font-weight: 500;
}

.check-card__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  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;
  flex: 1;
  margin-right: 8px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.meta-text {
  font-size: 11px;
  color: var(--color-text-placeholder);
  flex-shrink: 0;
}
</style>
