diff --git a/js/admin.js b/js/admin.js index 902d137..485ba92 100644 --- a/js/admin.js +++ b/js/admin.js @@ -35,6 +35,14 @@ function initEventListeners() { e.target.classList.remove('open'); } }); + const searchInput = document.getElementById('search-input'); + if (searchInput) { + searchInput.addEventListener('input', (e) => { + searchQuery = e.target.value; + currentPage = 1; // Reset à la page 1 à chaque recherche + renderAdminTable(); + }); + } } // ── CHARGEMENT DES DONNÉES (Anti-cache) ── @@ -56,32 +64,146 @@ function renderAdminTable() { const tbody = document.getElementById('admin-table-body'); if (!tbody) return; tbody.innerHTML = ''; - const filtered = allItems.filter(item => item.type === currentAdminTab); - + + // 1. Filtrage par onglet et recherche + let filtered = allItems.filter(item => item.type === currentAdminTab); + if (searchQuery) { + const q = searchQuery.toLowerCase(); + filtered = filtered.filter(f => + f.title.toLowerCase().includes(q) || + (f.director && f.director.toLowerCase().includes(q)) + ); + } + const countLabel = document.getElementById('admin-count-label'); if(countLabel) countLabel.textContent = `${filtered.length} élément(s)`; - filtered.forEach(f => { + // 2. Gestion de la 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); + + // 3. Rendu des lignes + pageItems.forEach(f => { const tr = document.createElement('tr'); + + let infoHtml = ''; + if (currentAdminTab === 'critique') { + const rating = Math.round(f.rating || 0); + const stars = '★'.repeat(rating) + '☆'.repeat(5 - rating); + infoHtml = `${stars}`; + + // Affichage Streaming ou Physique + if (f.streaming && f.streaming !== 'Disponible en support physique ou Cinéma') { + infoHtml += `${f.streaming}`; + } else { + infoHtml += `🎟️ Cinéma / Physique`; + } + } else { + infoHtml = `${f.format || '-'}`; + if(f.length) infoHtml += `${f.length}`; + } + tr.innerHTML = ` - + - + ${f.poster ? `Affiche` : '
'} - ${f.title} - ${f.year || '-'} - ${f.director || '-'} - ${currentAdminTab === 'critique' ? `${'★'.repeat(f.rating || 0)}` : `${f.format || '-'}`} -
- - +
+ ${f.title} + ${f.year || '-'}
+ + ${f.director || '-'} + +
+ ${infoHtml} +
+ + + + `; tbody.appendChild(tr); }); + + // 4. Rendu de la pagination + renderPagination(totalPages, filtered.length); +} + +// ── FONCTIONS DE PAGINATION ── +function renderPagination(totalPages, totalItems) { + const container = document.getElementById('pagination-container'); + if (!container) return; + container.innerHTML = ''; + + if (totalItems === 0) { + container.innerHTML = '

Aucun élément trouvé.

'; + return; + } + + // Info + const info = document.createElement('span'); + info.className = 'pagination-info'; + info.textContent = `Page ${currentPage} sur ${totalPages}`; + container.appendChild(info); + + // Prev + const prevBtn = document.createElement('button'); + prevBtn.innerHTML = ''; + prevBtn.disabled = currentPage === 1; + prevBtn.onclick = () => { currentPage--; renderAdminTable(); }; + container.appendChild(prevBtn); + + // Pages (affichage intelligent : 1 ... 4 5 6 ... 10) + const maxButtons = 5; + let startPage = Math.max(1, currentPage - Math.floor(maxButtons / 2)); + let endPage = Math.min(totalPages, startPage + maxButtons - 1); + if (endPage - startPage + 1 < maxButtons) { + startPage = Math.max(1, endPage - maxButtons + 1); + } + + if (startPage > 1) { + container.appendChild(createPageBtn(1)); + if (startPage > 2) container.appendChild(createEllipsis()); + } + + for (let i = startPage; i <= endPage; i++) { + container.appendChild(createPageBtn(i)); + } + + if (endPage < totalPages) { + if (endPage < totalPages - 1) container.appendChild(createEllipsis()); + container.appendChild(createPageBtn(totalPages)); + } + + // Next + const nextBtn = document.createElement('button'); + nextBtn.innerHTML = ''; + nextBtn.disabled = currentPage === totalPages; + nextBtn.onclick = () => { currentPage++; renderAdminTable(); }; + container.appendChild(nextBtn); +} + +function createPageBtn(num) { + const btn = document.createElement('button'); + btn.textContent = num; + if (num === currentPage) btn.classList.add('active'); + btn.onclick = () => { currentPage = num; renderAdminTable(); }; + return btn; +} + +function createEllipsis() { + const span = document.createElement('span'); + span.textContent = '...'; + span.style.color = 'var(--muted)'; + span.style.padding = '0 0.5rem'; + return span; } // ── GESTION DE MASSE ──