<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: '管网巡检-城北片区', inspector: '张工', startTime: '2025-06-15 09:00', endTime: '2025-06-15 11:30', result: 'normal', problemCount: 0 },
  { id: 2, taskName: '阀门巡检-高新区', inspector: '李工', startTime: '2025-06-15 14:00', endTime: '2025-06-15 16:00', result: 'abnormal', problemCount: 3 },
  { id: 3, taskName: '水表巡检-老城区', inspector: '王工', startTime: '2025-06-14 08:30', endTime: '2025-06-14 10:00', result: 'normal', problemCount: 0 },
  { id: 4, taskName: '消防栓巡检-商业区', inspector: '孙工', startTime: '2025-06-13 15:00', endTime: '2025-06-13 17:00', result: 'abnormal', problemCount: 1 },
  { id: 5, taskName: '泵站巡检-东区', inspector: '赵工', startTime: '2025-06-10 09:00', endTime: '2025-06-10 11:00', result: 'normal', problemCount: 0 },
]

/** 结果映射 */
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.inspector.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(`/inspectionRecords/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.inspector}`"
        @click="goDetail(record.id)"
      >
        <template #tags>
          <van-tag :type="resultColorMap[record.result]" size="medium">
            {{ resultMap[record.result] }}
          </van-tag>
          <van-tag v-if="record.problemCount > 0" type="danger" size="medium" plain>
            问题 {{ record.problemCount }}
          </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>
