import React, { useState, useEffect } from 'react'; import { api } from '../stores/api'; import PageHeader from '../components/PageHeader'; const CATEGORIES = [ { value: 'executif', label: 'Exécutif' }, { value: 'service', label: 'Service' }, { value: 'physique', label: 'Physique' }, ]; export default function GererPostes() { const [postes, setPostes] = useState([]); const [taches, setTaches] = useState([]); const [candidatures, setCandidatures] = useState([]); const [tab, setTab] = useState('postes'); const [showForm, setShowForm] = useState(false); const [showTacheForm, setShowTacheForm] = useState(false); const [msg, setMsg] = useState(''); const [error, setError] = useState(''); // Form poste const [nom, setNom] = useState(''); const [categorie, setCategorie] = useState('service'); const [description, setDescription] = useState(''); const [prerequis, setPrerequis] = useState(''); const [informations, setInformations] = useState(''); // Form tache const [tacheNom, setTacheNom] = useState(''); const [tacheInfo, setTacheInfo] = useState(''); // Affectation tache const [affectPosteId, setAffectPosteId] = useState(''); const [affectTacheId, setAffectTacheId] = useState(''); const charger = async () => { try { const [p, t, c] = await Promise.all([ api.get('/postes/?inclure_abolis=true'), api.get('/postes/taches'), api.get('/postes/candidatures'), ]); setPostes(p); setTaches(t); setCandidatures(c); } catch (e) { setError(e.message); } }; useEffect(() => { charger(); }, []); const flash = (m) => { setMsg(m); setTimeout(() => setMsg(''), 2500); }; const creerPoste = async () => { if (!nom.trim()) { setError('Nom requis'); return; } setError(''); try { await api.post('/postes/', { nom: nom.trim(), categorie, description: description.trim() || null, prerequis: prerequis.trim() || null, informations: informations.trim() || null, }); setNom(''); setDescription(''); setPrerequis(''); setInformations(''); setShowForm(false); flash('Poste créé ✓'); await charger(); } catch (e) { setError(e.message); } }; const abolir = async (id, nomPoste) => { if (!confirm(`Abolir le poste «${nomPoste}» ?\nTous les mandats actifs seront terminés.`)) return; try { const res = await api.request('DELETE', `/postes/${id}`); flash(`Poste aboli — ${res.mandats_termines} mandat(s) terminé(s)`); await charger(); } catch (e) { setError(e.message); } }; const reactiver = async (id) => { try { await api.post(`/postes/${id}/reactiver`); flash('Poste réactivé ✓'); await charger(); } catch (e) { setError(e.message); } }; const supprimerPermanent = async (id, nomPoste) => { if (!confirm(`Supprimer DÉFINITIVEMENT le poste «${nomPoste}» ?\nCette action est irréversible.`)) return; try { await api.request('DELETE', `/postes/${id}/permanent`); flash('Poste supprimé définitivement'); await charger(); } catch (e) { setError(e.message); } }; const creerTache = async () => { if (!tacheNom.trim()) { setError('Nom requis'); return; } try { await api.post('/postes/taches', { nom: tacheNom.trim(), informations: tacheInfo.trim() || null }); setTacheNom(''); setTacheInfo(''); setShowTacheForm(false); flash('Tâche créée ✓'); await charger(); } catch (e) { setError(e.message); } }; const supprimerTache = async (id, nomT) => { if (!confirm(`Supprimer la tâche «${nomT}» ?`)) return; try { await api.request('DELETE', `/postes/taches/${id}`); flash('Tâche supprimée'); await charger(); } catch (e) { setError(e.message); } }; const affecterTache = async () => { if (!affectPosteId || !affectTacheId) { setError('Sélectionnez un poste et une tâche'); return; } try { await api.post(`/postes/${affectPosteId}/taches/${affectTacheId}`); setAffectTacheId(''); flash('Tâche affectée ✓'); await charger(); } catch (e) { setError(e.message); } }; const desaffecterTache = async (posteId, tacheId) => { try { await api.request('DELETE', `/postes/${posteId}/taches/${tacheId}`); flash('Tâche retirée'); await charger(); } catch (e) { setError(e.message); } }; const traiterCandidature = async (id, statut) => { try { await api.patch(`/postes/candidatures/${id}?statut=${statut}`); flash(statut === 'acceptee' ? 'Candidature acceptée ✓' : 'Candidature refusée'); await charger(); } catch (e) { setError(e.message); } }; const postesActifs = postes.filter((p) => p.actif); const CAT_ICON = { executif: '🔵', service: '🟣', physique: '🟢' }; return (
{CAT_ICON[p.categorie] || '⚪'} {p.nom}
{p.description &&{p.description}
} {p.informations &&{p.informations}
} {/* Tâches associées */} {p.taches && p.taches.length > 0 && (Affecter une tâche à un poste
🔧 {t.nom}
{t.informations &&{t.informations}
} {t.postes.length > 0 && (→ {t.postes.join(', ')}
)}🙋
Aucune candidature en attente
{c.membre_prenom} → {c.poste_nom}
{c.motivation &&«{c.motivation}»
}{new Date(c.cree_le).toLocaleDateString('fr-CA')}