Actualiser js/admin.js
This commit is contained in:
+75
-71
@@ -1,23 +1,15 @@
|
|||||||
/**
|
/**
|
||||||
* =========================================================================
|
|
||||||
* Mon Cinéma - Module d'Administration (admin.js)
|
* 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';
|
||||||
|
|
||||||
// ── 1. GARDE DE SESSION ──
|
// ── INITIALISATION ──
|
||||||
(function guardSession() {
|
document.addEventListener('DOMContentLoaded', loadDashboardData);
|
||||||
if (!localStorage.getItem('token')) {
|
|
||||||
window.location.href = 'login.html';
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
// ══════════════════════════════════════════════════════════════════
|
|
||||||
// CHARGEMENT & RENDU
|
|
||||||
// ══════════════════════════════════════════════════════════════════
|
|
||||||
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`);
|
||||||
@@ -35,9 +27,9 @@ async function loadDashboardData() {
|
|||||||
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);
|
||||||
|
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');
|
||||||
@@ -47,7 +39,7 @@ function renderAdminTable() {
|
|||||||
<td><strong>${f.title}</strong></td>
|
<td><strong>${f.title}</strong></td>
|
||||||
<td>${f.year || ''}</td>
|
<td>${f.year || ''}</td>
|
||||||
<td>${f.director || ''}</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>
|
<td>
|
||||||
<button onclick="openEditModal('${f.id}')">Éditer</button>
|
<button onclick="openEditModal('${f.id}')">Éditer</button>
|
||||||
<button onclick="deleteSingleFilm('${f.id}')">Supprimer</button>
|
<button onclick="deleteSingleFilm('${f.id}')">Supprimer</button>
|
||||||
@@ -56,9 +48,60 @@ function renderAdminTable() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ══════════════════════════════════════════════════════════════════
|
// ── ACTIONS CRUD ──
|
||||||
// MODALES & ACTIONS
|
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() {
|
function openAddModal() {
|
||||||
document.getElementById('film-form').reset();
|
document.getElementById('film-form').reset();
|
||||||
document.getElementById('f-id').value = '';
|
document.getElementById('f-id').value = '';
|
||||||
@@ -68,75 +111,38 @@ function openAddModal() {
|
|||||||
function openEditModal(id) {
|
function openEditModal(id) {
|
||||||
const item = allItems.find(x => String(x.id) === String(id));
|
const item = allItems.find(x => String(x.id) === String(id));
|
||||||
if (!item) return;
|
if (!item) return;
|
||||||
|
// Remplissage formulaire...
|
||||||
document.getElementById('f-id').value = item.id;
|
document.getElementById('f-id').value = item.id;
|
||||||
document.getElementById('f-title').value = item.title;
|
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');
|
document.getElementById('admin-modal').classList.add('open');
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeAdminModal() { document.getElementById('admin-modal').classList.remove('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 openConfigModal() { document.getElementById('config-modal').classList.add('open'); }
|
||||||
function closeConfigModal() { document.getElementById('config-modal').classList.remove('open'); }
|
function closeConfigModal() { document.getElementById('config-modal').classList.remove('open'); }
|
||||||
function openPasswordModal() { document.getElementById('password-modal').classList.add('open'); }
|
function openPasswordModal() { document.getElementById('password-modal').classList.add('open'); }
|
||||||
function closePasswordModal() { document.getElementById('password-modal').classList.remove('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() {
|
async function saveNewPassword() {
|
||||||
const pwd1 = document.getElementById('new-password-input').value;
|
const newPwd = document.getElementById('new-password-input').value;
|
||||||
const pwd2 = document.getElementById('new-password-confirm').value;
|
const confirmPwd = document.getElementById('new-password-confirm').value;
|
||||||
if (pwd1 !== pwd2) return alert("Les mots de passe ne correspondent pas.");
|
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',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json', 'Authorization': localStorage.getItem('token') },
|
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ password: pwd1 })
|
body: JSON.stringify({ new_password: newPwd })
|
||||||
});
|
});
|
||||||
alert("Mot de passe mis à jour.");
|
if (res.ok) { alert("Mot de passe mis à jour."); closePasswordModal(); }
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── IMPORT CSV ──
|
||||||
async function handleCsvUpload(input) {
|
async function handleCsvUpload(input) {
|
||||||
if (!input.files[0]) return;
|
if (!input.files[0]) return;
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
@@ -147,6 +153,4 @@ async function handleCsvUpload(input) {
|
|||||||
body: formData
|
body: formData
|
||||||
});
|
});
|
||||||
loadDashboardData();
|
loadDashboardData();
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', loadDashboardData);
|
|
||||||
Reference in New Issue
Block a user