package pages import ( "fmt" "tankstopp/internal/models" "tankstopp/internal/views/components" ) templ DashboardPage(user *models.User, username string, stops []models.FuelStop, vehicles []models.Vehicle, totalStops int, totalCost float64, avgConsumption float64, lastFillUp *models.FuelStop) { @components.BaseLayout("Dashboard", user, username) { @components.PageHeader("Overview", "Dashboard")
@components.Card("Total Stops", "gas-station") {
{ fmt.Sprintf("%d", totalStops) }
75%
}
@components.Card("Total Spent", "currency") {
Total fuel costs
{ fmt.Sprintf("%.2f %s", totalCost, user.BaseCurrency) }
60%
}
@components.Card("Avg Consumption", "gauge") {
Liters per 100km
if avgConsumption > 0 { { fmt.Sprintf("%.1f L/100km", avgConsumption) } } else { { "N/A" } }
Efficiency
}
@components.Card("Vehicles", "car") {
Active vehicles
{ fmt.Sprintf("%d", len(vehicles)) }
100%
}
@components.Card("Total Distance", "trip") {
Kilometers driven
{ fmt.Sprintf("%.0f km", calculateTotalDistance(stops)) }
Tracked
}
@components.Card("Efficiency Trend", "chart-bar") {
Recent performance
if len(stops) >= 3 { if calculateEfficiencyTrend(stops) == "improving" { Improving } else if calculateEfficiencyTrend(stops) == "worsening" { Declining } else { Stable } } else { N/A }
if calculateEfficiencyTrend(stops) == "improving" {
} else if calculateEfficiencyTrend(stops) == "worsening" {
} else {
}
Trend
}
@components.Card("Best Efficiency", "award") {
Lowest consumption
{ fmt.Sprintf("%.1f L/100km", getBestEfficiency(stops)) }
Personal best
}
if lastFillUp != nil {

@components.Icon("clock", 24) Last Fill-up

Date
{ lastFillUp.Date.Format("Jan 2, 2006") }
Amount
{ fmt.Sprintf("%.2f L", lastFillUp.Liters) }
Cost
{ fmt.Sprintf("%.2f %s", lastFillUp.TotalPrice, user.BaseCurrency) }
Location
{ lastFillUp.Location }
}

Filters

@components.FormGroup("Vehicle", "") { @components.VehicleSelect("vehicle_id", 0, vehicles, false) }
@components.FormGroup("Fuel Type", "") { @components.FuelTypeSelect("fuel_type", "", false) }
@components.FormGroup("Date From", "") { @components.DateInput("date_from", "", false) }
@components.FormGroup("Date To", "") { @components.DateInput("date_to", "", false) }
@components.ButtonGroup() { }
if len(stops) > 0 { @FuelStopsTable(stops, user.BaseCurrency) } else { @components.EmptyState("gas-station", "No fuel stops found", "Start tracking your fuel expenses by adding your first fuel stop.", "Add your first stop", "/add") }
@DashboardScript() } } templ FuelStopsTable(stops []models.FuelStop, currency string) {

Recent Fuel Stops

for _, stop := range stops { }
Date Vehicle Location Fuel Type Amount Price/L Total Trip Length Consumption Odometer Actions
{ stop.Date.Format("Jan 2, 2006") }
{ stop.Date.Format("15:04") }
{ stop.Vehicle.Name }
if stop.Vehicle.LicensePlate != "" {
{ stop.Vehicle.LicensePlate }
}
@components.Icon("location", 24)
{ stop.Location }
{ stop.FuelType }
{ fmt.Sprintf("%.2f L", stop.Liters) }
{ fmt.Sprintf("%.3f %s", stop.PricePerL, currency) }
{ fmt.Sprintf("%.2f %s", stop.TotalPrice, currency) }
if stop.TripLength > 0 {
{ fmt.Sprintf("%.1f km", stop.TripLength) }
} else {
Not recorded
}
if stop.TripLength > 0 {
{ fmt.Sprintf("%.1f L/100km", (stop.Liters/stop.TripLength)*100) }
} else {
N/A
}
if stop.Odometer > 0 {
{ fmt.Sprintf("%d km", stop.Odometer) }
} else {
Not recorded
}
@components.ButtonGroup() { @components.EditButton(fmt.Sprintf("/edit/%d", stop.ID)) @components.DeleteButton(fmt.Sprintf("/delete/%d", stop.ID), fmt.Sprintf("fuel stop from %s", stop.Date.Format("Jan 2, 2006"))) }
} // Helper functions for statistics calculations func calculateTotalDistance(stops []models.FuelStop) float64 { var total float64 for _, stop := range stops { if stop.TripLength > 0 { total += stop.TripLength } } return total } func calculateEfficiencyTrend(stops []models.FuelStop) string { if len(stops) < 3 { return "insufficient_data" } // Calculate average consumption for recent vs older stops recent := stops[:len(stops)/2] older := stops[len(stops)/2:] recentAvg := calculateAverageConsumption(recent) olderAvg := calculateAverageConsumption(older) if recentAvg == 0 || olderAvg == 0 { return "insufficient_data" } diff := recentAvg - olderAvg if diff < -0.5 { return "improving" } else if diff > 0.5 { return "worsening" } return "stable" } func calculateAverageConsumption(stops []models.FuelStop) float64 { var totalConsumption float64 var count int for _, stop := range stops { if stop.TripLength > 0 { consumption := (stop.Liters / stop.TripLength) * 100 if consumption > 0 && consumption < 50 { totalConsumption += consumption count++ } } } if count == 0 { return 0 } return totalConsumption / float64(count) } func getBestEfficiency(stops []models.FuelStop) float64 { bestEfficiency := float64(999) // Start with high value for _, stop := range stops { if stop.TripLength > 0 { consumption := (stop.Liters / stop.TripLength) * 100 if consumption > 0 && consumption < bestEfficiency && consumption < 50 { bestEfficiency = consumption } } } if bestEfficiency == 999 { return 0 } return bestEfficiency } script DashboardScript() { function applyFilters() { const vehicleId = document.querySelector('select[name="vehicle_id"]').value; const fuelType = document.querySelector('select[name="fuel_type"]').value; const dateFrom = document.querySelector('input[name="date_from"]').value; const dateTo = document.querySelector('input[name="date_to"]').value; const params = new URLSearchParams(); if (vehicleId) params.append('vehicle_id', vehicleId); if (fuelType) params.append('fuel_type', fuelType); if (dateFrom) params.append('date_from', dateFrom); if (dateTo) params.append('date_to', dateTo); window.location.href = '/dashboard?' + params.toString(); } function clearFilters() { window.location.href = '/dashboard'; } function confirmDelete(itemName) { return confirm(`Are you sure you want to delete this ${itemName}? This action cannot be undone.`); } }