diff --git a/js/admin.js b/js/admin.js index b9ad0ad..4ceebbe 100644 --- a/js/admin.js +++ b/js/admin.js @@ -84,25 +84,22 @@ function renderAdminTable() { const startIdx = (currentPage - 1) * itemsPerPage; const pageItems = filtered.slice(startIdx, startIdx + itemsPerPage); - // 3. Rendu — 7 cellules pour correspondre aux 7 + // 3. Rendu pageItems.forEach(f => { const tr = document.createElement('tr'); let infoHtml = ''; if (currentAdminTab === 'critique') { - // 🌟 Gestion précise des notes Letterboxd (0.5 à 5) const rating = parseFloat(f.rating) || 0; - const fullStars = Math.floor(rating); // partie entière - const emptyStars = 5 - Math.ceil(rating); // étoiles vides + const fullStars = Math.floor(rating); + const emptyStars = 5 - Math.ceil(rating); const stars = '★'.repeat(fullStars) + '☆'.repeat(emptyStars); - infoHtml = `
${stars} ${rating.toFixed(1)}
`; - // Badge streaming if (f.streaming && f.streaming !== 'Disponible en support physique ou Cinéma') { infoHtml += `${f.streaming}`; } else { @@ -113,10 +110,9 @@ function renderAdminTable() { if(f.length) infoHtml += `${f.length}`; } - // ✅ 7 cellules pour 7 colonnes tr.innerHTML = ` - + ${f.poster ? `Affiche` : '
'} @@ -132,9 +128,31 @@ function renderAdminTable() { tbody.appendChild(tr); }); + // ✅ Synchronise les checkboxes avec selectedIds après rendu + document.querySelectorAll('.film-checkbox').forEach(cb => { + cb.checked = selectedIds.has(cb.value); + }); + renderPagination(totalPages, filtered.length); } +// ── SÉLECTION INDIVIDUELLE ── +function toggleSingleSelect(id, checkbox) { + if (checkbox.checked) { + selectedIds.add(String(id)); + } else { + selectedIds.delete(String(id)); + } + updateBulkBar(); + + // Met à jour le checkbox "select all" + const selectAll = document.getElementById('select-all-checkbox'); + const filtered = allItems.filter(item => item.type === currentAdminTab); + if (selectAll) { + selectAll.checked = filtered.length > 0 && filtered.every(f => selectedIds.has(String(f.id))); + } +} + // ── FONCTIONS DE PAGINATION ── function renderPagination(totalPages, totalItems) { const container = document.getElementById('pagination-container'); @@ -205,23 +223,37 @@ function createEllipsis() { return span; } -// ── GESTION DE MASSE ── +// ── GESTION DE MASSE AMÉLIORÉE ─ +let selectedIds = new Set(); // Stocke tous les IDs sélectionnés (toutes pages) + function toggleSelectAll(source) { - document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked); + // Sélectionne tous les films de la catégorie actuelle (pas juste la page) + const filtered = allItems.filter(item => item.type === currentAdminTab); + + if (source.checked) { + filtered.forEach(f => selectedIds.add(String(f.id))); + } else { + filtered.forEach(f => selectedIds.delete(String(f.id))); + } + + // Met à jour visuellement les checkboxes de la page courante + document.querySelectorAll('.film-checkbox').forEach(cb => { + cb.checked = selectedIds.has(cb.value); + }); + updateBulkBar(); } function updateBulkBar() { - const checked = document.querySelectorAll('.film-checkbox:checked'); const bulkBar = document.getElementById('bulk-actions-bar'); const bulkCount = document.getElementById('bulk-count'); + const selectAll = document.getElementById('select-all-checkbox'); - if (checked.length > 0) { + if (selectedIds.size > 0) { if(bulkBar) bulkBar.style.display = 'flex'; - if(bulkCount) bulkCount.textContent = checked.length; + if(bulkCount) bulkCount.textContent = selectedIds.size; } else { if(bulkBar) bulkBar.style.display = 'none'; - const selectAll = document.getElementById('select-all-checkbox'); if(selectAll) selectAll.checked = false; } } @@ -243,18 +275,22 @@ function closeConfirmModal() { // ── ACTIONS CRUD (SUPPRESSION SÉCURISÉE) ── async function executeBulkDelete() { - const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value); + const ids = Array.from(selectedIds); if (ids.length === 0) return; - + showConfirmModal(async () => { try { const res = await fetch(`${API_URL}?action=bulk_delete`, { method: 'POST', - headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, + headers: { + 'Authorization': localStorage.getItem('token'), + 'Content-Type': 'application/json' + }, body: JSON.stringify({ ids, type: currentAdminTab }) }); if (!res.ok) throw new Error("Erreur serveur lors de la suppression multiple."); + selectedIds.clear(); document.getElementById('bulk-actions-bar').style.display = 'none'; const selectAll = document.getElementById('select-all-checkbox'); if(selectAll) selectAll.checked = false;