Actualiser js/admin.js

This commit is contained in:
2026-06-18 12:05:32 +02:00
parent 7dd4a707d9
commit c8f4245973
+75 -71
View File
@@ -1,23 +1,15 @@
/**
* =========================================================================
* Mon Cinéma - Module d'Administration (admin.js)
* =========================================================================
* Version synchronisée avec api.php
*/
const API_URL = '../api.php';
let allItems = [];
let currentAdminTab = 'critique';
// ── 1. GARDE DE SESSION ──
(function guardSession() {
if (!localStorage.getItem('token')) {
window.location.href = 'login.html';
}
})();
// ── INITIALISATION ──
document.addEventListener('DOMContentLoaded', loadDashboardData);
// ══════════════════════════════════════════════════════════════════
// CHARGEMENT & RENDU
// ══════════════════════════════════════════════════════════════════
async function loadDashboardData() {
try {
const res = await fetch(`${API_URL}?action=get_films`);
@@ -35,9 +27,9 @@ async function loadDashboardData() {
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)`;
filtered.forEach(f => {
const tr = document.createElement('tr');
@@ -47,7 +39,7 @@ function renderAdminTable() {
<td><strong>${f.title}</strong></td>
<td>${f.year || ''}</td>
<td>${f.director || ''}</td>
<td>${currentAdminTab === 'critique' ? '★'.repeat(f.rating || 0) : (f.format || '-')}</td>
<td>${currentAdminTab === 'critique' ? (f.rating ? '★'.repeat(f.rating) : '☆') : (f.format || '-')}</td>
<td>
<button onclick="openEditModal('${f.id}')">Éditer</button>
<button onclick="deleteSingleFilm('${f.id}')">Supprimer</button>
@@ -56,9 +48,60 @@ function renderAdminTable() {
});
}
// ══════════════════════════════════════════════════════════════════
// MODALES & ACTIONS
// ══════════════════════════════════════════════════════════════════
// ── ACTIONS CRUD ──
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,
// Champs conditionnels
rating: document.getElementById('f-rating').value,
review: document.getElementById('f-review').value,
streaming: document.getElementById('f-streaming').value,
format: document.getElementById('f-format').value,
length: document.getElementById('f-length').value,
publisher: document.getElementById('f-publisher').value,
ean_isbn13: document.getElementById('f-ean').value,
number_of_discs: document.getElementById('f-discs').value,
aspect_ratio: document.getElementById('f-aspect').value,
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();
}
async function deleteSingleFilm(id) {
if (!confirm('Supprimer cette œuvre ?')) return;
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
method: 'DELETE',
headers: { 'Authorization': localStorage.getItem('token') }
});
loadDashboardData();
}
// ── MODALES & UI ──
function switchAdminTab(tabName) {
currentAdminTab = tabName;
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.getElementById(`btn-tab-${tabName}`).classList.add('active');
// Basculer l'affichage des formulaires
document.getElementById('form-critique-fields').style.display = tabName === 'critique' ? 'block' : 'none';
document.getElementById('form-videotheque-fields').style.display = tabName === 'videotheque' ? 'block' : 'none';
renderAdminTable();
}
function openAddModal() {
document.getElementById('film-form').reset();
document.getElementById('f-id').value = '';
@@ -68,75 +111,38 @@ function openAddModal() {
function openEditModal(id) {
const item = allItems.find(x => String(x.id) === String(id));
if (!item) return;
// Remplissage formulaire...
document.getElementById('f-id').value = item.id;
document.getElementById('f-title').value = item.title;
document.getElementById('f-year').value = item.year || '';
document.getElementById('f-director').value = item.director || '';
document.getElementById('admin-modal').classList.add('open');
}
function closeAdminModal() { document.getElementById('admin-modal').classList.remove('open'); }
async function saveFilmForm(e) {
e.preventDefault();
const payload = {
type: currentAdminTab,
id: document.getElementById('f-id').value || null,
title: document.getElementById('f-title').value,
year: document.getElementById('f-year').value,
director: document.getElementById('f-director').value
};
await fetch(`${API_URL}?action=save_film`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': localStorage.getItem('token') },
body: JSON.stringify(payload)
});
closeAdminModal();
loadDashboardData();
}
// ══════════════════════════════════════════════════════════════════
// GESTION COMPTE & SÉCURITÉ
// ══════════════════════════════════════════════════════════════════
function logout() { localStorage.removeItem('token'); window.location.href = 'login.html'; }
function openConfigModal() { document.getElementById('config-modal').classList.add('open'); }
function closeConfigModal() { document.getElementById('config-modal').classList.remove('open'); }
function openPasswordModal() { document.getElementById('password-modal').classList.add('open'); }
function closePasswordModal() { document.getElementById('password-modal').classList.remove('open'); }
function logout() {
localStorage.removeItem('token');
window.location.href = 'login.html';
}
// ── MOT DE PASSE ──
async function saveNewPassword() {
const pwd1 = document.getElementById('new-password-input').value;
const pwd2 = document.getElementById('new-password-confirm').value;
if (pwd1 !== pwd2) return alert("Les mots de passe ne correspondent pas.");
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.");
await fetch(`${API_URL}?action=update_password`, {
const res = await fetch(`${API_URL}?action=update_password`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': localStorage.getItem('token') },
body: JSON.stringify({ password: pwd1 })
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
body: JSON.stringify({ new_password: newPwd })
});
alert("Mot de passe mis à jour.");
closePasswordModal();
}
// ══════════════════════════════════════════════════════════════════
// UTILS
// ══════════════════════════════════════════════════════════════════
async function deleteSingleFilm(id) {
if (!confirm('Supprimer ?')) return;
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
method: 'DELETE',
headers: { 'Authorization': localStorage.getItem('token') }
});
loadDashboardData();
}
function switchAdminTab(tabName) {
currentAdminTab = tabName;
document.getElementById('btn-tab-critique').classList.toggle('active', tabName === 'critique');
document.getElementById('btn-tab-videotheque').classList.toggle('active', tabName === 'videotheque');
renderAdminTable();
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();
@@ -147,6 +153,4 @@ async function handleCsvUpload(input) {
body: formData
});
loadDashboardData();
}
document.addEventListener('DOMContentLoaded', loadDashboardData);
}