'use client'; import { useState, useEffect } from 'react'; interface Adherent { id: string; nom: string; prenom: string; dateNaissance: string; adresse: string; email: string; telephone: string; situation?: string | null; prescripteur?: string | null; facturation?: string | null; commentaire?: string | null; telephoneSecondaire?: string | null; instructions?: string | null; } interface AdherentFormProps { adherent: Adherent | null; onClose: () => void; } export default function AdherentForm({ adherent, onClose }: AdherentFormProps) { const [loading, setLoading] = useState(false); const [options, setOptions] = useState<{ situation: Array<{ id: string; value: string }>; prescripteur: Array<{ id: string; value: string }>; facturation: Array<{ id: string; value: string }>; }>({ situation: [], prescripteur: [], facturation: [], }); const [formData, setFormData] = useState({ nom: '', prenom: '', dateNaissance: '', adresse: '', email: '', telephone: '', situation: '', prescripteur: '', facturation: '', commentaire: '', telephoneSecondaire: '', instructions: '', }); useEffect(() => { fetchOptions(); }, []); const fetchOptions = async () => { try { const response = await fetch('/api/settings/adherent-options'); if (response.ok) { const data = await response.json(); setOptions({ situation: data.situation || [], prescripteur: data.prescripteur || [], facturation: data.facturation || [], }); } } catch (error) { console.error('Erreur lors du chargement des options:', error); } }; useEffect(() => { if (adherent) { const dateNaissance = new Date(adherent.dateNaissance); setFormData({ nom: adherent.nom, prenom: adherent.prenom, dateNaissance: dateNaissance.toISOString().split('T')[0], adresse: adherent.adresse, email: adherent.email, telephone: adherent.telephone, situation: adherent.situation || '', prescripteur: adherent.prescripteur || '', facturation: adherent.facturation || '', commentaire: adherent.commentaire || '', telephoneSecondaire: adherent.telephoneSecondaire || '', instructions: adherent.instructions || '', }); } }, [adherent]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { const url = adherent ? `/api/adherents/${adherent.id}` : '/api/adherents'; const method = adherent ? 'PUT' : 'POST'; const payload = { ...formData, situation: formData.situation || null, prescripteur: formData.prescripteur || null, facturation: formData.facturation || null, commentaire: formData.commentaire || null, telephoneSecondaire: formData.telephoneSecondaire || null, instructions: formData.instructions || null, }; const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }); if (response.ok) { onClose(); } else { const error = await response.json(); alert(error.error || 'Une erreur est survenue'); } } catch (error) { console.error('Erreur:', error); alert('Une erreur est survenue'); } finally { setLoading(false); } }; return (
{adherent ? 'Modifiez les informations de l\'adhérent ci-dessous.' : 'Remplissez les informations pour créer un nouvel adhérent.'}