import React, { useState, useEffect } from 'react'; import { api } from '../stores/api'; import PageHeader from '../components/PageHeader'; import SuccessCheck from '../components/SuccessCheck'; const CATEGORIES = [ { value: 'livre', label: 'Livre', emoji: '📕' }, { value: 'brochure', label: 'Brochure', emoji: '📄' }, { value: 'depliant', label: 'Dépliant', emoji: '📃' }, { value: 'pamphlet', label: 'Pamphlet', emoji: '📑' }, ]; export default function AjusterInventaire() { const [items, setItems] = useState([]); const [jetons, setJetons] = useState([]); const [loading, setLoading] = useState(true); const [success, setSuccess] = useState(''); const [error, setError] = useState(''); const [showAjout, setShowAjout] = useState(false); const charger = async () => { try { const [data, stockJetons] = await Promise.all([ api.get('/litterature/'), api.get('/commandes-jetons/stock'), ]); setItems(data); setJetons(stockJetons); } catch { setItems([]); setJetons([]); } setLoading(false); }; useEffect(() => { charger(); }, []); const ajusterStock = async (itemId, quantite) => { setError(''); try { const item = items.find((i) => i.id === itemId); await api.patch(`/litterature/${itemId}/stock`, { quantite }, { version: item?.modifie_le }); setSuccess('Stock ajusté ✓'); setTimeout(() => { setSuccess(''); charger(); }, 1000); } catch (e) { setError(e.message); } }; if (success) return ; return (

Jetons

Littérature

{/* Bouton ajouter un titre */} {showAjout && { setShowAjout(false); charger(); }} />} {error &&

{error}

} {loading &&

Chargement...

} {/* Liste des titres avec ajustement */} {items.map((item) => ( ))} {!loading && items.length === 0 && (
📦

Aucun titre dans le catalogue

)}
); } function StockJetons({ stock, loading }) { const aCommander = stock.filter((s) => s.stock_suggestion !== 'OK'); if (loading) return null; return (
{aCommander.length > 0 && (

Jetons à commander ({aCommander.length})

{aCommander.map((s) => (

{s.label} — {s.remis_dernier_mois} remis ce mois

))}
)} {stock.map((s) => (
{s.label.split(' ')[0]}

{s.label.split(' ').slice(1).join(' ')}

{s.remis_dernier_mois} remis ou vendu récemment

{s.stock_suggestion}
))} {stock.length === 0 && (
🏅

Aucune donnée de jetons pour le moment

)}
); } function ItemStock({ item, onAjuster }) { const [ajustement, setAjustement] = useState(''); return (

{item.titre}

{item.categorie}

{item.quantite_stock}

en stock

{/* Ajustement rapide */}
setAjustement(e.target.value)} className="flex-1 p-2 bg-gray-50 rounded-xl text-center border-0 focus:ring-2 focus:ring-purple-400" />
); } function FormAjoutTitre({ onSuccess }) { const [form, setForm] = useState({ titre: '', categorie: 'livre', prix_coutant: '', quantite_stock: '0', code_bsg: '', }); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const update = (k, v) => setForm({ ...form, [k]: v }); const handleSubmit = async () => { if (!form.titre.trim()) { setError('Le titre est obligatoire'); return; } setLoading(true); setError(''); try { await api.post('/litterature/', { titre: form.titre.trim(), categorie: form.categorie, prix_coutant: form.prix_coutant ? parseFloat(form.prix_coutant) : null, quantite_stock: parseInt(form.quantite_stock) || 0, code_bsg: form.code_bsg.trim() || null, }); onSuccess(); } catch (e) { setError(e.message); setLoading(false); } }; return (
update('titre', e.target.value)} className="w-full p-3 bg-white rounded-xl border-0 focus:ring-2 focus:ring-purple-400" autoFocus />
{CATEGORIES.map((c) => ( ))}
update('prix_coutant', e.target.value)} className="w-full p-2 bg-white rounded-xl border-0 focus:ring-2 focus:ring-purple-400 text-center" />
update('quantite_stock', e.target.value)} className="w-full p-2 bg-white rounded-xl border-0 focus:ring-2 focus:ring-purple-400 text-center" />
update('code_bsg', e.target.value)} className="w-full p-2 bg-white rounded-xl border-0 focus:ring-2 focus:ring-purple-400" /> {error &&

{error}

}
); }