<template>
  <view class="sub-page">
    <TopBar title="车辆列表" show-back />

    <view class="px-4 pt-3">
      <!-- 筛选 -->
      <view class="filter-bar mb-3">
        <view
          v-for="status in filterStatuses"
          :key="status.value"
          class="filter-chip"
          :class="{ 'filter-chip-active': currentFilter === status.value }"
          @click="currentFilter = status.value"
        >
          {{ status.label }}
        </view>
      </view>

      <view v-if="loading" class="py-10 text-center">
        <text class="empty-text">加载中...</text>
      </view>

      <view v-else-if="filteredVehicles.length === 0">
        <EmptyState message="暂无车辆" />
      </view>

      <view v-else class="gap-y-3">
        <view
          v-for="vehicle in filteredVehicles"
          :key="vehicle.id"
          class="vehicle-card"
          hover-class="vehicle-card-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="showVehicleDetail(vehicle)"
        >
          <view class="flex-1 min-w-0 mr-3">
            <text class="plate-number truncate">{{ vehicle.plateNumber }}</text>
            <text class="vehicle-type truncate">{{ vehicle.type || '-' }}</text>
          </view>
          <text class="status-tag flex-shrink-0" :class="getStatusTagClass(vehicle.status)">
            {{ getStatusText(vehicle.status) }}
          </text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { onPullDownRefresh } from '@dcloudio/uni-app'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'
import { useVehicleStore } from '../../stores/vehicle'
import type { Vehicle } from '../../types'

const vehicleStore = useVehicleStore()
const loading = ref(false)
const currentFilter = ref('all')

const filterStatuses = [
  { value: 'all', label: '全部' },
  { value: 'available', label: '可用' },
  { value: 'in_use', label: '使用中' },
  { value: 'maintenance', label: '维修中' },
]

const filteredVehicles = computed(() => {
  if (currentFilter.value === 'all') return vehicleStore.vehicles
  return vehicleStore.vehicles.filter((v) => v.status === currentFilter.value)
})

function getStatusText(status: string) {
  const map: Record<string, string> = {
    available: '可用',
    in_use: '使用中',
    maintenance: '维修中',
  }
  return map[status] || status
}

function getStatusTagClass(status: string) {
  const map: Record<string, string> = {
    available: 'status-tag-green',
    in_use: 'status-tag-blue',
    maintenance: 'status-tag-gold',
  }
  return map[status] || 'status-tag-gray'
}

function showVehicleDetail(vehicle: Vehicle) {
  const content = [
    `车牌号: ${vehicle.plateNumber}`,
    `类型: ${vehicle.type}`,
    `尺寸: ${vehicle.size || '-'}`,
    `载重: ${vehicle.loadCapacity || '-'}`,
    `容积: ${vehicle.capacity || '-'}`,
    `驾驶员: ${vehicle.driver || '未分配'}`,
    `驾驶员电话: ${vehicle.driverPhone || '-'}`,
    `购车日期: ${vehicle.purchaseDate || '-'}`,
    `里程: ${vehicle.mileage != null ? `${vehicle.mileage} km` : '-'}`,
    `状态: ${getStatusText(vehicle.status)}`,
  ].join('\n')

  uni.showModal({
    title: vehicle.plateNumber,
    content,
    showCancel: false,
  })
}

onMounted(async () => {
  loading.value = true
  await vehicleStore.fetchVehicles()
  loading.value = false
})

onPullDownRefresh(async () => {
  await vehicleStore.fetchVehicles()
  uni.stopPullDownRefresh()
})
</script>

<style scoped>
.sub-page {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  background-color: #f6fbf9;
  padding-bottom: 24px;
}

.gap-y-3 {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 筛选条：默认白底灰边，激活态绿色 */
.filter-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.filter-chip {
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 13px;
  color: #6b7280;
  background-color: #ffffff;
  border: 1px solid #e5e7eb;
  flex-shrink: 0;
}

.filter-chip-active {
  color: #ffffff;
  font-weight: 600;
  background: linear-gradient(135deg, #368f6f 0%, #4ba98a 100%);
  border-color: transparent;
}

.vehicle-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(164, 216, 152, 0.35);
  border-radius: 16px;
  box-shadow: 0 14px 40px -10px rgba(54, 143, 111, 0.22);
}

.plate-number {
  display: block;
  font-size: 18px;
  font-weight: 700;
  color: #1f2937;
}

.vehicle-type {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  color: #6b7280;
}

.vehicle-card-hover {
  background-color: rgba(164, 216, 152, 0.1);
}

.status-tag {
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.status-tag-green {
  color: #368f6f;
  background-color: rgba(164, 216, 152, 0.24);
}

.status-tag-blue {
  color: #2563eb;
  background-color: rgba(37, 99, 235, 0.1);
}

.status-tag-gold {
  color: #b7791f;
  background-color: rgba(251, 211, 141, 0.25);
}

.status-tag-gray {
  color: #6b7280;
  background-color: #f3f4f6;
}

.empty-text {
  font-size: 13px;
  color: #9ca3af;
}
</style>
