Actualiser js/admin.js

This commit is contained in:
2026-06-18 16:36:15 +02:00
parent 77de6ece1c
commit 6e2fa72fb7
+19 -71
View File
@@ -16,21 +16,18 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
// NOUVEAU : Centralisation des écouteurs d'événements
// NOUVEAU : On attache les événements aux formulaires
function initEventListeners() {
// 1. Soumission du formulaire Film
const filmForm = document.getElementById('film-form');
if (filmForm) filmForm.addEventListener('submit', saveFilmForm);
// 2. Import CSV
const csvInput = document.getElementById('csv-file');
if (csvInput) csvInput.addEventListener('change', (e) => handleCsvUpload(e.target));
// 3. Sauvegarde du mot de passe (Adapter l'ID 'save-password-btn' si votre HTML diffère)
const savePwdBtn = document.getElementById('save-password-btn');
const savePwdBtn = document.getElementById('save-password-btn'); // Adaptez l'ID si besoin
if (savePwdBtn) savePwdBtn.addEventListener('click', saveNewPassword);
// 4. Fermeture générique des modales (clic sur la croix ou sur le fond sombre)
// Fermeture des modales en cliquant sur le fond ou la croix
document.addEventListener('click', (e) => {
if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) {
const overlay = e.target.closest('.overlay');
@@ -65,6 +62,7 @@ function renderAdminTable() {
const countLabel = document.getElementById('admin-count-label');
if(countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
// ✅ CORRECTION : f => (sans espace) et nettoyage des espaces dans le HTML
filtered.forEach(f => {
const tr = document.createElement('tr');
tr.innerHTML = `
@@ -136,11 +134,9 @@ async function executeBulkDelete() {
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
body: JSON.stringify({ ids, type: currentAdminTab })
});
document.getElementById('bulk-actions-bar').style.display = 'none';
const selectAll = document.getElementById('select-all-checkbox');
if(selectAll) selectAll.checked = false;
loadDashboardData();
} catch (err) { console.error('Erreur bulk delete :', err); }
});
@@ -149,6 +145,7 @@ async function executeBulkDelete() {
async function deleteSingleFilm(id) {
showConfirmModal(async () => {
try {
// ✅ Le navigateur va maintenant correctement envoyer cette requête à votre api.php
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
method: 'DELETE',
headers: { 'Authorization': localStorage.getItem('token') }
@@ -158,39 +155,7 @@ async function deleteSingleFilm(id) {
});
}
// ── ACTIONS CRUD (SAUVEGARDE) ──
async function saveFilmForm(e) {
e.preventDefault();
const payload = {
type: currentAdminTab,
id: document.getElementById('f-id').value,
title: document.getElementById('f-title').value,
year: document.getElementById('f-year').value,
director: document.getElementById('f-director').value,
poster: document.getElementById('f-poster').value,
rating: document.getElementById('f-rating') ? document.getElementById('f-rating').value : '',
review: document.getElementById('f-review') ? document.getElementById('f-review').value : '',
streaming: document.getElementById('f-streaming') ? document.getElementById('f-streaming').value : '',
format: document.getElementById('f-format') ? document.getElementById('f-format').value : '',
length: document.getElementById('f-length') ? document.getElementById('f-length').value : '',
publisher: document.getElementById('f-publisher') ? document.getElementById('f-publisher').value : '',
ean_isbn13: document.getElementById('f-ean') ? document.getElementById('f-ean').value : '',
number_of_discs: document.getElementById('f-discs') ? document.getElementById('f-discs').value : '',
aspect_ratio: document.getElementById('f-aspect') ? document.getElementById('f-aspect').value : '',
description: document.getElementById('f-description') ? document.getElementById('f-description').value : ''
};
await fetch(`${API_URL}?action=save_film`, {
method: 'POST',
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
closeAdminModal();
loadDashboardData();
}
// ── MODALES & UI ──
// NOUVEAU : Fonction pour afficher/masquer les champs selon l'onglet
function toggleFormFields() {
const critFields = document.getElementById('form-critique-fields');
const vidFields = document.getElementById('form-videotheque-fields');
@@ -203,7 +168,6 @@ function switchAdminTab(tabName) {
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
const btn = document.getElementById(`btn-tab-${tabName}`);
if(btn) btn.classList.add('active');
toggleFormFields();
renderAdminTable();
}
@@ -211,7 +175,7 @@ function switchAdminTab(tabName) {
function openAddModal() {
document.getElementById('film-form').reset();
document.getElementById('f-id').value = '';
toggleFormFields(); // Correction : Affiche les bons champs dès l'ouverture
toggleFormFields();
document.getElementById('admin-modal').classList.add('open');
}
@@ -225,7 +189,7 @@ function openEditModal(id) {
document.getElementById('f-director').value = item.director || '';
document.getElementById('f-poster').value = item.poster || '';
toggleFormFields(); // Correction : Affiche les bons champs dès l'ouverture
toggleFormFields();
if(currentAdminTab === 'critique') {
document.getElementById('f-rating').value = item.rating || 3;
@@ -240,7 +204,6 @@ function openEditModal(id) {
document.getElementById('f-discs').value = item.number_of_discs || 1;
document.getElementById('f-description').value = item.description || '';
}
document.getElementById('admin-modal').classList.add('open');
}
@@ -255,30 +218,15 @@ function logout() {
window.location.href = 'login.html';
}
// ── MOT DE PASSE ──
async function saveNewPassword() {
const newPwd = document.getElementById('new-password-input').value;
const confirmPwd = document.getElementById('new-password-confirm').value;
if (newPwd !== confirmPwd) return alert("Les mots de passe ne correspondent pas.");
const res = await fetch(`${API_URL}?action=update_password`, {
method: 'POST',
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
body: JSON.stringify({ new_password: newPwd })
});
if (res.ok) { alert("Mot de passe mis à jour."); closePasswordModal(); }
}
// ── IMPORT CSV ──
async function handleCsvUpload(input) {
if (!input.files[0]) return;
const formData = new FormData();
formData.append('csv_file', input.files[0]);
formData.append('type', currentAdminTab); // Correction : Envoi du type d'onglet actif
await fetch(`${API_URL}?action=import_csv`, {
method: 'POST',
headers: { 'Authorization': localStorage.getItem('token') },
body: formData
});
loadDashboardData();
}
// ── SAUVEGARDE FILM ──
async function saveFilmForm(e) {
e.preventDefault();
const payload = {
type: currentAdminTab,
id: document.getElementById('f-id').value,
title: document.getElementById('f-title').value,
year: document.getElementById('f-year').value,
director: document.getElementById('f-director').value,
poster: document.getElementById('f-poster').value,
rating: document.getElementById('f-rating') ? document.getElementById('f-rating').value : '',
review