Actualiser js/admin.js
This commit is contained in:
+150
-113
@@ -1,41 +1,49 @@
|
|||||||
const API_URL = '../api.php';
|
const API_URL = '../api.php';
|
||||||
let allItems = [];
|
let allItems = [];
|
||||||
let currentAdminTab = 'critique';
|
let currentAdminTab = 'critique';
|
||||||
let editingItemId = null;
|
|
||||||
|
|
||||||
|
// ── INITIALISATION ──
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
loadDashboardData();
|
loadDashboardData();
|
||||||
initModals();
|
|
||||||
initForms();
|
// Attacher l'événement pour la modale de suppression
|
||||||
|
const confirmBtn = document.getElementById('confirm-btn');
|
||||||
|
if(confirmBtn) {
|
||||||
|
confirmBtn.addEventListener('click', () => {
|
||||||
|
if (pendingDeleteAction) pendingDeleteAction();
|
||||||
|
closeConfirmModal();
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function loadDashboardData() {
|
async function loadDashboardData() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_URL}?action=get_films`);
|
const res = await fetch(`${API_URL}?action=get_films`);
|
||||||
if (!res.ok) throw new Error('Erreur de chargement');
|
|
||||||
allItems = await res.json();
|
allItems = await res.json();
|
||||||
|
|
||||||
|
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();
|
renderAdminTable();
|
||||||
} catch (error) {
|
} catch (err) { console.error('Erreur chargement :', err); }
|
||||||
console.error("Erreur loadDashboardData:", error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderAdminTable() {
|
function renderAdminTable() {
|
||||||
const tbody = document.getElementById('admin-table-body');
|
const tbody = document.getElementById('admin-table-body');
|
||||||
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);
|
||||||
const countLabel = document.getElementById('admin-count-label');
|
|
||||||
if (countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
|
|
||||||
|
|
||||||
// BUG CORRIGÉ : f = > remplacé par f =>
|
const countLabel = document.getElementById('admin-count-label');
|
||||||
// Nettoyage des espaces parasites dans les balises HTML
|
if(countLabel) countLabel.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 = `
|
||||||
<td style="text-align:center;">
|
<td style="text-align:center;">
|
||||||
<input type="checkbox" class="film-checkbox" value="${f.id}">
|
<input type="checkbox" class="film-checkbox" value="${f.id}" onclick="updateBulkBar()">
|
||||||
</td>
|
</td>
|
||||||
<td style="text-align:center;">
|
<td style="text-align:center;">
|
||||||
${f.poster ? `<img src="${f.poster}" class="thumb" alt="Affiche">` : '<div class="thumb-ph"><i class="ti ti-photo"></i></div>'}
|
${f.poster ? `<img src="${f.poster}" class="thumb" alt="Affiche">` : '<div class="thumb-ph"><i class="ti ti-photo"></i></div>'}
|
||||||
@@ -54,6 +62,7 @@ function renderAdminTable() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── GESTION DE MASSE ──
|
||||||
function toggleSelectAll(source) {
|
function toggleSelectAll(source) {
|
||||||
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
||||||
updateBulkBar();
|
updateBulkBar();
|
||||||
@@ -63,23 +72,20 @@ function updateBulkBar() {
|
|||||||
const checked = document.querySelectorAll('.film-checkbox:checked');
|
const checked = document.querySelectorAll('.film-checkbox:checked');
|
||||||
const bulkBar = document.getElementById('bulk-actions-bar');
|
const bulkBar = document.getElementById('bulk-actions-bar');
|
||||||
const bulkCount = document.getElementById('bulk-count');
|
const bulkCount = document.getElementById('bulk-count');
|
||||||
|
|
||||||
if (checked.length > 0) {
|
if (checked.length > 0) {
|
||||||
bulkBar.style.display = 'flex';
|
if(bulkBar) bulkBar.style.display = 'flex';
|
||||||
bulkCount.textContent = checked.length;
|
if(bulkCount) bulkCount.textContent = checked.length;
|
||||||
} else {
|
} else {
|
||||||
bulkBar.style.display = 'none';
|
if(bulkBar) bulkBar.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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('change', (e) => {
|
// ── POP-UP CONFIRMATION SUPPRESSION ──
|
||||||
if (e.target.classList.contains('film-checkbox')) {
|
|
||||||
updateBulkBar();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let pendingDeleteAction = null;
|
let pendingDeleteAction = null;
|
||||||
|
|
||||||
function showConfirmModal(actionFn) {
|
function showConfirmModal(actionFn) {
|
||||||
pendingDeleteAction = actionFn;
|
pendingDeleteAction = actionFn;
|
||||||
const modal = document.getElementById('confirm-modal');
|
const modal = document.getElementById('confirm-modal');
|
||||||
@@ -89,123 +95,154 @@ function showConfirmModal(actionFn) {
|
|||||||
function closeConfirmModal() {
|
function closeConfirmModal() {
|
||||||
const modal = document.getElementById('confirm-modal');
|
const modal = document.getElementById('confirm-modal');
|
||||||
if(modal) modal.classList.remove('open');
|
if(modal) modal.classList.remove('open');
|
||||||
|
pendingDeleteAction = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('confirm-btn')?.addEventListener('click', () => {
|
// ── ACTIONS CRUD (SUPPRESSION) ──
|
||||||
if (pendingDeleteAction) pendingDeleteAction();
|
|
||||||
closeConfirmModal();
|
|
||||||
});
|
|
||||||
|
|
||||||
async function executeBulkDelete() {
|
async function executeBulkDelete() {
|
||||||
|
const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value);
|
||||||
|
if (ids.length === 0) return;
|
||||||
|
|
||||||
showConfirmModal(async () => {
|
showConfirmModal(async () => {
|
||||||
const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value);
|
try {
|
||||||
await fetch(`${API_URL}?action=bulk_delete`, {
|
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({ ids, type: currentAdminTab })
|
body: JSON.stringify({ ids, type: currentAdminTab })
|
||||||
});
|
});
|
||||||
loadDashboardData();
|
|
||||||
updateBulkBar();
|
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); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteSingleFilm(id) {
|
async function deleteSingleFilm(id) {
|
||||||
showConfirmModal(async () => {
|
showConfirmModal(async () => {
|
||||||
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
|
try {
|
||||||
method: 'DELETE',
|
await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, {
|
||||||
headers: { 'Authorization': localStorage.getItem('token') }
|
method: 'DELETE',
|
||||||
});
|
headers: { 'Authorization': localStorage.getItem('token') }
|
||||||
loadDashboardData();
|
});
|
||||||
|
loadDashboardData();
|
||||||
|
} catch (err) { console.error('Erreur delete :', err); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── 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 ──
|
||||||
function switchAdminTab(tabName) {
|
function switchAdminTab(tabName) {
|
||||||
currentAdminTab = tabName;
|
currentAdminTab = tabName;
|
||||||
document.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
|
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
|
||||||
|
const btn = document.getElementById(`btn-tab-${tabName}`);
|
||||||
|
if(btn) btn.classList.add('active');
|
||||||
|
|
||||||
|
document.getElementById('form-critique-fields').style.display = tabName === 'critique' ? 'block' : 'none';
|
||||||
|
document.getElementById('form-videotheque-fields').style.display = tabName === 'videotheque' ? 'block' : 'none';
|
||||||
|
|
||||||
renderAdminTable();
|
renderAdminTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- AJOUTS FONCTIONNELS MANQUANTS ---
|
function openAddModal() {
|
||||||
|
document.getElementById('film-form').reset();
|
||||||
|
document.getElementById('f-id').value = '';
|
||||||
|
document.getElementById('admin-modal').classList.add('open');
|
||||||
|
}
|
||||||
|
|
||||||
function openEditModal(id) {
|
function openEditModal(id) {
|
||||||
const item = allItems.find(i => i.id == id);
|
const item = allItems.find(x => String(x.id) === String(id));
|
||||||
if (!item) return;
|
if (!item) return;
|
||||||
|
|
||||||
editingItemId = id;
|
document.getElementById('f-id').value = item.id;
|
||||||
const modal = document.getElementById('edit-modal');
|
document.getElementById('f-title').value = item.title;
|
||||||
if (!modal) return;
|
document.getElementById('f-year').value = item.year || '';
|
||||||
|
document.getElementById('f-director').value = item.director || '';
|
||||||
|
document.getElementById('f-poster').value = item.poster || '';
|
||||||
|
|
||||||
const setVal = (id, val) => { const el = document.getElementById(id); if (el) el.value = val || ''; };
|
if(currentAdminTab === 'critique') {
|
||||||
setVal('film-title', item.title);
|
document.getElementById('f-rating').value = item.rating || 3;
|
||||||
setVal('film-year', item.year);
|
document.getElementById('f-review').value = item.review || '';
|
||||||
setVal('film-director', item.director);
|
document.getElementById('f-streaming').value = item.streaming || '';
|
||||||
setVal('film-poster', item.poster);
|
} else {
|
||||||
setVal('film-rating', item.rating);
|
document.getElementById('f-format').value = item.format || '';
|
||||||
setVal('film-review', item.review);
|
document.getElementById('f-length').value = item.length || '';
|
||||||
setVal('film-streaming', item.streaming);
|
document.getElementById('f-publisher').value = item.publisher || '';
|
||||||
setVal('film-format', item.format);
|
document.getElementById('f-aspect').value = item.aspect_ratio || '';
|
||||||
setVal('film-length', item.length);
|
document.getElementById('f-ean').value = item.ean_isbn13 || '';
|
||||||
setVal('film-publisher', item.publisher);
|
document.getElementById('f-discs').value = item.number_of_discs || 1;
|
||||||
setVal('film-aspect-ratio', item.aspect_ratio);
|
document.getElementById('f-description').value = item.description || '';
|
||||||
setVal('film-ean', item.ean_isbn13);
|
}
|
||||||
setVal('film-discs', item.number_of_discs);
|
|
||||||
setVal('film-description', item.description);
|
|
||||||
|
|
||||||
modal.classList.add('open');
|
document.getElementById('admin-modal').classList.add('open');
|
||||||
}
|
}
|
||||||
|
|
||||||
function initModals() {
|
function closeAdminModal() { document.getElementById('admin-modal').classList.remove('open'); }
|
||||||
document.querySelectorAll('.modal-close, .btn-cancel').forEach(btn => {
|
function openConfigModal() { document.getElementById('config-modal').classList.add('open'); }
|
||||||
btn.addEventListener('click', (e) => {
|
function closeConfigModal() { document.getElementById('config-modal').classList.remove('open'); }
|
||||||
const overlay = e.target.closest('.overlay');
|
function openPasswordModal() { document.getElementById('password-modal').classList.add('open'); }
|
||||||
if (overlay) overlay.classList.remove('open');
|
function closePasswordModal() { document.getElementById('password-modal').classList.remove('open'); }
|
||||||
});
|
|
||||||
});
|
function logout() {
|
||||||
document.querySelectorAll('.overlay').forEach(overlay => {
|
localStorage.removeItem('token');
|
||||||
overlay.addEventListener('click', (e) => {
|
window.location.href = 'login.html';
|
||||||
if (e.target === overlay) overlay.classList.remove('open');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function initForms() {
|
// ── MOT DE PASSE ──
|
||||||
const filmForm = document.getElementById('film-form');
|
async function saveNewPassword() {
|
||||||
if (filmForm) {
|
const newPwd = document.getElementById('new-password-input').value;
|
||||||
filmForm.addEventListener('submit', async (e) => {
|
const confirmPwd = document.getElementById('new-password-confirm').value;
|
||||||
e.preventDefault();
|
if (newPwd !== confirmPwd) return alert("Les mots de passe ne correspondent pas.");
|
||||||
const formData = new FormData(filmForm);
|
|
||||||
const data = Object.fromEntries(formData.entries());
|
const res = await fetch(`${API_URL}?action=update_password`, {
|
||||||
data.id = editingItemId;
|
method: 'POST',
|
||||||
data.type = currentAdminTab;
|
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ new_password: newPwd })
|
||||||
const action = editingItemId ? 'update_film' : 'add_film';
|
});
|
||||||
await fetch(`${API_URL}?action=${action}`, {
|
if (res.ok) { alert("Mot de passe mis à jour."); closePasswordModal(); }
|
||||||
method: 'POST',
|
}
|
||||||
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(data)
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('edit-modal')?.classList.remove('open');
|
|
||||||
editingItemId = null;
|
|
||||||
loadDashboardData();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const csvInput = document.getElementById('csv-file');
|
// ── IMPORT CSV ──
|
||||||
if (csvInput) {
|
async function handleCsvUpload(input) {
|
||||||
csvInput.addEventListener('change', async (e) => {
|
if (!input.files[0]) return;
|
||||||
const file = e.target.files[0];
|
const formData = new FormData();
|
||||||
if (!file) return;
|
formData.append('csv_file', input.files[0]);
|
||||||
const formData = new FormData();
|
await fetch(`${API_URL}?action=import_csv`, {
|
||||||
formData.append('file', file);
|
method: 'POST',
|
||||||
formData.append('type', currentAdminTab);
|
headers: { 'Authorization': localStorage.getItem('token') },
|
||||||
|
body: formData
|
||||||
await fetch(`${API_URL}?action=import_csv`, {
|
});
|
||||||
method: 'POST',
|
loadDashboardData();
|
||||||
headers: { 'Authorization': localStorage.getItem('token') },
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
loadDashboardData();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user