<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 mockSupervisors = [
  { id: 1, name: '张监督', company: '水务监理公司A', status: 'on_duty', phone: '13800001001', project: '城北供水改造工程', rating: 4.8 },
  { id: 2, name: '李监督', company: '市政监理公司B', status: 'off_duty', phone: '13800001002', project: '高新区管网新建', rating: 4.5 },
  { id: 3, name: '王监督', company: '环境监理公司C', status: 'on_duty', phone: '13800001003', project: '老城区雨污分流', rating: 4.9 },
  { id: 4, name: '赵监督', company: '水务监理公司A', status: 'on_duty', phone: '13800001004', project: '东区泵站升级', rating: 4.6 },
  { id: 5, name: '孙监督', company: '市政监理公司B', status: 'off_duty', phone: '13800001005', project: '商业区管道维护', rating: 4.3 },
]

/** 状态映射 */
const statusMap: Record<string, string> = {
  on_duty: '在岗',
  off_duty: '离岗',
}

const statusColorMap: Record<string, 'success' | 'danger'> = {
  on_duty: 'success',
  off_duty: 'danger',
}

/** 筛选后的列表 */
const filteredSupervisors = computed(() => {
  let list = mockSupervisors
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(s =>
      s.name.toLowerCase().includes(kw) ||
      s.company.toLowerCase().includes(kw) ||
      s.project.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value === 1) list = list.filter(s => s.status === 'on_duty')
  else if (activeTab.value === 2) list = list.filter(s => s.status === 'off_duty')
  return list
})

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

<template>
  <div class="supervisor-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="supervisor-list">
      <van-empty v-if="filteredSupervisors.length === 0" description="暂无监督人员" />
      <van-card
        v-for="supervisor in filteredSupervisors"
        :key="supervisor.id"
        :title="supervisor.name"
        :desc="supervisor.company"
        @click="goDetail(supervisor.id)"
      >
        <template #tags>
          <van-tag :type="statusColorMap[supervisor.status]" size="medium">
            {{ statusMap[supervisor.status] }}
          </van-tag>
          <van-tag type="primary" size="medium" plain>
            {{ supervisor.rating }} 分
          </van-tag>
        </template>
        <template #footer>
          <div class="supervisor-meta">
            <span>{{ supervisor.phone }}</span>
            <span>{{ supervisor.project }}</span>
          </div>
        </template>
      </van-card>
    </div>
  </div>
</template>

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

.supervisor-list {
  padding: 0 8px;

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

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

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