'use client'; export const dynamic = 'force-dynamic'; import React, { useState, useEffect, useRef } from 'react'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Button } from 'primereact/button'; import { InputText } from 'primereact/inputtext'; import { Card } from 'primereact/card'; import { Toast } from 'primereact/toast'; import { Toolbar } from 'primereact/toolbar'; import { Tag } from 'primereact/tag'; import { Dialog } from 'primereact/dialog'; import { Calendar } from 'primereact/calendar'; import { chantierService } from '../../../../services/api'; import { formatDate, formatCurrency } from '../../../../utils/formatters'; import type { Chantier } from '../../../../types/btp'; const ChantiersPlanifiesPage = () => { const [chantiers, setChantiers] = useState([]); const [loading, setLoading] = useState(true); const [globalFilter, setGlobalFilter] = useState(''); const [selectedChantiers, setSelectedChantiers] = useState([]); const [startDialog, setStartDialog] = useState(false); const [selectedChantier, setSelectedChantier] = useState(null); const [startDate, setStartDate] = useState(new Date()); const toast = useRef(null); const dt = useRef>(null); useEffect(() => { loadChantiers(); }, []); const loadChantiers = async () => { try { setLoading(true); const data = await chantierService.getAll(); // Filtrer seulement les chantiers planifiés const chantiersPlanifies = data.filter(chantier => chantier.statut === 'PLANIFIE'); setChantiers(chantiersPlanifies); } catch (error) { console.error('Erreur lors du chargement des chantiers:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de charger les chantiers planifiés', life: 3000 }); } finally { setLoading(false); } }; const getDaysUntilStart = (dateDebut: string | Date) => { const today = new Date(); const start = new Date(dateDebut); const diffTime = start.getTime() - today.getTime(); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); return diffDays; }; const isStartingSoon = (dateDebut: string | Date) => { const days = getDaysUntilStart(dateDebut); return days >= 0 && days <= 7; // Dans les 7 prochains jours }; const isOverdue = (dateDebut: string | Date) => { const days = getDaysUntilStart(dateDebut); return days < 0; // Date de début dépassée }; const startChantier = (chantier: Chantier) => { setSelectedChantier(chantier); setStartDate(new Date()); setStartDialog(true); }; const handleStartChantier = async () => { if (!selectedChantier) return; try { const updatedChantier = { ...selectedChantier, statut: 'EN_COURS' as any, dateDebut: startDate.toISOString().split('T')[0] }; await chantierService.update(selectedChantier.id, updatedChantier); // Retirer le chantier de la liste car il n'est plus "planifié" setChantiers(prev => prev.filter(c => c.id !== selectedChantier.id)); setStartDialog(false); toast.current?.show({ severity: 'success', summary: 'Succès', detail: 'Chantier démarré avec succès', life: 3000 }); } catch (error) { console.error('Erreur lors du démarrage:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de démarrer le chantier', life: 3000 }); } }; const exportCSV = () => { dt.current?.exportCSV(); }; const bulkStart = async () => { if (selectedChantiers.length === 0) { toast.current?.show({ severity: 'warn', summary: 'Attention', detail: 'Veuillez sélectionner au moins un chantier', life: 3000 }); return; } try { await Promise.all( selectedChantiers.map(chantier => chantierService.update(chantier.id, { ...chantier, statut: 'EN_COURS' as any, dateDebut: new Date().toISOString().split('T')[0] }) ) ); setChantiers(prev => prev.filter(c => !selectedChantiers.includes(c))); setSelectedChantiers([]); toast.current?.show({ severity: 'success', summary: 'Succès', detail: `${selectedChantiers.length} chantier(s) démarré(s)`, life: 3000 }); } catch (error) { console.error('Erreur lors du démarrage en lot:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de démarrer les chantiers sélectionnés', life: 3000 }); } }; const leftToolbarTemplate = () => { return (
Chantiers planifiés ({chantiers.length})
); }; const rightToolbarTemplate = () => { return (