Actualiser js/admin.js
This commit is contained in:
+19
-71
@@ -16,21 +16,18 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// NOUVEAU : Centralisation des écouteurs d'événements
|
// NOUVEAU : On attache les événements aux formulaires
|
||||||
function initEventListeners() {
|
function initEventListeners() {
|
||||||
// 1. Soumission du formulaire Film
|
|
||||||
const filmForm = document.getElementById('film-form');
|
const filmForm = document.getElementById('film-form');
|
||||||
if (filmForm) filmForm.addEventListener('submit', saveFilmForm);
|
if (filmForm) filmForm.addEventListener('submit', saveFilmForm);
|
||||||
|
|
||||||
// 2. Import CSV
|
|
||||||
const csvInput = document.getElementById('csv-file');
|
const csvInput = document.getElementById('csv-file');
|
||||||
if (csvInput) csvInput.addEventListener('change', (e) => handleCsvUpload(e.target));
|
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'); // Adaptez l'ID si besoin
|
||||||
const savePwdBtn = document.getElementById('save-password-btn');
|
|
||||||
if (savePwdBtn) savePwdBtn.addEventListener('click', saveNewPassword);
|
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) => {
|
document.addEventListener('click', (e) => {
|
||||||
if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) {
|
if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) {
|
||||||
const overlay = e.target.closest('.overlay');
|
const overlay = e.target.closest('.overlay');
|
||||||
@@ -65,6 +62,7 @@ function renderAdminTable() {
|
|||||||
const countLabel = document.getElementById('admin-count-label');
|
const countLabel = document.getElementById('admin-count-label');
|
||||||
if(countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
|
if(countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
|
||||||
|
|
||||||
|
// ✅ CORRECTION : f => (sans espace) et nettoyage des espaces dans le HTML
|
||||||
filtered.forEach(f => {
|
filtered.forEach(f => {
|
||||||
const tr = document.createElement('tr');
|
const tr = document.createElement('tr');
|
||||||
tr.innerHTML = `
|
tr.innerHTML = `
|
||||||
@@ -136,11 +134,9 @@ async function executeBulkDelete() {
|
|||||||
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ ids, type: currentAdminTab })
|
body: JSON.stringify({ ids, type: currentAdminTab })
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById('bulk-actions-bar').style.display = 'none';
|
document.getElementById('bulk-actions-bar').style.display = 'none';
|
||||||
const selectAll = document.getElementById('select-all-checkbox');
|
const selectAll = document.getElementById('select-all-checkbox');
|
||||||
if(selectAll) selectAll.checked = false;
|
if(selectAll) selectAll.checked = false;
|
||||||
|
|
||||||
loadDashboardData();
|
loadDashboardData();
|
||||||
} catch (err) { console.error('Erreur bulk delete :', err); }
|
} catch (err) { console.error('Erreur bulk delete :', err); }
|
||||||
});
|
});
|
||||||
@@ -149,6 +145,7 @@ async function executeBulkDelete() {
|
|||||||
async function deleteSingleFilm(id) {
|
async function deleteSingleFilm(id) {
|
||||||
showConfirmModal(async () => {
|
showConfirmModal(async () => {
|
||||||
try {
|
try {
|
||||||
|
// ✅ Le navigateur va maintenant correctement envoyer cette requête à votre api.php
|
||||||
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
|
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: { 'Authorization': localStorage.getItem('token') }
|
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 ──
|
// ── MODALES & UI ──
|
||||||
// NOUVEAU : Fonction pour afficher/masquer les champs selon l'onglet
|
|
||||||
function toggleFormFields() {
|
function toggleFormFields() {
|
||||||
const critFields = document.getElementById('form-critique-fields');
|
const critFields = document.getElementById('form-critique-fields');
|
||||||
const vidFields = document.getElementById('form-videotheque-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'));
|
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
|
||||||
const btn = document.getElementById(`btn-tab-${tabName}`);
|
const btn = document.getElementById(`btn-tab-${tabName}`);
|
||||||
if(btn) btn.classList.add('active');
|
if(btn) btn.classList.add('active');
|
||||||
|
|
||||||
toggleFormFields();
|
toggleFormFields();
|
||||||
renderAdminTable();
|
renderAdminTable();
|
||||||
}
|
}
|
||||||
@@ -211,7 +175,7 @@ function switchAdminTab(tabName) {
|
|||||||
function openAddModal() {
|
function openAddModal() {
|
||||||
document.getElementById('film-form').reset();
|
document.getElementById('film-form').reset();
|
||||||
document.getElementById('f-id').value = '';
|
document.getElementById('f-id').value = '';
|
||||||
toggleFormFields(); // Correction : Affiche les bons champs dès l'ouverture
|
toggleFormFields();
|
||||||
document.getElementById('admin-modal').classList.add('open');
|
document.getElementById('admin-modal').classList.add('open');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +189,7 @@ function openEditModal(id) {
|
|||||||
document.getElementById('f-director').value = item.director || '';
|
document.getElementById('f-director').value = item.director || '';
|
||||||
document.getElementById('f-poster').value = item.poster || '';
|
document.getElementById('f-poster').value = item.poster || '';
|
||||||
|
|
||||||
toggleFormFields(); // Correction : Affiche les bons champs dès l'ouverture
|
toggleFormFields();
|
||||||
|
|
||||||
if(currentAdminTab === 'critique') {
|
if(currentAdminTab === 'critique') {
|
||||||
document.getElementById('f-rating').value = item.rating || 3;
|
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-discs').value = item.number_of_discs || 1;
|
||||||
document.getElementById('f-description').value = item.description || '';
|
document.getElementById('f-description').value = item.description || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('admin-modal').classList.add('open');
|
document.getElementById('admin-modal').classList.add('open');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,30 +218,15 @@ function logout() {
|
|||||||
window.location.href = 'login.html';
|
window.location.href = 'login.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── MOT DE PASSE ──
|
// ── SAUVEGARDE FILM ──
|
||||||
async function saveNewPassword() {
|
async function saveFilmForm(e) {
|
||||||
const newPwd = document.getElementById('new-password-input').value;
|
e.preventDefault();
|
||||||
const confirmPwd = document.getElementById('new-password-confirm').value;
|
const payload = {
|
||||||
if (newPwd !== confirmPwd) return alert("Les mots de passe ne correspondent pas.");
|
type: currentAdminTab,
|
||||||
|
id: document.getElementById('f-id').value,
|
||||||
const res = await fetch(`${API_URL}?action=update_password`, {
|
title: document.getElementById('f-title').value,
|
||||||
method: 'POST',
|
year: document.getElementById('f-year').value,
|
||||||
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
director: document.getElementById('f-director').value,
|
||||||
body: JSON.stringify({ new_password: newPwd })
|
poster: document.getElementById('f-poster').value,
|
||||||
});
|
rating: document.getElementById('f-rating') ? document.getElementById('f-rating').value : '',
|
||||||
if (res.ok) { alert("Mot de passe mis à jour."); closePasswordModal(); }
|
review
|
||||||
}
|
|
||||||
|
|
||||||
// ── 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();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user