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

    <view class="px-4 pt-3">
      <!-- 统计卡片 -->
      <view class="glass-card p-4">
        <view class="grid grid-cols-3 gap-3">
          <view class="stat-card">
            <view class="stat-bar"></view>
            <text class="stat-value">{{ availableCount }}</text>
            <text class="stat-label">可用</text>
          </view>
          <view class="stat-card">
            <view class="stat-bar"></view>
            <text class="stat-value stat-value-blue">{{ inUseCount }}</text>
            <text class="stat-label">使用中</text>
          </view>
          <view class="stat-card">
            <view class="stat-bar"></view>
            <text class="stat-value stat-value-gold">{{ maintenanceCount }}</text>
            <text class="stat-label">维修中</text>
          </view>
        </view>
      </view>

      <!-- 筛选 -->
      <view class="filter-bar mb-3">
        <view
          v-for="type in filterTypes"
          :key="type.value"
          class="filter-chip"
          :class="{ 'filter-chip-active': currentFilter === type.value }"
          @click="currentFilter = type.value"
        >
          {{ type.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="glass-card">
        <view
          v-for="vehicle in filteredVehicles"
          :key="vehicle.id"
          class="vehicle-row"
        >
          <view class="flex items-center justify-between">
            <view class="flex items-center flex-1 min-w-0 mr-2">
              <view class="icon-chip mr-3">
                <text class="uni-icons uniui-gear-filled chip-glyph"></text>
              </view>
              <view class="min-w-0">
                <text class="row-title truncate">{{ vehicle.plateNumber }}</text>
                <text class="row-sub truncate">{{ vehicle.type }}</text>
              </view>
            </view>
            <text class="status-tag flex-shrink-0" :class="getStatusTagClass(vehicle.status)">
              {{ getStatusText(vehicle.status) }}
            </text>
          </view>

          <view class="row-divider">
            <view class="grid grid-cols-2 gap-y-2 gap-x-4">
              <view class="meta-line">
                <text class="meta-label">尺寸</text>
                <text class="meta-value">{{ vehicle.size || '-' }}</text>
              </view>
              <view class="meta-line">
                <text class="meta-label">载重</text>
                <text class="meta-value">{{ vehicle.loadCapacity || '-' }}</text>
              </view>
              <view class="meta-line">
                <text class="meta-label">容积</text>
                <text class="meta-value">{{ vehicle.capacity || '-' }}</text>
              </view>
              <view class="meta-line">
                <text class="meta-label">驾驶员</text>
                <text class="meta-value">{{ vehicle.driver || '未分配' }}</text>
              </view>
            </view>
          </view>

          <view
            v-if="vehicle.driverPhone"
            class="phone-btn mt-3"
            hover-class="phone-btn-hover"
            :hover-start-time="0"
            :hover-stay-time="120"
            @click="callPhone(vehicle.driverPhone)"
          >
            <text class="uni-icons uniui-phone-filled phone-glyph mr-1"></text>
            <text class="phone-text">联系司机</text>
          </view>
          <view v-else class="phone-btn-disabled mt-3">
            <text class="phone-text-disabled">暂无司机</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'
import { useVehicleStore } from '../../stores/vehicle'

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

const filterTypes = [
  { 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)
})

const availableCount = computed(() => vehicleStore.vehicles.filter((v) => v.status === 'available').length)
const inUseCount = computed(() => vehicleStore.vehicles.filter((v) => v.status === 'in_use').length)
const maintenanceCount = computed(() => vehicleStore.vehicles.filter((v) => v.status === 'maintenance').length)

function getStatusText(status: string) {
  const map: Record<string, string> = {
    available: '可用',
    in_use: '使用中',
    maintenance: '维修中',
    offline: '离线',
  }
  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',
    offline: 'status-tag-gray',
  }
  return map[status] || 'status-tag-gray'
}

function callPhone(phone: string) {
  if (!phone) return
  uni.makePhoneCall({ phoneNumber: phone })
}

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

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

.glass-card {
  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: 24px;
  box-shadow: 0 18px 50px -12px rgba(54, 143, 111, 0.28);
  overflow: hidden;
  margin-bottom: 12px;
}

.stat-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 88px;
  padding: 14px 6px 12px;
  border-radius: 16px;
  background-color: rgba(164, 216, 152, 0.14);
  border: 1px solid rgba(164, 216, 152, 0.3);
  overflow: hidden;
}

.stat-bar {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 26px;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: linear-gradient(135deg, #368f6f 0%, #5ab8d0 100%);
}

.stat-value {
  font-size: 24px;
  font-weight: 800;
  line-height: 1.1;
  color: #368f6f;
}

.stat-value-blue {
  color: #2563eb;
}

.stat-value-gold {
  color: #b7791f;
}

.stat-label {
  margin-top: 6px;
  font-size: 12px;
  color: #6b7280;
}

/* 筛选条：默认白底灰边，激活态绿色 */
.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-row {
  padding: 14px 16px;
  border-top: 1px solid rgba(164, 216, 152, 0.18);
}

.vehicle-row:first-child {
  border-top: none;
}

.icon-chip {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(164, 216, 152, 0.2);
}

.chip-glyph {
  font-size: 20px;
  color: #368f6f;
}

.row-title {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
}

.row-sub {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  color: #9ca3af;
}

.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;
}

.row-divider {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid rgba(164, 216, 152, 0.18);
}

.meta-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.meta-label {
  font-size: 12px;
  color: #9ca3af;
}

.meta-value {
  font-size: 13px;
  color: #4b5563;
}

.phone-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 9px 0;
  border-radius: 12px;
  background: linear-gradient(135deg, #368f6f 0%, #4ba98a 100%);
}

.phone-btn-hover {
  opacity: 0.88;
}

.phone-glyph {
  font-size: 14px;
  color: #ffffff;
}

.phone-text {
  font-size: 14px;
  font-weight: 600;
  color: #ffffff;
}

.phone-btn-disabled {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 9px 0;
  border-radius: 12px;
  background-color: #f3f4f6;
}

.phone-text-disabled {
  font-size: 14px;
  color: #9ca3af;
}

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