<script setup lang="ts">
/**
 * 排水户问题列表页
 *
 * 展示排水户相关问题列表，支持搜索和状态筛选。
 */
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const pshId = (route.params.id || route.query.id) as string

/** 搜索关键词 */
const searchText = ref('')

/** 当前激活的 Tab */
const activeTab = ref(0)

/** 模拟问题数据 */
const mockProblems = [
  { id: 1, pshName: '华丰食品厂', title: '排水管道堵塞', severity: 'high', status: 'pending', reportTime: '2025-06-15 09:30', reporter: '张工' },
  { id: 2, pshName: '阳光花园小区', title: '化粪池溢出', severity: 'medium', status: 'processing', reportTime: '2025-06-15 11:00', reporter: '李工' },
  { id: 3, pshName: '万达商业广场', title: '油脂分离器故障', severity: 'low', status: 'resolved', reportTime: '2025-06-14 08:00', reporter: '王工' },
  { id: 4, pshName: '东风化工厂', title: '污水超标排放', severity: 'critical', status: 'pending', reportTime: '2025-06-16 10:00', reporter: '赵工' },
  { id: 5, pshName: '翠湖小区', title: '雨污混接', severity: 'medium', status: 'processing', reportTime: '2025-06-15 14:30', reporter: '孙工' },
]

/** 状态映射 */
const statusMap: Record<string, string> = {
  pending: '待处理',
  processing: '处理中',
  resolved: '已解决',
}

const statusColorMap: Record<string, 'primary' | 'success' | 'warning' | 'danger'> = {
  pending: 'warning',
  processing: 'primary',
  resolved: 'success',
}

/** 严重程度映射 */
const severityMap: Record<string, string> = {
  low: '低',
  medium: '中',
  high: '高',
  critical: '紧急',
}

const severityColorMap: Record<string, string> = {
  low: '#07c160',
  medium: '#ff976a',
  high: '#ee0a24',
  critical: '#ee0a24',
}

/** 筛选后的列表 */
const filteredProblems = computed(() => {
  let list = mockProblems
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(p =>
      p.title.toLowerCase().includes(kw) ||
      p.pshName.toLowerCase().includes(kw) ||
      p.reporter.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value === 1) list = list.filter(p => p.status === 'pending')
  else if (activeTab.value === 2) list = list.filter(p => p.status === 'processing')
  else if (activeTab.value === 3) list = list.filter(p => p.status === 'resolved')
  return list
})

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

<template>
  <div class="problem-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="card-list">
      <van-empty v-if="filteredProblems.length === 0" description="暂无问题" />
      <van-card
        v-for="item in filteredProblems"
        :key="item.id"
        :title="item.title"
        :desc="`排水户: ${item.pshName}`"
        @click="goDetail(item.id)"
      >
        <template #tags>
          <van-tag :type="statusColorMap[item.status]" size="medium">
            {{ statusMap[item.status] }}
          </van-tag>
          <van-tag :color="severityColorMap[item.severity]" size="medium" text-color="#fff" plain>
            {{ severityMap[item.severity] }}
          </van-tag>
        </template>
        <template #footer>
          <div class="card-meta">
            <span>上报人: {{ item.reporter }}</span>
            <span>{{ item.reportTime }}</span>
          </div>
        </template>
      </van-card>
    </div>
  </div>
</template>

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

.card-list {
  padding: 0 8px;

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

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

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