// Vehicle Dashboard JavaScript - Navegación por tarjetas class VehicleDashboard { constructor() { this.currentView = 'brands'; // brands, models, vehicles this.selectedBrand = null; this.selectedModel = null; this.allVehicles = []; this.filteredVehicles = []; this.stats = { brands: 0, models: 0, vehicles: 0 }; this.init(); } async init() { await this.loadStats(); await this.showBrands(); this.bindFilterEvents(); } async loadStats() { try { const [brandsRes, vehiclesRes] = await Promise.all([ fetch('/api/brands'), fetch('/api/vehicles') ]); if (brandsRes.ok && vehiclesRes.ok) { const brands = await brandsRes.json(); const vehicles = await vehiclesRes.json(); // Contar modelos únicos const uniqueModels = new Set(vehicles.map(v => `${v.brand}-${v.model}`)); this.stats.brands = brands.length; this.stats.models = uniqueModels.size; this.stats.vehicles = vehicles.length; document.getElementById('totalBrands').textContent = this.stats.brands; document.getElementById('totalModels').textContent = this.stats.models; document.getElementById('totalVehicles').textContent = this.stats.vehicles; } } catch (error) { console.error('Error loading stats:', error); } } updateBreadcrumb() { const breadcrumb = document.getElementById('breadcrumb'); let html = ''; if (this.currentView === 'brands') { html = ``; } else if (this.currentView === 'models') { html = ` `; } else if (this.currentView === 'vehicles') { html = ` `; } breadcrumb.innerHTML = html; } async showBrands() { this.currentView = 'brands'; this.selectedBrand = null; this.selectedModel = null; this.updateBreadcrumb(); this.hideFilters(); const container = document.getElementById('mainContent'); container.innerHTML = `

Cargando marcas...

`; try { const [brandsRes, vehiclesRes] = await Promise.all([ fetch('/api/brands'), fetch('/api/vehicles') ]); if (!brandsRes.ok || !vehiclesRes.ok) { throw new Error('Error al cargar datos'); } const brands = await brandsRes.json(); const vehicles = await vehiclesRes.json(); // Contar modelos y vehículos por marca const brandStats = {}; brands.forEach(brand => { brandStats[brand] = { models: new Set(), vehicles: 0 }; }); vehicles.forEach(v => { if (brandStats[v.brand]) { brandStats[v.brand].models.add(v.model); brandStats[v.brand].vehicles++; } }); if (brands.length === 0) { container.innerHTML = `

No hay marcas disponibles

Agrega algunas marcas a la base de datos

`; return; } container.innerHTML = `
${brands.map(brand => `
${brand}
${brandStats[brand].models.size} modelos
${brandStats[brand].vehicles} vehículos
`).join('')}
`; } catch (error) { console.error('Error:', error); container.innerHTML = `

Error al cargar marcas

${error.message}

`; } } async goToModels(brand) { this.currentView = 'models'; this.selectedBrand = brand; this.selectedModel = null; this.updateBreadcrumb(); this.hideFilters(); const container = document.getElementById('mainContent'); container.innerHTML = `

Cargando modelos de ${brand}...

`; try { const [modelsRes, vehiclesRes] = await Promise.all([ fetch(`/api/models?brand=${encodeURIComponent(brand)}`), fetch(`/api/vehicles?brand=${encodeURIComponent(brand)}`) ]); if (!modelsRes.ok || !vehiclesRes.ok) { throw new Error('Error al cargar datos'); } const models = await modelsRes.json(); const vehicles = await vehiclesRes.json(); // Contar vehículos y años por modelo const modelStats = {}; models.forEach(model => { modelStats[model] = { years: new Set(), vehicles: 0, engines: new Set() }; }); vehicles.forEach(v => { if (modelStats[v.model]) { modelStats[v.model].years.add(v.year); modelStats[v.model].vehicles++; modelStats[v.model].engines.add(v.engine); } }); if (models.length === 0) { container.innerHTML = `

No hay modelos para ${brand}

Esta marca no tiene modelos registrados

