Files
mon-petit-cinema/api.php
T
2026-06-18 10:49:41 +02:00

156 lines
6.1 KiB
PHP

/**
* Mon Cinéma - Module d'Administration (admin.js)
* Version synchronisée avec api.php
*/
const API_URL = '../api.php';
let allItems = [];
let currentAdminTab = 'critique';
// ── INITIALISATION ──
document.addEventListener('DOMContentLoaded', loadDashboardData);
async function loadDashboardData() {
try {
const res = await fetch(`${API_URL}?action=get_films`);
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();
} catch (err) { console.error('Erreur chargement :', err); }
}
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');
tr.innerHTML = `
<td><input type="checkbox" class="film-checkbox" value="${f.id}"></td>
<td>${f.poster ? `<img src="${f.poster}" class="thumb" width="40">` : '-'}</td>
<td><strong>${f.title}</strong></td>
<td>${f.year || ''}</td>
<td>${f.director || ''}</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>
</td>`;
tbody.appendChild(tr);
});
}
// ── 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 = '';
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();
}