'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { useNotification } from './NotificationProvider'; import useSWR from 'swr'; interface User { id: string; email: string; name: string | null; roleId: string | null; role: { id: string; name: string; description: string | null; } | null; } interface AdherentOption { id: string; type: 'situation' | 'prescripteur' | 'facturation'; value: string; order: number; } interface OptionsByType { situation: AdherentOption[]; prescripteur: AdherentOption[]; facturation: AdherentOption[]; } const fetcher = (url: string) => fetch(url).then((res) => res.json()); export default function ParametresContent() { const { showNotification } = useNotification(); const { data: user } = useSWR('/api/auth/me', fetcher); const [activeConfigSection, setActiveConfigSection] = useState<'adherents' | null>(null); const [options, setOptions] = useState({ situation: [], prescripteur: [], facturation: [], }); const [loading, setLoading] = useState(true); const [editingId, setEditingId] = useState(null); const [editingValue, setEditingValue] = useState(''); const [newValue, setNewValue] = useState>({ situation: '', prescripteur: '', facturation: '', }); useEffect(() => { fetchOptions(); }, []); const fetchOptions = async () => { setLoading(true); 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); showNotification('Erreur lors du chargement des options', 'error'); } finally { setLoading(false); } }; const handleAdd = async (type: 'situation' | 'prescripteur' | 'facturation') => { const value = newValue[type].trim(); if (!value) { showNotification('Veuillez entrer une valeur', 'error'); return; } try { const response = await fetch('/api/settings/adherent-options', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ type, value, }), }); if (response.ok) { setNewValue({ ...newValue, [type]: '' }); await fetchOptions(); showNotification('Option ajoutée avec succès', 'success'); } else { const error = await response.json(); showNotification(error.error || 'Erreur lors de l\'ajout', 'error'); } } catch (error) { console.error('Erreur:', error); showNotification('Erreur lors de l\'ajout', 'error'); } }; const handleEdit = (option: AdherentOption) => { setEditingId(option.id); setEditingValue(option.value); }; const handleSaveEdit = async (id: string, type: 'situation' | 'prescripteur' | 'facturation') => { const value = editingValue.trim(); if (!value) { showNotification('Veuillez entrer une valeur', 'error'); return; } try { const response = await fetch(`/api/settings/adherent-options/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ value }), }); if (response.ok) { setEditingId(null); setEditingValue(''); await fetchOptions(); showNotification('Option modifiée avec succès', 'success'); } else { const error = await response.json(); showNotification(error.error || 'Erreur lors de la modification', 'error'); } } catch (error) { console.error('Erreur:', error); showNotification('Erreur lors de la modification', 'error'); } }; const handleCancelEdit = () => { setEditingId(null); setEditingValue(''); }; const handleDelete = async (id: string) => { if (!confirm('Êtes-vous sûr de vouloir supprimer cette option ?')) { return; } try { const response = await fetch(`/api/settings/adherent-options/${id}`, { method: 'DELETE', }); if (response.ok) { await fetchOptions(); showNotification('Option supprimée avec succès', 'success'); } else { const error = await response.json(); showNotification(error.error || 'Erreur lors de la suppression', 'error'); } } catch (error) { console.error('Erreur:', error); showNotification('Erreur lors de la suppression', 'error'); } }; const OptionCard = ({ type, label, icon, }: { type: 'situation' | 'prescripteur' | 'facturation'; label: string; icon: React.ReactNode; }) => { const typeOptions = options[type] || []; return (
{icon}

{label}

{typeOptions.length} option{typeOptions.length > 1 ? 's' : ''}
{/* Liste des options */}
{loading ? (
Chargement...
) : typeOptions.length === 0 ? (
Aucune option configurée
) : ( typeOptions.map((option) => (
{editingId === option.id ? ( <> setEditingValue(e.target.value)} className="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-lblue focus:border-transparent" onKeyDown={(e) => { if (e.key === 'Enter') { handleSaveEdit(option.id, type); } else if (e.key === 'Escape') { handleCancelEdit(); } }} autoFocus /> ) : ( <> {option.value} )}
)) )}
{/* Formulaire d'ajout */}
setNewValue({ ...newValue, [type]: e.target.value })} placeholder={`Ajouter une nouvelle ${label.toLowerCase()}`} className="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-lblue focus:border-transparent" onKeyDown={(e) => { if (e.key === 'Enter') { handleAdd(type); } }} />
); }; const getUserInitials = () => { if (user?.name) { const names = user.name.split(' '); if (names.length >= 2) { return `${names[0].charAt(0)}${names[1].charAt(0)}`.toUpperCase(); } return user.name.charAt(0).toUpperCase(); } return user?.email?.charAt(0).toUpperCase() || 'U'; }; const router = useRouter(); return (

Paramètres

Gérez votre profil et configurez la plateforme

{/* Colonne de gauche - Profil/Compte */}
{getUserInitials()}

{user?.name || 'Utilisateur'}

{user?.email}

{/* Colonne de droite - Configuration de la plateforme (visible uniquement pour Admin) */} {(user?.role?.name === 'Admin' || user?.role?.name === 'Administrateur') && (
)}
); }