const API_URL = '../api.php'; let allItems = []; let currentAdminTab = 'critique'; document.addEventListener('DOMContentLoaded', loadDashboardData); async function loadDashboardData() { const res = await fetch(`${API_URL}?action=get_films`); allItems = await res.json(); renderAdminTable(); } function renderAdminTable() { const tbody = document.getElementById('admin-table-body'); if (!tbody) return; tbody.innerHTML = ''; const filtered = allItems.filter(item => item.type === currentAdminTab); filtered.forEach(f => { const tr = document.createElement('tr'); tr.innerHTML = ` ${f.poster ? `` : '-'} ${f.title} ${f.year || ''} ${f.director || ''} ${currentAdminTab === 'critique' ? (f.rating ? '★'.repeat(f.rating) : '☆') : (f.format || '-')} `; tbody.appendChild(tr); }); } // 1. Sélectionner tout function toggleSelectAll(source) { document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked); updateBulkBar(); } // 2. Mettre à jour la barre (appelée par chaque clic sur une checkbox) function updateBulkBar() { const checked = document.querySelectorAll('.film-checkbox:checked'); const bulkBar = document.getElementById('bulk-actions-bar'); const bulkCount = document.getElementById('bulk-count'); if (checked.length > 0) { bulkBar.style.display = 'flex'; bulkCount.textContent = checked.length; } else { bulkBar.style.display = 'none'; document.getElementById('select-all-checkbox').checked = false; } } // Détection de clic sur une case document.addEventListener('change', (e) => { if (e.target.classList.contains('film-checkbox')) { const count = document.querySelectorAll('.film-checkbox:checked').length; document.getElementById('bulk-actions-bar').style.display = count > 0 ? 'flex' : 'none'; document.getElementById('bulk-count').textContent = count; } }); // Remplacez les deux fonctions de suppression par celles-ci : // Variable pour stocker la fonction à appeler après confirmation let pendingDeleteAction = null; function showConfirmModal(actionFn) { pendingDeleteAction = actionFn; document.getElementById('confirm-modal').classList.add('open'); } function closeConfirmModal() { document.getElementById('confirm-modal').classList.remove('open'); } // Bouton supprimer dans la modale document.getElementById('confirm-btn').addEventListener('click', () => { if (pendingDeleteAction) pendingDeleteAction(); closeConfirmModal(); }); // Mise à jour de la suppression de masse async function executeBulkDelete() { showConfirmModal(async () => { const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value); await fetch(`${API_URL}?action=bulk_delete`, { method: 'POST', headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, body: JSON.stringify({ ids, type: currentAdminTab }) }); loadDashboardData(); document.getElementById('bulk-actions-bar').style.display = 'none'; }); } // Mise à jour de la suppression unitaire async function deleteSingleFilm(id) { showConfirmModal(async () => { await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { method: 'DELETE', headers: { 'Authorization': localStorage.getItem('token') } }); loadDashboardData(); }); } function switchAdminTab(tabName) { currentAdminTab = tabName; document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`)); renderAdminTable(); }