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

114 lines
4.0 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 :
// 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();
}