<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 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 scoreClass(score: number): string {
  if (score >= 90) return 'score-green'
  if (score >= 70) return 'score-orange'
  return 'score-red'
}

/** 筛选后的记录列表 */
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">
    <!-- 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>

    <!-- Record List -->
    <div class="record-list">
      <van-empty
        v-if="filteredRecords.length === 0"
        image="search"
        description="暂无检查记录"
      />
      <div
        v-for="record in filteredRecords"
        :key="record.id"
        class="record-card"
        @click="goDetail(record.id)"
      >
        <!-- Card Header -->
        <div class="record-card__header">
          <h4 class="record-card__title">{{ record.project }}</h4>
          <span :class="['score-badge', scoreClass(record.score)]">{{ record.score }}分</span>
        </div>

        <!-- Card Body -->
        <div class="record-card__body">
          <div class="record-info">
            <span class="info-item">
              <van-icon name="user-o" size="13" />
              {{ record.inspector }}
            </span>
            <span class="info-item">
              <van-icon name="clock-o" size="13" />
              {{ record.date }}
            </span>
          </div>
        </div>

        <!-- Card Footer -->
        <div class="record-card__footer">
          <span class="content-text">{{ record.content }}</span>
          <van-tag :type="resultColorMap[record.result]" size="medium" round class="result-badge">
            {{ resultMap[record.result] }}
          </van-tag>
        </div>
      </div>
    </div>
  </div>
</template>

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

// Record List
.record-list { padding: 8px 12px 16px; }

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

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

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

.score-badge {
  font-size: 13px;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 12px;
  flex-shrink: 0;
  color: #fff;
  &.score-green { background: #4CAF50; }
  &.score-orange { background: #FF9800; }
  &.score-red { background: #F44336; }
}

.record-card__body {
  margin-bottom: 8px;
}

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

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

.record-card__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  border-top: 1px solid var(--color-border-light);
}

.content-text {
  font-size: 12px;
  color: var(--color-text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  margin-right: 8px;
}

.result-badge {
  flex-shrink: 0;
}
</style>
