From d8a8d9b835087df245347de22ccb3b0239f94bbe Mon Sep 17 00:00:00 2001 From: Cedric Date: Mon, 22 Jun 2026 13:58:09 +0200 Subject: [PATCH] Actualiser js/admin.js --- js/admin.js | 473 +++++++++++----------------------------------------- 1 file changed, 101 insertions(+), 372 deletions(-) diff --git a/js/admin.js b/js/admin.js index cd3170d..13d483d 100644 --- a/js/admin.js +++ b/js/admin.js @@ -17,7 +17,6 @@ function getStarsHTML(rating) { return html; } -// ── PARSER CSV RENFORCÉ ── function parseCSV(text) { if (text.charCodeAt(0) === 0xFEFF) text = text.slice(1); const rows = []; @@ -47,9 +46,7 @@ function parseCSV(text) { for (let i = 1; i < rows.length; i++) { if (rows[i].length === 1 && rows[i][0].trim() === '') continue; const obj = {}; - headers.forEach((h, idx) => { - obj[h] = rows[i][idx] !== undefined ? rows[i][idx] : ''; - }); + headers.forEach((h, idx) => { obj[h] = rows[i][idx] !== undefined ? rows[i][idx] : ''; }); data.push(obj); } return data; @@ -70,15 +67,22 @@ document.addEventListener('DOMContentLoaded', () => { function initEventListeners() { const filmForm = document.getElementById('film-form'); if (filmForm) filmForm.addEventListener('submit', saveFilmForm); + + // 🔥 DISPATCHER D'IMPORT : Appelle la bonne fonction selon l'onglet actif const csvInput = document.getElementById('csv-file'); - if (csvInput) csvInput.addEventListener('change', (e) => handleCsvUpload(e.target)); + if (csvInput) { + csvInput.addEventListener('change', (e) => { + if (currentAdminTab === 'critique') { + handleCritiqueUpload(e.target); + } else { + handleVideothequeUpload(e.target); + } + }); + } const searchInput = document.getElementById('search-input'); if (searchInput) { - searchInput.addEventListener('input', () => { - currentPage = 1; - renderAdminTable(); - }); + searchInput.addEventListener('input', () => { currentPage = 1; renderAdminTable(); }); } const selectAll = document.getElementById('select-all-checkbox'); @@ -89,17 +93,12 @@ function initEventListeners() { const overlay = e.target.closest('.overlay'); if (overlay) overlay.classList.remove('open'); } - if (e.target.classList.contains('overlay')) { - e.target.classList.remove('open'); - } + if (e.target.classList.contains('overlay')) e.target.classList.remove('open'); }); const physicalFilter = document.getElementById('admin-physical-checkbox'); if (physicalFilter) { - physicalFilter.addEventListener('change', () => { - currentPage = 1; - renderAdminTable(); - }); + physicalFilter.addEventListener('change', () => { currentPage = 1; renderAdminTable(); }); } } @@ -148,12 +147,8 @@ function renderAdminTable() { const tr = document.createElement('tr'); const isChecked = selectedIds.has(String(f.id)) ? 'checked' : ''; tr.innerHTML = ` - - - - - ${f.poster ? `Affiche` : '
'} - + + ${f.poster ? `Affiche` : '
'} ${f.title} ${f.year || '-'} ${f.director || '-'} @@ -168,34 +163,21 @@ function renderAdminTable() { }); renderPagination(totalPages, filtered.length); - const selectAll = document.getElementById('select-all-checkbox'); - if (selectAll) { - selectAll.checked = pageItems.length > 0 && pageItems.every(f => selectedIds.has(String(f.id))); - } + if (selectAll) selectAll.checked = pageItems.length > 0 && pageItems.every(f => selectedIds.has(String(f.id))); } function toggleSingleSelect(id, checkbox) { if (checkbox.checked) selectedIds.add(String(id)); else selectedIds.delete(String(id)); updateBulkBar(); - const filtered = allItems.filter(item => item.type === currentAdminTab); - const selectAll = document.getElementById('select-all-checkbox'); - if (selectAll) { - selectAll.checked = filtered.length > 0 && filtered.every(f => selectedIds.has(String(f.id))); - } } function toggleSelectAll(source) { 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))); - } - document.querySelectorAll('.film-checkbox').forEach(cb => { - cb.checked = selectedIds.has(cb.value); - }); + if (source.checked) filtered.forEach(f => selectedIds.add(String(f.id))); + else filtered.forEach(f => selectedIds.delete(String(f.id))); + document.querySelectorAll('.film-checkbox').forEach(cb => { cb.checked = selectedIds.has(cb.value); }); updateBulkBar(); } @@ -216,10 +198,7 @@ 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; - } + if (totalItems === 0) { container.innerHTML = '

