'use client'; import React, { useState, useEffect } from 'react'; import { Card } from 'primereact/card'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Tag } from 'primereact/tag'; import { Button } from 'primereact/button'; import { ProgressBar } from 'primereact/progressbar'; import { Timeline } from 'primereact/timeline'; import { Avatar } from 'primereact/avatar'; import type { Chantier } from '../../../../types/btp'; import type { User } from '../../../../types/auth'; import chantierService from '../../../../services/chantierService'; import { useAuth } from '../../../../contexts/AuthContext'; interface ClientStats { chantiersTotal: number; chantiersEnCours: number; chantiersTermines: number; prochaineCheance: Date | null; } interface Notification { id: string; type: 'info' | 'warning' | 'success'; titre: string; message: string; date: Date; lu: boolean; } const ClientDashboard = () => { const [stats, setStats] = useState({ chantiersTotal: 0, chantiersEnCours: 0, chantiersTermines: 0, prochaineCheance: null }); const [mesChantiers, setMesChantiers] = useState([]); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); const { user: currentUser } = useAuth(); // Vérifier que l'utilisateur est connecté et est un client if (!currentUser || !currentUser.roles?.includes('CLIENT')) { return (

Accès non autorisé

Vous devez être connecté en tant que client.

); } useEffect(() => { loadClientData(); loadNotifications(); }, []); const loadClientData = async () => { try { setLoading(true); // Charger mes chantiers (en utilisant l'id utilisateur) const chantiers = await chantierService.getByClient(currentUser.id as any); setMesChantiers(chantiers); // Calculer les statistiques const chantiersEnCours = chantiers.filter(c => c.statut === 'EN_COURS').length; const chantiersTermines = chantiers.filter(c => c.statut === 'TERMINE').length; // Prochaine échéance (chantier avec dateFinPrevue la plus proche) const chantiersAvecEcheance = chantiers .filter(c => c.dateFinPrevue && c.statut === 'EN_COURS') .sort((a, b) => new Date(a.dateFinPrevue!).getTime() - new Date(b.dateFinPrevue!).getTime()); const prochaineCheance = chantiersAvecEcheance.length > 0 ? new Date(chantiersAvecEcheance[0].dateFinPrevue!) : null; setStats({ chantiersTotal: chantiers.length, chantiersEnCours, chantiersTermines, prochaineCheance }); } catch (error) { console.error('Erreur lors du chargement des données client:', error); } finally { setLoading(false); } }; const loadNotifications = () => { // Données mockées pour les notifications const mockNotifications: Notification[] = [ { id: '1', type: 'info', titre: 'Nouveau devis disponible', message: 'Le devis pour votre extension cuisine est maintenant disponible', date: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), lu: false }, { id: '2', type: 'warning', titre: 'Rendez-vous prévu', message: 'Rendez-vous avec votre gestionnaire demain à 14h00', date: new Date(Date.now() - 24 * 60 * 60 * 1000), lu: false }, { id: '3', type: 'success', titre: 'Phase terminée', message: 'La phase "Gros œuvre" de votre chantier a été terminée', date: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), lu: true } ]; setNotifications(mockNotifications); }; const chantierStatutBodyTemplate = (rowData: Chantier) => { return ( ); }; const montantBodyTemplate = (rowData: Chantier) => { return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(rowData.montantPrevu || 0); }; const avancementBodyTemplate = (rowData: Chantier) => { const avancement = chantierService.calculateAvancement(rowData); return (
{Math.round(avancement)}%
); }; const notificationTemplate = (item: Notification) => { const getSeverityIcon = (type: string) => { switch (type) { case 'warning': return 'pi-exclamation-triangle'; case 'success': return 'pi-check-circle'; default: return 'pi-info-circle'; } }; const getSeverityColor = (type: string) => { switch (type) { case 'warning': return 'orange'; case 'success': return 'green'; default: return 'blue'; } }; return (
{item.titre}
{item.date.toLocaleDateString('fr-FR')}

{item.message}

{!item.lu && (
)}
); }; return (
{/* Header */}

Mon Espace Client

Bienvenue {currentUser.firstName} {currentUser.lastName}

{/* Statistiques */}
{stats.chantiersTotal}
Mes projets
{stats.chantiersEnCours}
En cours
{stats.chantiersTermines}
Terminés
{stats.prochaineCheance ? stats.prochaineCheance.toLocaleDateString('fr-FR') : 'Aucune' }
Prochaine échéance
{/* Notifications et projets */}
{notifications.slice(0, 4).map((notif) => (
{notificationTemplate(notif)}
))}
c.statut === 'EN_COURS')} paginator rows={5} dataKey="id" emptyMessage="Aucun projet en cours" loading={loading} > new Date(rowData.dateDebut).toLocaleDateString('fr-FR')} style={{ minWidth: '8rem' }} /> rowData.dateFinPrevue ? new Date(rowData.dateFinPrevue).toLocaleDateString('fr-FR') : ''} style={{ minWidth: '8rem' }} /> (
{/* Tous mes projets */}
new Date(rowData.dateDebut).toLocaleDateString('fr-FR')} sortable style={{ minWidth: '10rem' }} /> rowData.dateFinPrevue ? new Date(rowData.dateFinPrevue).toLocaleDateString('fr-FR') : ''} sortable style={{ minWidth: '10rem' }} /> (
)} style={{ minWidth: '6rem' }} />
); }; export default ClientDashboard;