<script setup lang="ts">
/**
 * 工程项目列表
 *
 * 展示在建工程项目列表，支持按状态筛选和搜索，
 * 显示项目进度信息。
 */
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const searchText = ref('')
const activeTab = ref(0)

/** 状态映射 */
const statusMap: Record<string, string> = {
  building: '在建',
  paused: '暂停',
  completed: '竣工',
}

const statusColorMap: Record<string, 'primary' | 'warning' | 'success'> = {
  building: 'primary',
  paused: 'warning',
  completed: 'success',
}

/** 进度颜色 */
function progressColor(pct: number): string {
  if (pct >= 80) return '#07c160'
  if (pct >= 40) return '#1989fa'
  return '#ff976a'
}

/** 模拟项目数据 */
const mockProjects = [
  { id: 1, name: '城北雨水管网改造工程', no: 'XM-2025-001', company: '中建三局', progress: 75, status: 'building', manager: '赵工', startDate: '2025-03-01' },
  { id: 2, name: '东风路排水泵站新建项目', no: 'XM-2025-002', company: '水务工程公司', progress: 30, status: 'building', manager: '钱工', startDate: '2025-04-15' },
  { id: 3, name: '中山河河道整治工程', no: 'XM-2025-003', company: '市政建设集团', progress: 90, status: 'building', manager: '孙工', startDate: '2025-01-10' },
  { id: 4, name: '开发区污水管网铺设', no: 'XM-2025-004', company: '中交一公局', progress: 0, status: 'paused', manager: '李工', startDate: '2025-05-01' },
  { id: 5, name: '老城区雨污分流改造', no: 'XM-2025-005', company: '中铁十二局', progress: 100, status: 'completed', manager: '周工', startDate: '2024-09-01' },
]

const filteredList = computed(() => {
  let list = mockProjects
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(p =>
      p.name.toLowerCase().includes(kw) ||
      p.no.toLowerCase().includes(kw) ||
      p.company.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value === 1) list = list.filter(p => p.status === 'building')
  else if (activeTab.value === 2) list = list.filter(p => p.status === 'paused')
  else if (activeTab.value === 3) list = list.filter(p => p.status === 'completed')
  return list
})

function goDetail(id: number) {
  router.push(`/constructionDetail/${id}`)
}
</script>

<template>
  <div class="page-container">
    <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="card-list">
      <van-empty v-if="filteredList.length === 0" description="暂无项目" />
      <van-card
        v-for="item in filteredList"
        :key="item.id"
        :title="item.name"
        :desc="`施工单位: ${item.company}`"
        @click="goDetail(item.id)"
      >
        <template #tags>
          <van-tag :type="statusColorMap[item.status]" size="medium">
            {{ statusMap[item.status] }}
          </van-tag>
        </template>
        <template #footer>
          <div class="progress-wrap">
            <van-progress
              :percentage="item.progress"
              :color="progressColor(item.progress)"
              :pivot-text="`${item.progress}%`"
            />
          </div>
          <div class="card-meta">
            <span>{{ item.no }}</span>
            <span>负责人: {{ item.manager }}</span>
          </div>
        </template>
      </van-card>
    </div>
  </div>
</template>

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

.card-list {
  padding: 0 8px;

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

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

.progress-wrap {
  margin: 8px 0;
}

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