import { BORDER_RADIUS, COLORS, SPACING } from "@/constants/theme"; import { useAuth } from "@/contexts/AuthContext"; import { obterCorridas } from "@/services/db"; import { router, useFocusEffect } from "expo-router"; import React, { useCallback, useState } from "react"; import { FlatList, ScrollView, StyleSheet, Text, TouchableOpacity, View, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; type Corrida = { id: string; data: string; empresa: string; km: number; custoPorKm: number; total: number; }; const HistoricoItem = ({ item, index }: { item: Corrida; index: number }) => ( #{index + 1} {item.data} {item.empresa} Distância {item.km} km Valor/km R$ {item.custoPorKm.toFixed(2)} Total R$ {item.total.toFixed(2)} ); export default function HistoricoPage() { const { user } = useAuth(); const [corridas, setCorridas] = useState([]); const loadCorridas = useCallback(async () => { if (!user?.id) return; try { const data = await obterCorridas(user.id); setCorridas( data.map((c) => ({ id: c.id, data: c.ride_date, empresa: c.company, km: c.km, custoPorKm: c.cost_per_km, total: c.total, })), ); } catch (error) { console.error("Erro ao carregar histórico:", error); } }, [user?.id]); useFocusEffect(useCallback(() => { loadCorridas(); }, [loadCorridas])); const totalGanhos = corridas.reduce((acc, c) => acc + c.total, 0); const totalKm = corridas.reduce((acc, c) => acc + c.km, 0); return ( router.back()} style={styles.backButton} activeOpacity={0.7}> Voltar Histórico {corridas.length} corrida{corridas.length !== 1 ? "s" : ""} registrada{corridas.length !== 1 ? "s" : ""} {corridas.length > 0 && ( {totalKm.toFixed(0)} km Total Rodado R$ {totalGanhos.toFixed(2)} Total Ganho )} {corridas.length === 0 ? ( 🚗 Nenhuma corrida ainda Registre sua primeira corrida para vê-la aqui. ) : ( item.id} renderItem={({ item, index }) => ( )} contentContainerStyle={styles.listContent} showsVerticalScrollIndicator={false} /> )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: COLORS.background, }, header: { paddingHorizontal: SPACING.lg, paddingVertical: SPACING.md, borderBottomWidth: 1, borderBottomColor: COLORS.border, }, backButton: { flexDirection: "row", alignItems: "center", gap: SPACING.xs, alignSelf: "flex-start", }, backIcon: { fontSize: 18, color: COLORS.text, }, backLabel: { fontSize: 15, color: COLORS.textSecondary, fontWeight: "500", }, titleSection: { paddingHorizontal: SPACING.lg, paddingTop: SPACING.lg, paddingBottom: SPACING.md, }, title: { fontSize: 28, fontWeight: "800", color: COLORS.text, marginBottom: 4, }, subtitle: { fontSize: 13, color: COLORS.textTertiary, }, summaryRow: { flexDirection: "row", gap: SPACING.sm, paddingHorizontal: SPACING.lg, paddingBottom: SPACING.lg, }, summaryCard: { flex: 1, backgroundColor: COLORS.surface, borderRadius: BORDER_RADIUS.lg, padding: SPACING.md, borderWidth: 1, borderColor: COLORS.border, }, summaryCardAccent: { borderColor: COLORS.success, }, summaryValue: { fontSize: 18, fontWeight: "800", color: COLORS.text, marginBottom: 2, }, summaryValueAccent: { color: COLORS.success, }, summaryLabel: { fontSize: 11, color: COLORS.textTertiary, fontWeight: "600", textTransform: "uppercase", letterSpacing: 0.5, }, listContent: { padding: SPACING.lg, paddingBottom: SPACING.xxl, }, item: { backgroundColor: COLORS.surface, borderRadius: BORDER_RADIUS.lg, marginBottom: SPACING.md, overflow: "hidden", borderWidth: 1, borderColor: COLORS.border, }, itemHeader: { flexDirection: "row", paddingHorizontal: SPACING.lg, paddingTop: SPACING.md, paddingBottom: SPACING.sm, justifyContent: "space-between", alignItems: "center", borderBottomWidth: 1, borderBottomColor: COLORS.border, }, itemNumber: { fontSize: 12, fontWeight: "700", color: COLORS.textTertiary, letterSpacing: 0.5, }, itemDate: { fontSize: 12, color: COLORS.textTertiary, }, itemEmpresa: { paddingHorizontal: SPACING.lg, paddingVertical: SPACING.sm, backgroundColor: COLORS.surfaceLight, }, empresaLabel: { fontSize: 14, fontWeight: "700", color: COLORS.text, }, itemDetails: { flexDirection: "row", paddingHorizontal: SPACING.lg, paddingVertical: SPACING.md, }, detailBox: { flex: 1, }, detailBoxHighlight: { alignItems: "flex-end", }, detailLabel: { fontSize: 10, color: COLORS.textTertiary, fontWeight: "600", textTransform: "uppercase", letterSpacing: 0.4, marginBottom: 3, }, detailValue: { fontSize: 14, fontWeight: "700", color: COLORS.text, }, detailValueHighlight: { color: COLORS.success, fontSize: 16, }, emptyContainer: { flex: 1, justifyContent: "center", alignItems: "center", padding: SPACING.lg, }, emptyBox: { alignItems: "center", }, emptyEmoji: { fontSize: 48, marginBottom: SPACING.lg, }, emptyTitle: { fontSize: 18, fontWeight: "700", color: COLORS.text, marginBottom: SPACING.sm, }, emptyText: { fontSize: 14, color: COLORS.textTertiary, textAlign: "center", lineHeight: 20, }, });