76 lines
2.9 KiB
JavaScript
76 lines
2.9 KiB
JavaScript
const API_URL = '../api.php';
|
|
let allItems = [];
|
|
let currentAdminTab = 'critique';
|
|
|
|
document.addEventListener('DOMContentLoaded', loadDashboardData);
|
|
|
|
async function loadDashboardData() {
|
|
const res = await fetch(`${API_URL}?action=get_films`);
|
|
allItems = await res.json();
|
|
renderAdminTable();
|
|
}
|
|
|
|
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 = `
|
|
<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="deleteSingleFilm('${f.id}')">Supprimer</button>
|
|
</td>`;
|
|
tbody.appendChild(tr);
|
|
});
|
|
}
|
|
|
|
// ── Sélection de masse ──
|
|
function toggleSelectAll(source) {
|
|
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
|
document.getElementById('bulk-actions-bar').style.display = source.checked ? 'flex' : 'none';
|
|
}
|
|
|
|
// 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',
|
|
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ ids, type: currentAdminTab })
|
|
});
|
|
loadDashboardData();
|
|
document.getElementById('bulk-actions-bar').style.display = 'none';
|
|
}
|
|
|
|
async function deleteSingleFilm(id) {
|
|
if (!confirm('Supprimer ce film ?')) 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.querySelectorAll('.tab-btn').forEach(b => b.classList.toggle('active', b.id === `btn-tab-${tabName}`));
|
|
renderAdminTable();
|
|
} |