Actualiser js/admin.js

This commit is contained in:
2026-06-18 14:02:45 +02:00
parent 13b8ce70a5
commit 839c7536bc
+114 -25
View File
@@ -1,22 +1,36 @@
const API_URL = '../api.php';
let allItems = [];
let currentAdminTab = 'critique';
let editingItemId = null;
document.addEventListener('DOMContentLoaded', loadDashboardData);
document.addEventListener('DOMContentLoaded', () => {
loadDashboardData();
initModals();
initForms();
});
async function loadDashboardData() {
const res = await fetch(`${API_URL}?action=get_films`);
allItems = await res.json();
renderAdminTable();
try {
const res = await fetch(`${API_URL}?action=get_films`);
if (!res.ok) throw new Error('Erreur de chargement');
allItems = await res.json();
renderAdminTable();
} catch (error) {
console.error("Erreur loadDashboardData:", error);
}
}
function renderAdminTable() {
const tbody = document.getElementById('admin-table-body');
if (!tbody) return;
tbody.innerHTML = '';
const filtered = allItems.filter(item => item.type === currentAdminTab);
document.getElementById('admin-count-label').textContent = `${filtered.length} élément(s)`;
const filtered = allItems.filter(item => item.type === currentAdminTab);
const countLabel = document.getElementById('admin-count-label');
if (countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
// BUG CORRIGÉ : f = > remplacé par f =>
// Nettoyage des espaces parasites dans les balises HTML
filtered.forEach(f => {
const tr = document.createElement('tr');
tr.innerHTML = `
@@ -40,57 +54,48 @@ function renderAdminTable() {
});
}
// 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;
const selectAll = document.getElementById('select-all-checkbox');
if(selectAll) selectAll.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;
updateBulkBar();
}
});
// 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');
const modal = document.getElementById('confirm-modal');
if(modal) modal.classList.add('open');
}
function closeConfirmModal() {
document.getElementById('confirm-modal').classList.remove('open');
const modal = document.getElementById('confirm-modal');
if(modal) modal.classList.remove('open');
}
// Bouton supprimer dans la modale
document.getElementById('confirm-btn').addEventListener('click', () => {
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);
@@ -100,11 +105,10 @@ async function executeBulkDelete() {
body: JSON.stringify({ ids, type: currentAdminTab })
});
loadDashboardData();
document.getElementById('bulk-actions-bar').style.display = 'none';
updateBulkBar();
});
}
// Mise à jour de la suppression unitaire
async function deleteSingleFilm(id) {
showConfirmModal(async () => {
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
@@ -120,3 +124,88 @@ function switchAdminTab(tabName) {
document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
renderAdminTable();
}
// --- AJOUTS FONCTIONNELS MANQUANTS ---
function openEditModal(id) {
const item = allItems.find(i => i.id == id);
if (!item) return;
editingItemId = id;
const modal = document.getElementById('edit-modal');
if (!modal) return;
const setVal = (id, val) => { const el = document.getElementById(id); if (el) el.value = val || ''; };
setVal('film-title', item.title);
setVal('film-year', item.year);
setVal('film-director', item.director);
setVal('film-poster', item.poster);
setVal('film-rating', item.rating);
setVal('film-review', item.review);
setVal('film-streaming', item.streaming);
setVal('film-format', item.format);
setVal('film-length', item.length);
setVal('film-publisher', item.publisher);
setVal('film-aspect-ratio', item.aspect_ratio);
setVal('film-ean', item.ean_isbn13);
setVal('film-discs', item.number_of_discs);
setVal('film-description', item.description);
modal.classList.add('open');
}
function initModals() {
document.querySelectorAll('.modal-close, .btn-cancel').forEach(btn => {
btn.addEventListener('click', (e) => {
const overlay = e.target.closest('.overlay');
if (overlay) overlay.classList.remove('open');
});
});
document.querySelectorAll('.overlay').forEach(overlay => {
overlay.addEventListener('click', (e) => {
if (e.target === overlay) overlay.classList.remove('open');
});
});
}
function initForms() {
const filmForm = document.getElementById('film-form');
if (filmForm) {
filmForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(filmForm);
const data = Object.fromEntries(formData.entries());
data.id = editingItemId;
data.type = currentAdminTab;
const action = editingItemId ? 'update_film' : 'add_film';
await fetch(`${API_URL}?action=${action}`, {
method: 'POST',
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
document.getElementById('edit-modal')?.classList.remove('open');
editingItemId = null;
loadDashboardData();
});
}
const csvInput = document.getElementById('csv-file');
if (csvInput) {
csvInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
const formData = new FormData();
formData.append('file', file);
formData.append('type', currentAdminTab);
await fetch(`${API_URL}?action=import_csv`, {
method: 'POST',
headers: { 'Authorization': localStorage.getItem('token') },
body: formData
});
loadDashboardData();
});
}
}