diff --git a/js/admin.js b/js/admin.js index 74cc682..b7417ec 100644 --- a/js/admin.js +++ b/js/admin.js @@ -80,6 +80,14 @@ document.addEventListener('DOMContentLoaded', () => { }); function initEventListeners() { + const physicalCheckbox = document.getElementById('admin-physical-checkbox'); + if (physicalCheckbox) { + physicalCheckbox.addEventListener('change', (e) => { + adminPhysicalOnlyFilter = e.target.checked; + currentPage = 1; + renderAdminTable(); + }); + } const filmForm = document.getElementById('film-form'); if (filmForm) filmForm.addEventListener('submit', saveFilmForm); @@ -137,70 +145,117 @@ function getFilteredItems() { return filtered; } -// ── RENDU DU TABLEAU (6 COLONNES) ── -// ── RENDU DU TABLEAU (7 COLONNES ALIGNÉES) ── +// ── RENDU DU TABLEAU (VERSION COMPLÈTE ET CORRIGÉE) ── function renderAdminTable() { const tbody = document.getElementById('admin-table-body'); if (!tbody) return; tbody.innerHTML = ''; + // 1. Récupération des valeurs de filtrage const searchInput = document.getElementById('search-input'); - const currentSearch = searchInput ? searchInput.value.toLowerCase() : ''; + const query = searchInput ? searchInput.value.toLowerCase().trim() : ''; + + const physicalCheckbox = document.getElementById('admin-physical-checkbox'); + const isPhysicalOnly = physicalCheckbox ? physicalCheckbox.checked : false; + // 2. Filtrage des données let filtered = allItems.filter(item => item.type === currentAdminTab); - // FILTRE PHYSIQUE UNIQUEMENT - if (adminPhysicalOnlyFilter) { - filtered = filtered.filter(f => - !f.streaming || - f.streaming === 'Disponible en support physique ou Cinéma' || - f.streaming.trim() === '' + // Filtre support physique / cinéma uniquement + if (isPhysicalOnly) { + filtered = filtered.filter(item => { + const streaming = item.streaming ? item.streaming.trim() : ''; + return !streaming || streaming === 'Disponible en support physique ou Cinéma'; + }); + } + + // Filtre recherche + if (query) { + filtered = filtered.filter(item => + item.title.toLowerCase().includes(query) || + (item.director && item.director.toLowerCase().includes(query)) ); } - - const filtered = getFilteredItems(); + // 3. Mise à jour du compteur const countLabel = document.getElementById('admin-count-label'); - if(countLabel) countLabel.textContent = `${filtered.length} élément(s)`; + if (countLabel) countLabel.textContent = `${filtered.length} élément(s)`; + // 4. Logique de pagination const totalPages = Math.ceil(filtered.length / itemsPerPage) || 1; if (currentPage > totalPages) currentPage = totalPages; - const startIdx = (currentPage - 1) * itemsPerPage; - const pageItems = filtered.slice(startIdx, startIdx + itemsPerPage); + const startIndex = (currentPage - 1) * itemsPerPage; + const pageItems = filtered.slice(startIndex, startIndex + itemsPerPage); - pageItems.forEach(f => { + // 5. Génération des lignes (6 colonnes : Checkbox, Titre, Année, Réalisateur, Info, Actions) + pageItems.forEach(item => { const tr = document.createElement('tr'); - const isChecked = selectedIds.has(String(f.id)) ? 'checked' : ''; + const isChecked = selectedIds.has(String(item.id)) ? 'checked' : ''; + const infoHTML = currentAdminTab === 'critique' + ? `${getStarsHTML(item.rating)}` + : `${item.format || '-'}`; + tr.innerHTML = ` - + - - ${f.poster ? `Affiche` : '
'} - - ${f.title} - ${f.year || '-'} - ${f.director || '-'} - - ${currentAdminTab === 'critique' - ? `${getStarsHTML(f.rating)} ${parseFloat(f.rating || 0)}/5` - : `${f.format || '-'}`} - - + ${item.title} + ${item.year || '-'} + ${item.director || '-'} + ${infoHTML} +
- - + +
- `; + + `; tbody.appendChild(tr); }); + // 6. Rendu de la pagination renderPagination(totalPages, filtered.length); - + + // 7. Synchronisation de la case "Tout sélectionner" + syncSelectAllCheckbox(); +} + +// ── GESTION DES CHECKBOXES INDIVIDUELLES ─ +function handleFilmCheckbox(checkbox) { + const id = checkbox.value; + if (checkbox.checked) { + selectedIds.add(id); + } else { + selectedIds.delete(id); + } + updateBulkBar(); + syncSelectAllCheckbox(); +} + +// ─ SYNCHRONISATION DU "TOUT SÉLECTIONNER" ── +function syncSelectAllCheckbox() { const selectAll = document.getElementById('select-all-checkbox'); - if (selectAll) { - selectAll.checked = pageItems.length > 0 && pageItems.every(f => selectedIds.has(String(f.id))); + if (!selectAll) return; + + const visibleCheckboxes = document.querySelectorAll('#admin-table-body .film-checkbox'); + const allChecked = visibleCheckboxes.length > 0 && Array.from(visibleCheckboxes).every(cb => cb.checked); + + selectAll.checked = allChecked; +} + +// ── BARRE D'ACTIONS GROUPÉES ── +function updateBulkBar() { + const bulkBar = document.getElementById('bulk-actions-bar'); + const bulkCount = document.getElementById('bulk-count'); + + if (!bulkBar || !bulkCount) return; + + if (selectedIds.size > 0) { + bulkBar.style.display = 'flex'; + bulkCount.textContent = selectedIds.size; + } else { + bulkBar.style.display = 'none'; } }