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);
});
}
// ── Sélection de masse ──
function toggleSelectAll(source) {
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
document.getElementById('bulk-actions-bar').style.display = source.checked ? 'flex' : 'none';
}
// 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 :
async function deleteSingleFilm(id) {
if (!confirm('Supprimer ce film ?')) return;
const token = localStorage.getItem('token');
const res = await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
method: 'DELETE',
headers: { 'Authorization': token } // <-- Vérifiez que ce token est bien présent
});
const data = await res.json();
console.log("Réponse suppression simple :", data);
loadDashboardData();
}
async function executeBulkDelete() {
const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value);
if (ids.length === 0) return;
if (!confirm(`Supprimer ces ${ids.length} films ?`)) return;
const token = localStorage.getItem('token');
const res = await fetch(`${API_URL}?action=bulk_delete`, {
method: 'POST',
headers: {
'Authorization': token, // <-- Vérifiez que ce token est bien présent
'Content-Type': 'application/json'
},
body: JSON.stringify({ ids, type: currentAdminTab })
});
const data = await res.json();
console.log("Réponse suppression masse :", data);
document.getElementById('bulk-actions-bar').style.display = 'none';
loadDashboardData();
}
function switchAdminTab(tabName) {
currentAdminTab = tabName;
document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
renderAdminTable();
}