`; return; } container.innerHTML = `
${models.map(model => { const stats = modelStats[model]; const yearsArray = Array.from(stats.years).sort((a, b) => b - a); const yearRange = yearsArray.length > 0 ? (yearsArray.length > 1 ? `${yearsArray[yearsArray.length - 1]} - ${yearsArray[0]}` : `${yearsArray[0]}`) : 'N/A'; return `
${model}
${yearRange}
${stats.engines.size} motores
${stats.vehicles} variantes
`; }).join('')}
`; } catch (error) { console.error('Error:', error); container.innerHTML = `

Error al cargar modelos

${error.message}

`; } } async goToVehicles(brand, model) { this.currentView = 'vehicles'; this.selectedBrand = brand; this.selectedModel = model; this.updateBreadcrumb(); this.showFilters(); const container = document.getElementById('mainContent'); container.innerHTML = `

Cargando vehículos...

`; try { const response = await fetch( `/api/vehicles?brand=${encodeURIComponent(brand)}&model=${encodeURIComponent(model)}` ); if (!response.ok) { throw new Error('Error al cargar vehículos'); } this.allVehicles = await response.json(); this.filteredVehicles = [...this.allVehicles]; // Poblar filtros await this.populateFilters(brand, model); this.displayVehicles(); } catch (error) { console.error('Error:', error); container.innerHTML = `

Error al cargar vehículos

${error.message}

`; } } async populateFilters(brand, model) { try { const [yearsRes, enginesRes] = await Promise.all([ fetch(`/api/years?brand=${encodeURIComponent(brand)}&model=${encodeURIComponent(model)}`), fetch(`/api/engines?brand=${encodeURIComponent(brand)}&model=${encodeURIComponent(model)}`) ]); if (yearsRes.ok) { const years = await yearsRes.json(); const yearFilter = document.getElementById('yearFilter'); yearFilter.innerHTML = ''; years.forEach(year => { yearFilter.innerHTML += ``; }); } if (enginesRes.ok) { const engines = await enginesRes.json(); const engineFilter = document.getElementById('engineFilter'); engineFilter.innerHTML = ''; engines.forEach(engine => { engineFilter.innerHTML += ``; }); } } catch (error) { console.error('Error populating filters:', error); } } bindFilterEvents() { document.getElementById('yearFilter').addEventListener('change', () => { this.applyFilters(); }); document.getElementById('engineFilter').addEventListener('change', () => { this.applyFilters(); }); } applyFilters() { const year = document.getElementById('yearFilter').value; const engine = document.getElementById('engineFilter').value; this.filteredVehicles = this.allVehicles.filter(v => { return (!year || v.year.toString() === year) && (!engine || v.engine === engine); }); this.displayVehicles(); } displayVehicles() { const container = document.getElementById('mainContent'); const resultCount = document.getElementById('resultCount'); resultCount.textContent = `${this.filteredVehicles.length} resultado${this.filteredVehicles.length !== 1 ? 's' : ''}`; if (this.filteredVehicles.length === 0) { container.innerHTML = `

No se encontraron vehículos

Intenta ajustar los filtros

`; return; } container.innerHTML = `
${this.filteredVehicles.map(v => `
${v.year} ${v.brand} ${v.model}
${v.engine}
${v.fuel_type || 'N/A'}
${v.power_hp || 0} HP
${v.transmission || 'N/A'}
${v.drivetrain || 'N/A'}
${v.cylinders || 0} cil.
${v.displacement_cc || 0} cc
${v.trim_level && v.trim_level !== 'unknown' ? `
${v.trim_level}
` : ''}
`).join('')}
`; } goToBrands() { this.showBrands(); } showFilters() { document.getElementById('filtersBar').style.display = 'block'; // Reset filters document.getElementById('yearFilter').value = ''; document.getElementById('engineFilter').value = ''; } hideFilters() { document.getElementById('filtersBar').style.display = 'none'; } } // Initialize dashboard globally let dashboard; document.addEventListener('DOMContentLoaded', () => { dashboard = new VehicleDashboard(); });