import { ref, computed } from 'vue';
import car1 from '../assets/images/car1.png';
import car2 from '../assets/images/car2.png';
import car3 from '../assets/images/car3.png';
import car4 from '../assets/images/car4.png';

export function useVehicle() {
  // 车辆数据
  const vehicleData = ref([
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    },
    {
      id: 1,
      carId: 'QN001',
      type: '驱动车',
      brand: '特斯拉',
      organization: '物流管理公司',
      routeStatus: '任务中',
      status: '在线',
      chargeStatus: '停车充电',
      voltage: '360V',
      current: '200A',
      soc: '90%',
      lastTime: '2024-08-17 15:48:30',
      batteryLevel: 'T3高位',
      location: '张三',
      contactPhone: '12345678920',
      battery: 85,
      runningStatus: '任务中'
    }
    
  ]);

  // 过滤条件
  const filters = ref({
    searchText: '',
    vehicleType: '',
    vehicleStatus: ''
  });

  // 统计数据
  const vehicleStats = computed(() => [
    { 
      title: '车辆总数', 
      value: vehicleData.value.length.toString().padStart(5, '0'), 
      icon: car1, 
      change: 30 
    },
    { 
      title: '在线', 
      value: vehicleData.value.filter(v => v.status === '在线').length.toString().padStart(5, '0'), 
      icon: car2, 
      change: 30 
    },
    { 
      title: '离线', 
      value: vehicleData.value.filter(v => v.status === '离线').length.toString().padStart(5, '0'), 
      icon: car3, 
      change: 30 
    },
    { 
      title: '故障', 
      value: vehicleData.value.filter(v => v.status === '故障').length.toString().padStart(5, '0'), 
      icon: car4, 
      change: 30 
    }
  ]);

  // 筛选后的数据
  const filteredVehicleData = computed(() => {
    let filtered = [...vehicleData.value];
    
    if (filters.value.searchText) {
      const searchText = filters.value.searchText.toLowerCase();
      filtered = filtered.filter(item => 
        item.id?.toString().toLowerCase().includes(searchText) ||
        item.carId?.toLowerCase().includes(searchText) ||
        item.type?.toLowerCase().includes(searchText) ||
        item.organization?.toLowerCase().includes(searchText)
      );
    }
    
    if (filters.value.vehicleStatus) {
      filtered = filtered.filter(item => item.status === filters.value.vehicleStatus);
    }
    
    if (filters.value.vehicleType) {
      filtered = filtered.filter(item => item.type === filters.value.vehicleType);
    }
    
    return filtered;
  });

  // 获取行驶状态对应的类型
  function getRouteStatusType(status) {
    switch (status) {
      case '任务中':
        return 'success';
      case '待命中':
        return '';
      case '充电中':
        return 'warning';
      case '等待中':
        return 'info';
      default:
        return '';
    }
  }

  // 更新过滤条件
  function updateFilters(newFilters) {
    filters.value = { ...filters.value, ...newFilters };
  }

  // 重置过滤条件
  function resetFilters() {
    filters.value = {
      searchText: '',
      vehicleType: '',
      vehicleStatus: ''
    };
  }

  // 导出数据
  function exportData() {
    // 实现导出逻辑
    console.log('导出数据', filteredVehicleData.value);
  }

  return {
    vehicleData,
    vehicleStats,
    filters,
    filteredVehicleData,
    getRouteStatusType,
    updateFilters,
    resetFilters,
    exportData
  };
} 