<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 mockCheckRecords = [
  { id: 1, project: '城北供水管网养护', inspector: '张检查员', result: 'qualified', score: 95, date: '2025-06-15', content: '管道冲洗、阀门润滑、井盖检查' },
  { id: 2, project: '高新区阀门养护', inspector: '李检查员', result: 'unqualified', score: 58, date: '2025-06-14', content: '部分阀门操作卡涩，需重新养护' },
  { id: 3, project: '老城区水表更换', inspector: '王检查员', result: 'qualified', score: 88, date: '2025-06-13', content: '水表安装规范，计量准确' },
  { id: 4, project: '商业区消防栓检查', inspector: '赵检查员', result: 'qualified', score: 92, date: '2025-06-12', content: '消防栓出水正常，压力达标' },
  { id: 5, project: '东区泵站养护', inspector: '孙检查员', result: 'unqualified', score: 62, date: '2025-06-11', content: '泵站噪音超标，需维修后复查' },
]

/** 结果映射 */
const resultMap: Record<string, string> = {
  qualified: '合格',
  unqualified: '不合格',
}

const resultColorMap: Record<string, 'success' | 'danger'> = {
  qualified: 'success',
  unqualified: 'danger',
}

/** 评分颜色 */
function scoreColor(score: number): string {
  if (score >= 90) return '#4CAF50'
  if (score >= 70) return '#FF9800'
  return '#F44336'
}

/** 筛选后的记录列表 */
const filteredRecords = computed(() => {
  let list = mockCheckRecords
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(r =>
      r.project.toLowerCase().includes(kw) ||
      r.inspector.toLowerCase().includes(kw) ||
      r.content.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value === 1) list = list.filter(r => r.result === 'qualified')
  else if (activeTab.value === 2) list = list.filter(r => r.result === 'unqualified')
  return list
})

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

<template>
  <div class="check-records-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-tabs>

    <div class="record-list">
      <van-empty v-if="filteredRecords.length === 0" description="暂无检查记录" />
      <van-card
        v-for="record in filteredRecords"
        :key="record.id"
        :title="record.project"
        :desc="`检查员: ${record.inspector}`"
        @click="goDetail(record.id)"
      >
        <template #tags>
          <van-tag :type="resultColorMap[record.result]" size="medium">
            {{ resultMap[record.result] }}
          </van-tag>
          <van-tag size="medium" :color="scoreColor(record.score)" text-color="#fff">
            {{ record.score }} 分
          </van-tag>
        </template>
        <template #footer>
          <div class="record-meta">
            <span>{{ record.date }}</span>
            <span>{{ record.content }}</span>
          </div>
        </template>
      </van-card>
    </div>
  </div>
</template>

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

.record-list {
  padding: 0 8px;

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

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

.record-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 12px;
  color: var(--color-text-secondary);
}
</style>
