import React, { useState, useEffect } from 'react'; import { api } from '../stores/api'; import PageHeader from '../components/PageHeader'; import SuccessCheck from '../components/SuccessCheck'; export default function ConfigRepartition() { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [success, setSuccess] = useState(false); const [error, setError] = useState(''); useEffect(() => { api.get('/config-repartition/') .then(setItems) .catch(() => {}) .finally(() => setLoading(false)); }, []); const total = items.reduce((sum, i) => sum + (parseFloat(i.pourcentage) || 0), 0); const updatePct = (idx, val) => { const updated = [...items]; updated[idx] = { ...updated[idx], pourcentage: val }; setItems(updated); }; const handleSave = async () => { if (Math.abs(total - 100) > 0.01) { setError(`Le total doit être 100%. Actuellement: ${total.toFixed(2)}%`); return; } setSaving(true); setError(''); try { await api.request('PUT', '/config-repartition/', { items: items.map((i) => ({ destination: i.destination, pourcentage: parseFloat(i.pourcentage), })), }); setSuccess(true); } catch (e) { setError(e.message); setSaving(false); } }; if (success) return ; return (
{loading &&

Chargement...

}

💡 Répartition recommandée : District 30%, Région 30%, BSG 30%, Intergroupe 10%. Le total doit toujours être 100%.

{items.map((item, idx) => (

{item.destination_label}

depuis {item.actif_depuis}

updatePct(idx, parseFloat(e.target.value))} className="flex-1 accent-aa-blue" />
updatePct(idx, e.target.value)} className="w-full p-2 bg-gray-50 rounded-xl text-center font-bold border-0 focus:ring-2 focus:ring-aa-blue" /> %
))} {/* Total */} {items.length > 0 && (
Total : {total.toFixed(1)}% {Math.abs(total - 100) >= 0.01 && (

Le total doit être exactement 100%

)}
)} {error &&

{error}

}
); }