/** * ========================================================================= * Mon Cinéma - Module d'Administration (admin.js) * ========================================================================= */ 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'; } })(); // ══════════════════════════════════════════════════════════════════ // CHARGEMENT & RENDU // ══════════════════════════════════════════════════════════════════ 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); filtered.forEach(f => { const tr = document.createElement('tr'); tr.innerHTML = ` ${f.poster ? `` : '-'} ${f.title} ${f.year || ''} ${f.director || ''} ${currentAdminTab === 'critique' ? '★'.repeat(f.rating || 0) : (f.format || '-')} `; tbody.appendChild(tr); }); } // ══════════════════════════════════════════════════════════════════ // MODALES & ACTIONS // ══════════════════════════════════════════════════════════════════ 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; 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'); } 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."); await fetch(`${API_URL}?action=update_password`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': localStorage.getItem('token') }, body: JSON.stringify({ password: pwd1 }) }); 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(); } 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(); } document.addEventListener('DOMContentLoaded', loadDashboardData);