Actualiser js/admin.js
This commit is contained in:
+114
-25
@@ -1,22 +1,36 @@
|
|||||||
const API_URL = '../api.php';
|
const API_URL = '../api.php';
|
||||||
let allItems = [];
|
let allItems = [];
|
||||||
let currentAdminTab = 'critique';
|
let currentAdminTab = 'critique';
|
||||||
|
let editingItemId = null;
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', loadDashboardData);
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
loadDashboardData();
|
||||||
|
initModals();
|
||||||
|
initForms();
|
||||||
|
});
|
||||||
|
|
||||||
async function loadDashboardData() {
|
async function loadDashboardData() {
|
||||||
const res = await fetch(`${API_URL}?action=get_films`);
|
try {
|
||||||
allItems = await res.json();
|
const res = await fetch(`${API_URL}?action=get_films`);
|
||||||
renderAdminTable();
|
if (!res.ok) throw new Error('Erreur de chargement');
|
||||||
|
allItems = await res.json();
|
||||||
|
renderAdminTable();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Erreur loadDashboardData:", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderAdminTable() {
|
function renderAdminTable() {
|
||||||
const tbody = document.getElementById('admin-table-body');
|
const tbody = document.getElementById('admin-table-body');
|
||||||
if (!tbody) return;
|
if (!tbody) return;
|
||||||
tbody.innerHTML = '';
|
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 => {
|
filtered.forEach(f => {
|
||||||
const tr = document.createElement('tr');
|
const tr = document.createElement('tr');
|
||||||
tr.innerHTML = `
|
tr.innerHTML = `
|
||||||
@@ -40,57 +54,48 @@ function renderAdminTable() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Sélectionner tout
|
|
||||||
function toggleSelectAll(source) {
|
function toggleSelectAll(source) {
|
||||||
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
||||||
updateBulkBar();
|
updateBulkBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Mettre à jour la barre (appelée par chaque clic sur une checkbox)
|
|
||||||
function updateBulkBar() {
|
function updateBulkBar() {
|
||||||
const checked = document.querySelectorAll('.film-checkbox:checked');
|
const checked = document.querySelectorAll('.film-checkbox:checked');
|
||||||
const bulkBar = document.getElementById('bulk-actions-bar');
|
const bulkBar = document.getElementById('bulk-actions-bar');
|
||||||
const bulkCount = document.getElementById('bulk-count');
|
const bulkCount = document.getElementById('bulk-count');
|
||||||
|
|
||||||
if (checked.length > 0) {
|
if (checked.length > 0) {
|
||||||
bulkBar.style.display = 'flex';
|
bulkBar.style.display = 'flex';
|
||||||
bulkCount.textContent = checked.length;
|
bulkCount.textContent = checked.length;
|
||||||
} else {
|
} else {
|
||||||
bulkBar.style.display = 'none';
|
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) => {
|
document.addEventListener('change', (e) => {
|
||||||
if (e.target.classList.contains('film-checkbox')) {
|
if (e.target.classList.contains('film-checkbox')) {
|
||||||
const count = document.querySelectorAll('.film-checkbox:checked').length;
|
updateBulkBar();
|
||||||
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;
|
let pendingDeleteAction = null;
|
||||||
|
|
||||||
function showConfirmModal(actionFn) {
|
function showConfirmModal(actionFn) {
|
||||||
pendingDeleteAction = actionFn;
|
pendingDeleteAction = actionFn;
|
||||||
document.getElementById('confirm-modal').classList.add('open');
|
const modal = document.getElementById('confirm-modal');
|
||||||
|
if(modal) modal.classList.add('open');
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeConfirmModal() {
|
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();
|
if (pendingDeleteAction) pendingDeleteAction();
|
||||||
closeConfirmModal();
|
closeConfirmModal();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mise à jour de la suppression de masse
|
|
||||||
async function executeBulkDelete() {
|
async function executeBulkDelete() {
|
||||||
showConfirmModal(async () => {
|
showConfirmModal(async () => {
|
||||||
const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value);
|
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 })
|
body: JSON.stringify({ ids, type: currentAdminTab })
|
||||||
});
|
});
|
||||||
loadDashboardData();
|
loadDashboardData();
|
||||||
document.getElementById('bulk-actions-bar').style.display = 'none';
|
updateBulkBar();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mise à jour de la suppression unitaire
|
|
||||||
async function deleteSingleFilm(id) {
|
async function deleteSingleFilm(id) {
|
||||||
showConfirmModal(async () => {
|
showConfirmModal(async () => {
|
||||||
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
|
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}`));
|
document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
|
||||||
renderAdminTable();
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user