Actualiser js/admin.js
This commit is contained in:
+27
-168
@@ -1,27 +1,13 @@
|
|||||||
/**
|
|
||||||
* Mon Cinéma - Module d'Administration (admin.js)
|
|
||||||
* Version synchronisée avec api.php
|
|
||||||
*/
|
|
||||||
|
|
||||||
const API_URL = '../api.php';
|
const API_URL = '../api.php';
|
||||||
let allItems = [];
|
let allItems = [];
|
||||||
let currentAdminTab = 'critique';
|
let currentAdminTab = 'critique';
|
||||||
|
|
||||||
// ── INITIALISATION ──
|
|
||||||
document.addEventListener('DOMContentLoaded', loadDashboardData);
|
document.addEventListener('DOMContentLoaded', loadDashboardData);
|
||||||
|
|
||||||
async function loadDashboardData() {
|
async function loadDashboardData() {
|
||||||
try {
|
const res = await fetch(`${API_URL}?action=get_films`);
|
||||||
const res = await fetch(`${API_URL}?action=get_films`);
|
allItems = await res.json();
|
||||||
allItems = await res.json();
|
renderAdminTable();
|
||||||
|
|
||||||
const secRes = await fetch(`${API_URL}?action=check_security_status`);
|
|
||||||
const secData = await secRes.json();
|
|
||||||
const banner = document.getElementById('security-banner');
|
|
||||||
if (banner) banner.style.display = secData.is_blank ? 'flex' : 'none';
|
|
||||||
|
|
||||||
renderAdminTable();
|
|
||||||
} catch (err) { console.error('Erreur chargement :', err); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderAdminTable() {
|
function renderAdminTable() {
|
||||||
@@ -29,8 +15,7 @@ function renderAdminTable() {
|
|||||||
if (!tbody) return;
|
if (!tbody) return;
|
||||||
tbody.innerHTML = '';
|
tbody.innerHTML = '';
|
||||||
const filtered = allItems.filter(item => item.type === currentAdminTab);
|
const filtered = allItems.filter(item => item.type === currentAdminTab);
|
||||||
document.getElementById('admin-count-label').textContent = `${filtered.length} élément(s)`;
|
|
||||||
|
|
||||||
filtered.forEach(f => {
|
filtered.forEach(f => {
|
||||||
const tr = document.createElement('tr');
|
const tr = document.createElement('tr');
|
||||||
tr.innerHTML = `
|
tr.innerHTML = `
|
||||||
@@ -41,47 +26,42 @@ function renderAdminTable() {
|
|||||||
<td>${f.director || ''}</td>
|
<td>${f.director || ''}</td>
|
||||||
<td>${currentAdminTab === 'critique' ? (f.rating ? '★'.repeat(f.rating) : '☆') : (f.format || '-')}</td>
|
<td>${currentAdminTab === 'critique' ? (f.rating ? '★'.repeat(f.rating) : '☆') : (f.format || '-')}</td>
|
||||||
<td>
|
<td>
|
||||||
<button onclick="openEditModal('${f.id}')">Éditer</button>
|
|
||||||
<button onclick="deleteSingleFilm('${f.id}')">Supprimer</button>
|
<button onclick="deleteSingleFilm('${f.id}')">Supprimer</button>
|
||||||
</td>`;
|
</td>`;
|
||||||
tbody.appendChild(tr);
|
tbody.appendChild(tr);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── ACTIONS CRUD ──
|
// ── Sélection de masse ──
|
||||||
async function saveFilmForm(e) {
|
function toggleSelectAll(source) {
|
||||||
e.preventDefault();
|
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
||||||
const payload = {
|
document.getElementById('bulk-actions-bar').style.display = source.checked ? 'flex' : 'none';
|
||||||
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`, {
|
// Détection de clic sur une case
|
||||||
|
document.addEventListener('change', (e) => {
|
||||||
|
if (e.target.classList.contains('film-checkbox')) {
|
||||||
|
const count = document.querySelectorAll('.film-checkbox:checked').length;
|
||||||
|
document.getElementById('bulk-actions-bar').style.display = count > 0 ? 'flex' : 'none';
|
||||||
|
document.getElementById('bulk-count').textContent = count;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function executeBulkDelete() {
|
||||||
|
const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value);
|
||||||
|
if (!confirm(`Supprimer ces ${ids.length} films ?`)) return;
|
||||||
|
|
||||||
|
await fetch(`${API_URL}?action=bulk_delete`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(payload)
|
body: JSON.stringify({ ids, type: currentAdminTab })
|
||||||
});
|
});
|
||||||
closeAdminModal();
|
|
||||||
loadDashboardData();
|
loadDashboardData();
|
||||||
|
document.getElementById('bulk-actions-bar').style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteSingleFilm(id) {
|
async function deleteSingleFilm(id) {
|
||||||
if (!confirm('Supprimer cette œuvre ?')) return;
|
if (!confirm('Supprimer ce film ?')) return;
|
||||||
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') }
|
||||||
@@ -89,129 +69,8 @@ async function deleteSingleFilm(id) {
|
|||||||
loadDashboardData();
|
loadDashboardData();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── MODALES & UI ──
|
|
||||||
function switchAdminTab(tabName) {
|
function switchAdminTab(tabName) {
|
||||||
currentAdminTab = tabName;
|
currentAdminTab = tabName;
|
||||||
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
|
document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
|
||||||
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();
|
renderAdminTable();
|
||||||
}
|
|
||||||
|
|
||||||
function openAddModal() {
|
|
||||||
document.getElementById('film-form').reset();
|
|
||||||
document.getElementById('f-id').value = '';
|
|
||||||
document.getElementById('admin-modal').classList.add('open');
|
|
||||||
}
|
|
||||||
|
|
||||||
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('admin-modal').classList.add('open');
|
|
||||||
}
|
|
||||||
|
|
||||||
function closeAdminModal() { document.getElementById('admin-modal').classList.remove('open'); }
|
|
||||||
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 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]);
|
|
||||||
await fetch(`${API_URL}?action=import_csv`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Authorization': localStorage.getItem('token') },
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
loadDashboardData();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── GESTION SUPPRESSION DE MASSE ──
|
|
||||||
|
|
||||||
// Fonction appelée par le checkbox "Tout cocher"
|
|
||||||
function toggleSelectAll(source) {
|
|
||||||
const checkboxes = document.querySelectorAll('.film-checkbox');
|
|
||||||
checkboxes.forEach(checkbox => checkbox.checked = source.checked);
|
|
||||||
updateBulkBar();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fonction pour mettre à jour l'affichage de la barre d'actions
|
|
||||||
function updateBulkBar() {
|
|
||||||
const checkboxes = document.querySelectorAll('.film-checkbox:checked');
|
|
||||||
const bulkBar = document.getElementById('bulk-actions-bar');
|
|
||||||
const bulkCount = document.getElementById('bulk-count');
|
|
||||||
|
|
||||||
if (checkboxes.length > 0) {
|
|
||||||
bulkBar.style.display = 'flex';
|
|
||||||
bulkCount.textContent = checkboxes.length;
|
|
||||||
} else {
|
|
||||||
bulkBar.style.display = 'none';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Écouteur d'événement pour détecter les clics sur les checkboxes individuelles
|
|
||||||
document.addEventListener('change', (e) => {
|
|
||||||
if (e.target.classList.contains('film-checkbox')) {
|
|
||||||
updateBulkBar();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fonction appelée par le bouton "Supprimer la sélection"
|
|
||||||
async function executeBulkDelete() {
|
|
||||||
const selected = Array.from(document.querySelectorAll('.film-checkbox:checked'))
|
|
||||||
.map(cb => cb.value);
|
|
||||||
|
|
||||||
if (selected.length === 0) return;
|
|
||||||
if (!confirm(`Supprimer ces ${selected.length} éléments ?`)) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}?action=bulk_delete`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Authorization': localStorage.getItem('token'),
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ ids: selected, type: currentAdminTab })
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
// Réinitialiser la barre après suppression
|
|
||||||
document.getElementById('bulk-actions-bar').style.display = 'none';
|
|
||||||
document.getElementById('select-all-checkbox').checked = false;
|
|
||||||
loadDashboardData();
|
|
||||||
} else {
|
|
||||||
alert("Erreur lors de la suppression.");
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Erreur bulk delete :', err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user