Aucun élément trouvé.

'; return; } if (totalPages <= 1) return; const info = document.createElement('span'); @@ -233,22 +212,12 @@ function renderPagination(totalPages, totalItems) { prevBtn.onclick = () => { currentPage--; renderAdminTable(); }; container.appendChild(prevBtn); - 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)); + for (let i = 1; i <= totalPages; i++) { + const btn = document.createElement('button'); + btn.textContent = i; + if (i === currentPage) btn.classList.add('active'); + btn.onclick = () => { currentPage = i; renderAdminTable(); }; + container.appendChild(btn); } const nextBtn = document.createElement('button'); @@ -258,70 +227,28 @@ function renderPagination(totalPages, totalItems) { 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; -} - -function showConfirmModal(actionFn) { - pendingDeleteAction = actionFn; - const modal = document.getElementById('confirm-modal'); - if (modal) modal.classList.add('open'); -} - -function closeConfirmModal() { - const modal = document.getElementById('confirm-modal'); - if (modal) modal.classList.remove('open'); - pendingDeleteAction = null; -} +function showConfirmModal(actionFn) { pendingDeleteAction = actionFn; document.getElementById('confirm-modal')?.classList.add('open'); } +function closeConfirmModal() { document.getElementById('confirm-modal')?.classList.remove('open'); pendingDeleteAction = null; } async function executeBulkDelete() { 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' }, - body: JSON.stringify({ ids, type: currentAdminTab }) - }); - if (!res.ok) throw new Error("Erreur serveur."); + await fetch(`${API_URL}?action=bulk_delete`, { method: 'POST', headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, body: JSON.stringify({ ids, type: currentAdminTab }) }); selectedIds.clear(); document.getElementById('bulk-actions-bar').style.display = 'none'; - const selectAll = document.getElementById('select-all-checkbox'); - if (selectAll) selectAll.checked = false; loadDashboardData(); - } catch (err) { - console.error('Erreur bulk delete :', err); - alert("Une erreur est survenue."); - } + } catch (err) { alert("Erreur serveur."); } }); } async function deleteSingleFilm(id) { showConfirmModal(async () => { try { - const res = await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { - method: 'DELETE', - headers: { 'Authorization': localStorage.getItem('token') } - }); - if (!res.ok) throw new Error("Erreur serveur."); + await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { method: 'DELETE', headers: { 'Authorization': localStorage.getItem('token') } }); loadDashboardData(); - } catch (err) { - console.error('Erreur delete :', err); - alert("Une erreur est survenue."); - } + } catch (err) { alert("Erreur serveur."); } }); } @@ -338,28 +265,20 @@ function switchAdminTab(tabName) { selectedIds.clear(); const searchInput = document.getElementById('search-input'); if (searchInput) searchInput.value = ''; + const physicalFilter = document.getElementById('admin-physical-checkbox'); if (physicalFilter) { - physicalFilter.checked = false; // On réinitialise le filtre à chaque changement d'onglet - - // On cherche le conteneur parent (le