import React, { useEffect, useState } from 'react'; import { api } from '../stores/api'; import { useStore } from '../stores/store'; import PageHeader from '../components/PageHeader'; import { dateHeureReunionIso, datesReunion, dateLocale, labelDateReunion } from '../utils/datesReunion'; const TYPES_JETONS = [['desir', 'Désir'], ['24h', '24 heures'], ['1mois', '1 mois'], ['2mois', '2 mois'], ['3mois', '3 mois'], ['6mois', '6 mois'], ['9mois', '9 mois'], ['1an', '1 an'], ['multiple', 'Multiple']]; const INPUT = 'w-full p-3 bg-gray-50 rounded-xl border-0 focus:ring-2 focus:ring-aa-blue'; export default function ReunionHebdomadaire() { const { postes, groupe } = useStore(); const estExecutif = postes.some((p) => ['RSG', 'RSG Substitut(e)', 'Trésorier(ière)', 'Secrétaire'].includes(p)); const choixDates = datesReunion(groupe, { joursAvant: 365, joursApres: 180 }); const [membres, setMembres] = useState([]); const [catalogue, setCatalogue] = useState([]); const [reunions, setReunions] = useState([]); const [dateSelection, setDateSelection] = useState(dateLocale()); const [dateReunion, setDateReunion] = useState(dateLocale()); const [theme, setTheme] = useState(''); const [annonces, setAnnonces] = useState(''); const [note, setNote] = useState(''); const [collecteMontant, setCollecteMontant] = useState(''); const [detenteurId, setDetenteurId] = useState(''); const [jetonType, setJetonType] = useState(''); const [jetonMembreId, setJetonMembreId] = useState(''); const [jetonAnnees, setJetonAnnees] = useState(''); const [litteratureId, setLitteratureId] = useState(''); const [quantite, setQuantite] = useState('1'); const [venteMontant, setVenteMontant] = useState(''); const [msg, setMsg] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const charger = async () => { try { const [m, l, r] = await Promise.all([ api.get('/membres/actifs'), api.get('/litterature/'), api.get('/reunions/?limite=80'), ]); setMembres(m); setCatalogue(l); setReunions(r.filter((x) => x.type === 'reguliere')); if (m.length > 0 && !detenteurId) setDetenteurId(m[0].id); } catch (e) { setError(e.message); } }; useEffect(() => { charger(); }, []); useEffect(() => { if (choixDates.length > 0) { if (!choixDates.includes(dateSelection)) setDateSelection(choixDates.find((d) => d >= dateLocale()) || choixDates[choixDates.length - 1]); if (!choixDates.includes(dateReunion)) setDateReunion(choixDates.find((d) => d >= dateLocale()) || choixDates[choixDates.length - 1]); } }, [groupe?.jour_reunion]); const rapport = reunions.find((r) => r.date_reunion?.slice(0, 10) === dateSelection); const dateSimple = () => dateReunion; const choisirLitterature = (id) => { setLitteratureId(id); const item = catalogue.find((c) => c.id === id); if (item?.prix_coutant) setVenteMontant((item.prix_coutant * parseInt(quantite || '1')).toFixed(2)); }; const changerQuantite = (valeur) => { setQuantite(valeur); const item = catalogue.find((c) => c.id === litteratureId); if (item?.prix_coutant && valeur) setVenteMontant((item.prix_coutant * parseInt(valeur)).toFixed(2)); }; const imprimer = () => window.print(); const enregistrer = async () => { setError(''); setMsg(''); setLoading(true); const faits = []; try { if (theme.trim() || annonces.trim() || note.trim()) { const annoncesList = annonces.split('\n').map((a) => a.trim()).filter(Boolean); await api.post('/reunions/', { type: 'reguliere', date_reunion: dateHeureReunionIso(dateReunion, groupe), theme: theme.trim() || 'Rencontre hebdomadaire', annonces: annoncesList.length ? annoncesList : null, note: note.trim() || null, }); faits.push('rapport'); } if (collecteMontant) { await api.post('/collectes/', { date_collecte: dateSimple(), montant: parseFloat(collecteMontant), detenteur_id: detenteurId }); faits.push('collecte'); } if (jetonType) { const body = { type_jeton: jetonType, date_remise: dateSimple() }; if (jetonMembreId) body.membre_id = jetonMembreId; if (jetonType === 'multiple') body.annees = parseInt(jetonAnnees); await api.post('/jetons/', body); faits.push('jeton'); } if (litteratureId) { await api.post('/ventes-litterature/', { litterature_id: litteratureId, quantite: parseInt(quantite), montant: parseFloat(venteMontant), date_vente: dateSimple() }); faits.push('littérature'); } if (faits.length === 0) setError('Remplissez au moins une section'); else { setMsg(`Enregistré : ${faits.join(', ')}`); setDateSelection(dateSimple()); await charger(); } } catch (e) { setError(e.message); } setLoading(false); }; return (
{dateSelection}
{rapport ? (Thème : {rapport.theme || '____________________________'}
{"\n\n\n"}
}Notes : {rapport.note || '____________________________________________'}
Thème : ____________________________________________
Annonces :
Notes :
Collecte : __________ $
Détenteur : _______________
Jeton : __________________
Littérature : __________ $