<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 mockOrders = [
  { id: 1, title: '管道渗漏-城北片区', facility: '供水管网', status: 'pending', severity: '严重', reporter: '张工', reportTime: '2025-06-15 09:30', address: '城北工业大道12号' },
  { id: 2, title: '阀门损坏-高新区', facility: '阀门井', status: 'processing', severity: '一般', reporter: '李工', reportTime: '2025-06-15 11:00', address: '高新区创新路55号' },
  { id: 3, title: '水表故障-老城区', facility: '水表', status: 'resolved', severity: '轻微', reporter: '王工', reportTime: '2025-06-14 08:00', address: '老城区中山路88号' },
  { id: 4, title: '消防栓漏水-商业区', facility: '消防栓', status: 'pending', severity: '一般', reporter: '孙工', reportTime: '2025-06-16 10:00', address: '商业区解放路101号' },
  { id: 5, title: '泵站异常-东区', facility: '泵站', status: 'processing', severity: '严重', reporter: '赵工', reportTime: '2025-06-15 14:30', address: '东区滨河路88号' },
]

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

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

/** 严重程度映射 */
const severityColorMap: Record<string, 'danger' | 'warning' | 'primary'> = {
  '严重': 'danger',
  '一般': 'warning',
  '轻微': 'primary',
}

/** 筛选后的工单列表 */
const filteredOrders = computed(() => {
  let list = mockOrders
  if (searchText.value) {
    const kw = searchText.value.toLowerCase()
    list = list.filter(o =>
      o.title.toLowerCase().includes(kw) ||
      o.facility.toLowerCase().includes(kw) ||
      o.address.toLowerCase().includes(kw)
    )
  }
  if (activeTab.value === 1) list = list.filter(o => o.status === 'pending')
  else if (activeTab.value === 2) list = list.filter(o => o.status === 'processing')
  else if (activeTab.value === 3) list = list.filter(o => o.status === 'resolved')
  return list
})

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

<template>
  <div class="problem-page">
    <!-- NavBar -->
    <van-nav-bar
      title="问题工单"
      left-arrow
      fixed
      placeholder
      @click-left="router.back()"
    />

    <!-- Search -->
    <div class="search-wrapper">
      <van-search
        v-model="searchText"
        placeholder="搜索工单标题、设施、地址"
        shape="round"
        background="transparent"
      />
    </div>

    <!-- Tabs -->
    <van-tabs
      v-model:active="activeTab"
      sticky
      :duration="0.3"
      swipeable
      color="#1E74FF"
      title-active-color="#1E74FF"
      title-inactive-color="#646566"
      line-width="20"
      line-height="3"
    >
      <van-tab title="全部" />
      <van-tab title="待处理" />
      <van-tab title="处理中" />
      <van-tab title="已解决" />
    </van-tabs>

    <!-- Order List -->
    <div class="order-list">
      <van-empty
        v-if="filteredOrders.length === 0"
        image="search"
        description="暂无问题工单"
      />
      <div
        v-for="order in filteredOrders"
        :key="order.id"
        class="order-card"
        @click="goDetail(order.id)"
      >
        <!-- Card Header -->
        <div class="order-card__header">
          <h4 class="order-card__title">{{ order.title }}</h4>
          <van-tag
            :type="severityColorMap[order.severity]"
            size="medium"
            plain
            round
          >
            {{ order.severity }}
          </van-tag>
        </div>

        <!-- Card Body -->
        <div class="order-card__body">
          <div class="order-info">
            <span class="info-item">
              <van-icon name="cluster-o" size="13" />
              {{ order.facility }}
            </span>
            <span class="info-item">
              <van-icon name="user-o" size="13" />
              {{ order.reporter }}
            </span>
          </div>
          <div class="order-info">
            <span class="info-item">
              <van-icon name="clock-o" size="13" />
              {{ order.reportTime }}
            </span>
          </div>
        </div>

        <!-- Card Footer -->
        <div class="order-card__footer">
          <span class="address-text">
            <van-icon name="location-o" size="12" color="#969799" />
            {{ order.address }}
          </span>
          <van-tag
            :type="statusColorMap[order.status]"
            size="medium"
            round
            class="status-badge"
          >
            {{ statusMap[order.status] }}
          </van-tag>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.problem-page {
  min-height: 100vh;
  background: var(--color-bg-page);

  // NavBar — brand blue bg, white text
  :deep(.van-nav-bar) {
    background: #1E74FF;
  }
  :deep(.van-nav-bar__title) {
    color: #fff;
    font-size: 18px;
    font-weight: 600;
  }
  :deep(.van-nav-bar__text),
  :deep(.van-nav-bar__arrow) {
    color: #fff;
  }
  :deep(.van-nav-bar .van-icon) {
    color: #fff;
  }
}

// Search — rounded, white bg
.search-wrapper {
  padding: 8px 12px 0;
  background: #F4F7F8;

  :deep(.van-search__content) {
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }
}

// Tabs
:deep(.van-tabs) {
  background: #fff;
}

:deep(.van-tabs__wrap) {
  padding: 0 8px;
}

:deep(.van-tab) {
  font-size: 14px;
}

// Order List
.order-list {
  padding: 8px 12px 16px;
}

.order-card {
  background: #fff;
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: box-shadow 0.15s ease;

  &:active {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }
}

.order-card__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 10px;
}

.order-card__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-primary);
  flex: 1;
  margin-right: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.order-card__body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.order-info {
  display: flex;
  gap: 16px;
  font-size: 13px;
  color: var(--color-text-regular);
}

.info-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.order-card__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  border-top: 1px solid var(--color-border-light);
}

.address-text {
  font-size: 12px;
  color: var(--color-text-placeholder);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  margin-right: 8px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.status-badge {
  flex-shrink: 0;
}
</style>
