122 lines
4.6 KiB
JavaScript
122 lines
4.6 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);
|
|
document.getElementById('admin-count-label').textContent = `${filtered.length} élément(s)`;
|
|
|
|
filtered.forEach(f => {
|
|
const tr = document.createElement('tr');
|
|
tr.innerHTML = `
|
|
<td style="text-align:center;">
|
|
<input type="checkbox" class="film-checkbox" value="${f.id}">
|
|
</td>
|
|
<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>'}
|
|
</td>
|
|
<td><strong>${f.title}</strong></td>
|
|
<td>${f.year || '-'}</td>
|
|
<td>${f.director || '-'}</td>
|
|
<td>${currentAdminTab === 'critique' ? `<span class="tbl-stars">${'★'.repeat(f.rating || 0)}</span>` : `<span class="badge-format">${f.format || '-'}</span>`}</td>
|
|
<td>
|
|
<div class="tbl-actions">
|
|
<button onclick="openEditModal('${f.id}')" title="Éditer"><i class="ti ti-edit"></i></button>
|
|
<button class="del" onclick="deleteSingleFilm('${f.id}')" title="Supprimer"><i class="ti ti-trash"></i></button>
|
|
</div>
|
|
</td>`;
|
|
tbody.appendChild(tr);
|
|
});
|
|
}
|
|
|
|
// 1. Sélectionner tout
|
|
function toggleSelectAll(source) {
|
|
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
|
updateBulkBar();
|
|
}
|
|
|
|
// 2. Mettre à jour la barre (appelée par chaque clic sur une checkbox)
|
|
function updateBulkBar() {
|
|
const checked = document.querySelectorAll('.film-checkbox:checked');
|
|
const bulkBar = document.getElementById('bulk-actions-bar');
|
|
const bulkCount = document.getElementById('bulk-count');
|
|
|
|
if (checked.length > 0) {
|
|
bulkBar.style.display = 'flex';
|
|
bulkCount.textContent = checked.length;
|
|
} else {
|
|
bulkBar.style.display = 'none';
|
|
document.getElementById('select-all-checkbox').checked = false;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
});
|
|
|
|
// Remplacez les deux fonctions de suppression par celles-ci :
|
|
|
|
// Variable pour stocker la fonction à appeler après confirmation
|
|
let pendingDeleteAction = null;
|
|
|
|
function showConfirmModal(actionFn) {
|
|
pendingDeleteAction = actionFn;
|
|
document.getElementById('confirm-modal').classList.add('open');
|
|
}
|
|
|
|
function closeConfirmModal() {
|
|
document.getElementById('confirm-modal').classList.remove('open');
|
|
}
|
|
|
|
// Bouton supprimer dans la modale
|
|
document.getElementById('confirm-btn').addEventListener('click', () => {
|
|
if (pendingDeleteAction) pendingDeleteAction();
|
|
closeConfirmModal();
|
|
});
|
|
|
|
// Mise à jour de la suppression de masse
|
|
async function executeBulkDelete() {
|
|
showConfirmModal(async () => {
|
|
const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value);
|
|
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';
|
|
});
|
|
}
|
|
|
|
// Mise à jour de la suppression unitaire
|
|
async function deleteSingleFilm(id) {
|
|
showConfirmModal(async () => {
|
|
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();
|
|
} |