<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 mockRecords = [
  { id: 1, taskName: '管网养护-城北片区', maintainer: '刘工', startTime: '2025-06-15 09:00', endTime: '2025-06-15 11:30', result: 'normal', score: 95 },
  { id: 2, taskName: '阀门养护-高新区', maintainer: '陈工', startTime: '2025-06-15 14:00', endTime: '2025-06-15 16:00', result: 'abnormal', score: 72 },
  { id: 3, taskName: '水表养护-老城区', maintainer: '杨工', startTime: '2025-06-14 08:30', endTime: '2025-06-14 10:00', result: 'normal', score: 88 },
  { id: 4, taskName: '消防栓养护-商业区', maintainer: '周工', startTime: '2025-06-13 15:00', endTime: '2025-06-13 17:00', result: 'abnormal', score: 65 },
  { id: 5, taskName: '泵站养护-东区', maintainer: '吴工', startTime: '2025-06-10 09:00', endTime: '2025-06-10 11:00', result: 'normal', score: 91 },
]

/** 结果映射 */
const resultMap: Record<string, string> = {
  normal: '正常',
  abnormal: '异常',
}

const resultColorMap: Record<string, 'success' | 'danger'> = {
  normal: 'success',
  abnormal: 'danger',
}

/** 日期筛选工具函数 */
function getToday() {
  const d = new Date()
  return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
}

const today = getToday()

function isThisWeek(dateStr: string) {
  const d = new Date(dateStr)
  const now = new Date()
  const startOfWeek = new Date(now)
  startOfWeek.setDate(now.getDate() - now.getDay())
  startOfWeek.setHours(0, 0, 0, 0)
  return d >= startOfWeek
}

function isThisMonth(dateStr: string) {
  const d = new Date(dateStr)
  const now = new Date()
  return d.getFullYear() === now.getFullYear() && d.getMonth() === now.getMonth()
}

/** 筛选后的记录列表 */
const filteredRecords = computed(() => {
  let list = mockRecords
  // 搜索筛选
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(r =>
      r.taskName.toLowerCase().includes(kw) ||
      r.maintainer.toLowerCase().includes(kw)
    )
  }
  // 日期筛选
  if (activeTab.value === 1) {
    list = list.filter(r => r.startTime.startsWith(today))
  } else if (activeTab.value === 2) {
    list = list.filter(r => isThisWeek(r.startTime))
  } else if (activeTab.value === 3) {
    list = list.filter(r => isThisMonth(r.startTime))
  }
  return list
})

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

<template>
  <div class="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-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.taskName"
        :desc="`养护人: ${record.maintainer}`"
        @click="goDetail(record.id)"
      >
        <template #tags>
          <van-tag :type="resultColorMap[record.result]" size="medium">
            {{ resultMap[record.result] }}
          </van-tag>
          <van-tag type="primary" size="medium" plain>
            评分 {{ record.score }}
          </van-tag>
        </template>
        <template #footer>
          <div class="record-meta">
            <span>开始: {{ record.startTime }}</span>
            <span>结束: {{ record.endTime }}</span>
          </div>
        </template>
      </van-card>
    </div>
  </div>
</template>

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