import React, { useState, useEffect } from 'react'; import SelectChoice from '@/components/SelectChoice'; import { useClasses } from '@/context/ClassesContext'; import { useClasseForm } from '@/context/ClasseFormContext'; import { BE_SCHOOL_PLANNING_URL } from '@/utils/Url'; import { BookOpen, Users } from 'lucide-react'; const SpecialityEventModal = ({ isOpen, onClose, selectedCell, existingEvent, handleUpdatePlanning, classe }) => { const { formData, setFormData } = useClasseForm(); const { groupSpecialitiesBySubject } = useClasses(); const [selectedSpeciality, setSelectedSpeciality] = useState(''); const [selectedTeacher, setSelectedTeacher] = useState(''); const [eventData, setEventData] = useState({ specialiteId: '', teacherId: '', start: '', duration: '1h' }); useEffect(() => { if (!isOpen) { // Réinitialiser eventData lorsque la modale se ferme setEventData({ specialiteId: '', teacherId: '', start: '', duration: '1h' }); setSelectedSpeciality(''); setSelectedTeacher(''); } }, [isOpen]); useEffect(() => { if (isOpen) { console.log('debug : ', selectedCell); if (existingEvent) { // Mode édition setEventData(existingEvent); setSelectedSpeciality(existingEvent.specialiteId); setSelectedTeacher(existingEvent.teacherId); } else { // Mode création setEventData(prev => ({ ...prev, start: selectedCell.hour, duration: '1h' })); setSelectedSpeciality(''); setSelectedTeacher(''); } } }, [isOpen, existingEvent, selectedCell]); if (!isOpen) return null; const handleSubmit = (e) => { e.preventDefault(); if (!eventData.specialiteId) { alert('Veuillez sélectionner une spécialité'); return; } if (!eventData.teacherId) { alert('Veuillez sélectionner un enseignant'); return; } // Transformer eventData pour correspondre au format du planning const selectedTeacherData = formData.teachers.find(teacher => teacher.id === parseInt(eventData.teacherId, 10)); const newCourse = { color: '#FF0000', // Vous pouvez définir la couleur de manière dynamique si nécessaire teachers: selectedTeacherData ? [`${selectedTeacherData.nom} ${selectedTeacherData.prenom}`] : [], heure: `${eventData.start}:00`, duree: eventData.duration.replace('h', ''), // Supposons que '1h' signifie 1 matiere: 'GROUPE' }; // Mettre à jour le planning const updatedPlannings = classe.plannings_read.map(planning => { if (planning.niveau === selectedCell.selectedLevel) { const newEmploiDuTemps = { ...planning.emploiDuTemps }; if (!newEmploiDuTemps[selectedCell.day]) { newEmploiDuTemps[selectedCell.day] = []; } const courseTime = newCourse.heure; const existingCourseIndex = newEmploiDuTemps[selectedCell.day].findIndex(course => course.heure === courseTime); if (existingCourseIndex !== -1) { newEmploiDuTemps[selectedCell.day][existingCourseIndex] = newCourse; } else { newEmploiDuTemps[selectedCell.day].push(newCourse); } return { ...planning, emploiDuTemps: newEmploiDuTemps }; } return planning; }); const updatedPlanning = updatedPlannings.find(planning => planning.niveau === selectedCell.selectedLevel); setFormData(prevFormData => ({ ...prevFormData, plannings: updatedPlannings })); // Appeler handleUpdatePlanning avec les arguments appropriés const planningId = updatedPlanning ? updatedPlanning.planning.id : null; console.log("id : ", planningId) if (planningId) { handleUpdatePlanning(BE_SCHOOL_PLANNING_URL, planningId, updatedPlanning.emploiDuTemps); } onClose(); }; const filteredTeachers = selectedSpeciality ? formData.teachers.filter(teacher => teacher.specialites.includes(parseInt(selectedSpeciality, 10)) ) : formData.teachers; const handleSpecialityChange = (e) => { const specialityId = e.target.value; setSelectedSpeciality(specialityId); // Mettre à jour eventData setEventData(prev => ({ ...prev, specialiteId: specialityId })); }; const handleTeacherChange = (e) => { const teacherId = e.target.value; setSelectedTeacher(teacherId); // Mettre à jour eventData setEventData(prev => ({ ...prev, teacherId: teacherId })); }; return (