'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import TrajetForm from './TrajetForm'; import AdherentForm from './AdherentForm'; import TrajetDetailModal from './TrajetDetailModal'; import { getParticipationRef } from '@/lib/participation-ref'; interface Stats { participationsMois: { montant: number; nombreFactures: number; }; trajetsAujourdhui: { nombre: number; difference: number; }; trajetsRealisesMois: { nombre: number; pourcentageEvolution: number; }; chauffeursActifs: { nombre: number; total: number; }; } interface Trajet { id: string; date: string; adresseDepart: string; adresseArrivee: string; commentaire?: string | null; statut: string; participations?: { id: string }[]; adherent: { id: string; nom: string; prenom: string; telephone: string; email: string; }; chauffeur?: { id: string; nom: string; prenom: string; telephone: string; } | null; } interface DashboardContentProps { userName?: string | null; } export default function DashboardContent({ userName }: DashboardContentProps) { const router = useRouter(); const [stats, setStats] = useState(null); const [trajetsRecents, setTrajetsRecents] = useState([]); const [loading, setLoading] = useState(true); const [showTrajetForm, setShowTrajetForm] = useState(false); const [showAdherentForm, setShowAdherentForm] = useState(false); const [selectedTrajet, setSelectedTrajet] = useState(null); useEffect(() => { fetchStats(); fetchTrajetsRecents(); }, []); const fetchStats = async () => { try { const response = await fetch('/api/dashboard/stats'); if (response.ok) { const data = await response.json(); setStats(data); } } catch (error) { console.error('Erreur lors du chargement des statistiques:', error); } }; const fetchTrajetsRecents = async () => { setLoading(true); try { const response = await fetch('/api/trajets?limit=3'); if (response.ok) { const data = await response.json(); setTrajetsRecents(data); } } catch (error) { console.error('Erreur lors du chargement des trajets récents:', error); } finally { setLoading(false); } }; const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric', }); }; const formatTime = (dateString: string) => { const date = new Date(dateString); return date.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }); }; const getInitials = (nom: string, prenom: string) => { return `${prenom.charAt(0)}${nom.charAt(0)}`.toUpperCase(); }; const getStatusColor = (statut: string) => { switch (statut) { case 'Validé': return 'bg-green-100 text-green-700'; case 'Terminé': return 'bg-green-100 text-green-700'; case 'En cours': return 'bg-blue-100 text-blue-700'; case 'Planifié': return 'bg-gray-100 text-gray-700'; case 'Annulé': return 'bg-red-100 text-red-700'; default: return 'bg-gray-100 text-gray-700'; } }; const getStatusDotColor = (statut: string) => { switch (statut) { case 'Validé': case 'Terminé': return 'bg-green-500'; case 'En cours': return 'bg-blue-500'; case 'Planifié': return 'bg-yellow-500'; case 'Annulé': return 'bg-red-500'; default: return 'bg-gray-500'; } }; const getChauffeurNumber = (chauffeur: { prenom: string; nom: string } | null | undefined) => { if (!chauffeur) return null; // Simple hash pour obtenir un numéro de chauffeur (1-5) const hash = (chauffeur.prenom + chauffeur.nom).split('').reduce((acc, char) => acc + char.charCodeAt(0), 0); return (hash % 5) + 1; }; // Montant estimé par trajet (basé sur l'image) const getMontantTrajet = () => { return 6.80; // Montant moyen }; return (
{/* En-tête */}

Content de vous revoir {userName || 'Utilisateur'}

Bienvenue sur votre tableau de bord.

{/* Statistiques */}
{/* Participations du mois */}

Participations du mois

{stats ? `${stats.participationsMois.montant.toFixed(2).replace('.', ',')}€` : '0,00€'}

{stats ? `${stats.participationsMois.nombreFactures} participation${stats.participationsMois.nombreFactures > 1 ? 's' : ''}` : '0 participation'}

{/* Trajets Aujourd'hui */}

Trajets Aujourd'hui

{stats ? stats.trajetsAujourdhui.nombre : 0}

{stats && stats.trajetsAujourdhui.difference !== 0 ? `${stats.trajetsAujourdhui.difference > 0 ? '+' : ''}${stats.trajetsAujourdhui.difference} vs hier` : 'Aucun changement'}

{/* Trajets réalisés ce mois */}

Trajets réalisés ce mois

{stats ? stats.trajetsRealisesMois.nombre : 0}

{stats && stats.trajetsRealisesMois.pourcentageEvolution !== 0 ? `${stats.trajetsRealisesMois.pourcentageEvolution > 0 ? '+' : ''}${stats.trajetsRealisesMois.pourcentageEvolution}% vs mois dernier` : 'Aucun changement'}

{/* Chauffeurs actifs */}

Chauffeurs actifs

{stats ? stats.chauffeursActifs.nombre : 0}

{stats ? `Sur ${stats.chauffeursActifs.total} total` : 'Sur 0 total'}

{/* Actions Rapides et Trajets Récents côte à côte */}
{/* Actions Rapides */}

Actions Rapides

Bientôt ?

{/* Trajets Récents */}

Trajets Récents

{loading ? (
Chargement...
) : trajetsRecents.length === 0 ? (
Aucun trajet créé récemment
) : (
{trajetsRecents.map((trajet) => (
setSelectedTrajet(trajet)} className="p-3 sm:p-4 bg-gray-50 rounded-lg border border-gray-200 hover:border-gray-300 hover:shadow-sm transition-all cursor-pointer" >
{/* Avatar adhérent */}
{getInitials(trajet.adherent.nom, trajet.adherent.prenom)}
{/* Informations principales */}

{trajet.adherent.prenom} {trajet.adherent.nom}

{trajet.participations?.[0] && ( {getParticipationRef(trajet.participations[0].id)} )} {formatDate(trajet.date)} {formatTime(trajet.date)} {trajet.statut}
{/* Adresses */}
Départ:{' '} {trajet.adresseDepart}
Arrivée:{' '} {trajet.adresseArrivee}
{/* Chauffeur */}
{trajet.chauffeur ? (
{getInitials(trajet.chauffeur.nom, trajet.chauffeur.prenom)}
Chauffeur
{trajet.chauffeur.prenom} {trajet.chauffeur.nom}
) : (
Aucun chauffeur assigné
)}
{/* Commentaire */} {trajet.commentaire && (

{trajet.commentaire}

)}
))}
)}
{/* Modals */} {showTrajetForm && ( setShowTrajetForm(false)} onSuccess={() => { setShowTrajetForm(false); fetchTrajetsRecents(); fetchStats(); }} /> )} {showAdherentForm && ( { setShowAdherentForm(false); fetchStats(); }} /> )} {selectedTrajet && ( setSelectedTrajet(null)} onUpdate={() => { fetchTrajetsRecents(); fetchStats(); }} /> )}
); }