Files
mon-petit-cinema/js/admin.js
T
2026-06-18 13:36:51 +02:00

113 lines
3.9 KiB
JavaScript

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 = `
<td><input type="checkbox" class="film-checkbox" value="${f.id}"></td>
<td>${f.poster ? `<img src="${f.poster}" class="thumb" width="40">` : '-'}</td>
<td><strong>${f.title}</strong></td>
<td>${f.year || ''}</td>
<td>${f.director || ''}</td>
<td>${currentAdminTab === 'critique' ? (f.rating ? '★'.repeat(f.rating) : '☆') : (f.format || '-')}</td>
<td>
<button onclick="deleteSingleFilm('${f.id}')">Supprimer</button>
</td>`;
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 :
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();
}
// 3. Suppression
async function executeBulkDelete() {
const checked = document.querySelectorAll('.film-checkbox:checked');
const ids = Array.from(checked).map(cb => cb.value);
if (ids.length === 0) return;
if (!confirm(`Supprimer ces ${ids.length} films ?`)) return;
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: ids, type: currentAdminTab })
});
const data = await res.json();
if (data.success) {
document.getElementById('bulk-actions-bar').style.display = 'none';
loadDashboardData();
}
} catch (err) {
console.error('Erreur bulk delete :', err);
}
}
function switchAdminTab(tabName) {
currentAdminTab = tabName;
document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
renderAdminTable();
}