<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 statusTagType: 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 projects = [
  { id: 1, name: '城北雨水管网改造工程', no: 'XM-2025-001', company: '中建三局', progress: 75, status: 'building', manager: '赵工' },
  { id: 2, name: '东风路排水泵站新建项目', no: 'XM-2025-002', company: '水务工程公司', progress: 30, status: 'building', manager: '钱工' },
  { id: 3, name: '中山河河道整治工程', no: 'XM-2025-003', company: '市政建设集团', progress: 90, status: 'building', manager: '孙工' },
  { id: 4, name: '开发区污水管网铺设', no: 'XM-2025-004', company: '中交一公局', progress: 0, status: 'paused', manager: '李工' },
  { id: 5, name: '老城区雨污分流改造', no: 'XM-2025-005', company: '中铁十二局', progress: 100, status: 'completed', manager: '周工' },
  { id: 6, name: '南湖片区海绵城市试点', no: 'XM-2025-006', company: '葛洲坝集团', progress: 55, status: 'building', manager: '吴工' },
]

const filteredList = computed(() => {
  let list = projects
  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(`/projectManagementDetail/${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="statusTagType[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);
  }
}

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

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