'use client'; import React, { useState, useEffect } from 'react'; import { useParams, useRouter } from 'next/navigation'; import { Card } from 'primereact/card'; import { Button } from 'primereact/button'; import { Calendar } from 'primereact/calendar'; import { Timeline } from 'primereact/timeline'; import { Tag } from 'primereact/tag'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Chart } from 'primereact/chart'; import { TabView, TabPanel } from 'primereact/tabview'; import { planningService } from '@/services/api'; import type { PlanningEvent } from '@/types/btp'; export default function ChantierPlanningPage() { const params = useParams(); const router = useRouter(); const id = params.id as string; const [taches, setTaches] = useState([]); const [loading, setLoading] = useState(true); const [selectedDate, setSelectedDate] = useState(new Date()); const [viewMode, setViewMode] = useState<'timeline' | 'gantt'>('timeline'); useEffect(() => { if (id) { loadPlanning(); } }, [id]); const loadPlanning = async () => { try { setLoading(true); // Récupérer les événements de planning pour ce chantier const data = await planningService.getEvents({ chantierId: id }); setTaches(data || []); } catch (error) { console.error('Erreur lors du chargement du planning:', error); // L'intercepteur API gérera automatiquement la redirection si 401 setTaches([]); } finally { setLoading(false); } }; const formatDate = (dateString: string) => { if (!dateString) return 'N/A'; return new Date(dateString).toLocaleDateString('fr-FR'); }; const getStatutSeverity = (statut: string) => { switch (statut?.toUpperCase()) { case 'A_FAIRE': return 'info'; case 'EN_COURS': return 'warning'; case 'TERMINE': return 'success'; case 'EN_RETARD': return 'danger'; default: return 'info'; } }; const getStatutLabel = (statut: string) => { const labels: { [key: string]: string } = { 'A_FAIRE': 'À faire', 'EN_COURS': 'En cours', 'TERMINE': 'Terminé', 'EN_RETARD': 'En retard' }; return labels[statut] || statut; }; const statutBodyTemplate = (rowData: PlanningEvent) => { return ( ); }; const dateBodyTemplate = (rowData: PlanningEvent) => { return (
Début: {formatDate(rowData.dateDebut)}
Fin: {formatDate(rowData.dateFin)}
); }; const equipeBodyTemplate = (rowData: PlanningEvent) => { return rowData.equipe?.nom || 'Non assignée'; }; const prioriteBodyTemplate = (rowData: PlanningEvent) => { const severity = rowData.priorite === 'CRITIQUE' ? 'danger' : rowData.priorite === 'HAUTE' ? 'warning' : rowData.priorite === 'NORMALE' ? 'info' : 'success'; return ; }; const actionsBodyTemplate = (rowData: PlanningEvent) => { return (
); }; const customizedMarker = (item: PlanningEvent) => { return ( ); }; const customizedContent = (item: PlanningEvent) => { return (
{item.titre}

{item.description}

Équipe: {item.equipe?.nom || 'Non assignée'}
Priorité: {item.priorite}
Dates: {formatDate(item.dateDebut)} - {formatDate(item.dateFin)}
); }; // Générer les données pour le diagramme de Gantt const getGanttData = () => { if (!taches || taches.length === 0) return null; // Calculer les durées en jours pour chaque tâche const ganttDatasets = taches.map(tache => { const debut = new Date(tache.dateDebut).getTime(); const fin = new Date(tache.dateFin).getTime(); const duree = Math.ceil((fin - debut) / (1000 * 60 * 60 * 24)); return { label: tache.titre, duree: duree, statut: tache.statut }; }); const colors = ganttDatasets.map(item => { switch (item.statut?.toUpperCase()) { case 'TERMINE': return '#10b981'; case 'EN_COURS': return '#3b82f6'; case 'EN_RETARD': return '#ef4444'; default: return '#6b7280'; } }); return { labels: ganttDatasets.map(d => d.label), datasets: [{ label: 'Durée (jours)', data: ganttDatasets.map(d => d.duree), backgroundColor: colors, borderColor: colors, borderWidth: 1 }] }; }; const ganttOptions = { indexAxis: 'y' as const, responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false }, title: { display: true, text: 'Diagramme de Gantt - Durée des tâches' } }, scales: { x: { beginAtZero: true, title: { display: true, text: 'Durée (jours)' } } } }; return (
{/* Calendrier */}
setSelectedDate(e.value as Date)} inline showWeek />

Légende

En cours
Terminé
En retard
À faire
{/* Timeline ou Gantt */}
{viewMode === 'timeline' ? ( ) : (
{getGanttData() ? ( ) : (

Aucune tâche à afficher dans le diagramme de Gantt

)}
)}
{/* Tableau des tâches */}
); }