From 0daac45b37fd50e6d33635ed649cf4acdf0e5322 Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 18 Jun 2026 16:44:14 +0200 Subject: [PATCH] Actualiser js/admin.js --- js/admin.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/js/admin.js b/js/admin.js index b378fe4..942f17f 100644 --- a/js/admin.js +++ b/js/admin.js @@ -16,7 +16,6 @@ document.addEventListener('DOMContentLoaded', () => { } }); -// NOUVEAU : On attache les événements aux formulaires function initEventListeners() { const filmForm = document.getElementById('film-form'); if (filmForm) filmForm.addEventListener('submit', saveFilmForm); @@ -24,7 +23,7 @@ function initEventListeners() { const csvInput = document.getElementById('csv-file'); if (csvInput) csvInput.addEventListener('change', (e) => handleCsvUpload(e.target)); - const savePwdBtn = document.getElementById('save-password-btn'); // Adaptez l'ID si besoin + const savePwdBtn = document.getElementById('save-password-btn'); if (savePwdBtn) savePwdBtn.addEventListener('click', saveNewPassword); // Fermeture des modales en cliquant sur le fond ou la croix @@ -62,7 +61,6 @@ 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 = ` @@ -145,7 +143,6 @@ 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') } @@ -228,5 +225,80 @@ async function saveFilmForm(e) { year: document.getElementById('f-year').value, director: document.getElementById('f-director').value, poster: document.getElementById('f-poster').value, + // Champs "critiques" rating: document.getElementById('f-rating') ? document.getElementById('f-rating').value : '', - review \ No newline at end of file + review: document.getElementById('f-review') ? document.getElementById('f-review').value : '', + streaming: document.getElementById('f-streaming') ? document.getElementById('f-streaming').value : '', + // Champs "vidéothèque" + 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 : '', + aspect_ratio: document.getElementById('f-aspect') ? document.getElementById('f-aspect').value : '', + ean_isbn13: document.getElementById('f-ean') ? document.getElementById('f-ean').value : '', + number_of_discs: document.getElementById('f-discs') ? document.getElementById('f-discs').value : 1, + description: document.getElementById('f-description') ? document.getElementById('f-description').value : '' + }; + + try { + await fetch(`${API_URL}?action=save_film`, { + method: 'POST', + headers: { + 'Authorization': localStorage.getItem('token'), + 'Content-Type': 'application/json' + }, + body: JSON.stringify(payload) + }); + closeAdminModal(); + loadDashboardData(); + } catch (err) { + console.error('Erreur lors de la sauvegarde :', err); + } +} + +// ── GESTION DES FICHIERS CSV ── +async function handleCsvUpload(input) { + if (!input.files || input.files.length === 0) return; + + const file = input.files[0]; + const formData = new FormData(); + formData.append('csv_file', file); + formData.append('type', currentAdminTab); + + try { + await fetch(`${API_URL}?action=import_csv`, { + method: 'POST', + headers: { 'Authorization': localStorage.getItem('token') }, + body: formData + }); + input.value = ''; // Réinitialiser le champ + closeConfigModal(); + loadDashboardData(); + } catch (err) { + console.error('Erreur lors de l\'importation du fichier CSV :', err); + } +} + +// ── MISE A JOUR DU MOT DE PASSE ── +async function saveNewPassword() { + const pwdInput = document.getElementById('new-password'); // Assurez-vous que l'ID correspond à votre HTML + if (!pwdInput) return; + + const newPassword = pwdInput.value; + if (!newPassword) return; + + try { + await fetch(`${API_URL}?action=update_password`, { + method: 'POST', + headers: { + 'Authorization': localStorage.getItem('token'), + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ new_password: newPassword }) + }); + pwdInput.value = ''; // Vider le champ + closePasswordModal(); + alert('Mot de passe mis à jour avec succès.'); + } catch (err) { + console.error('Erreur lors de la mise à jour du mot de passe :', err); + } +} \ No newline at end of file