211 lines
7.4 KiB
JavaScript
211 lines
7.4 KiB
JavaScript
const API_URL = '../api.php';
|
|
let allItems = [];
|
|
let currentAdminTab = 'critique';
|
|
let editingItemId = null;
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadDashboardData();
|
|
initModals();
|
|
initForms();
|
|
});
|
|
|
|
async function loadDashboardData() {
|
|
try {
|
|
const res = await fetch(`${API_URL}?action=get_films`);
|
|
if (!res.ok) throw new Error('Erreur de chargement');
|
|
allItems = await res.json();
|
|
renderAdminTable();
|
|
} catch (error) {
|
|
console.error("Erreur loadDashboardData:", error);
|
|
}
|
|
}
|
|
|
|
function renderAdminTable() {
|
|
const tbody = document.getElementById('admin-table-body');
|
|
if (!tbody) return;
|
|
tbody.innerHTML = '';
|
|
|
|
const filtered = allItems.filter(item => item.type === currentAdminTab);
|
|
const countLabel = document.getElementById('admin-count-label');
|
|
if (countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
|
|
|
|
// BUG CORRIGÉ : f = > remplacé par f =>
|
|
// Nettoyage des espaces parasites dans les balises HTML
|
|
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);
|
|
});
|
|
}
|
|
|
|
function toggleSelectAll(source) {
|
|
document.querySelectorAll('.film-checkbox').forEach(cb => cb.checked = source.checked);
|
|
updateBulkBar();
|
|
}
|
|
|
|
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';
|
|
const selectAll = document.getElementById('select-all-checkbox');
|
|
if(selectAll) selectAll.checked = false;
|
|
}
|
|
}
|
|
|
|
document.addEventListener('change', (e) => {
|
|
if (e.target.classList.contains('film-checkbox')) {
|
|
updateBulkBar();
|
|
}
|
|
});
|
|
|
|
let pendingDeleteAction = null;
|
|
function showConfirmModal(actionFn) {
|
|
pendingDeleteAction = actionFn;
|
|
const modal = document.getElementById('confirm-modal');
|
|
if(modal) modal.classList.add('open');
|
|
}
|
|
|
|
function closeConfirmModal() {
|
|
const modal = document.getElementById('confirm-modal');
|
|
if(modal) modal.classList.remove('open');
|
|
}
|
|
|
|
document.getElementById('confirm-btn')?.addEventListener('click', () => {
|
|
if (pendingDeleteAction) pendingDeleteAction();
|
|
closeConfirmModal();
|
|
});
|
|
|
|
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();
|
|
updateBulkBar();
|
|
});
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
// --- AJOUTS FONCTIONNELS MANQUANTS ---
|
|
|
|
function openEditModal(id) {
|
|
const item = allItems.find(i => i.id == id);
|
|
if (!item) return;
|
|
|
|
editingItemId = id;
|
|
const modal = document.getElementById('edit-modal');
|
|
if (!modal) return;
|
|
|
|
const setVal = (id, val) => { const el = document.getElementById(id); if (el) el.value = val || ''; };
|
|
setVal('film-title', item.title);
|
|
setVal('film-year', item.year);
|
|
setVal('film-director', item.director);
|
|
setVal('film-poster', item.poster);
|
|
setVal('film-rating', item.rating);
|
|
setVal('film-review', item.review);
|
|
setVal('film-streaming', item.streaming);
|
|
setVal('film-format', item.format);
|
|
setVal('film-length', item.length);
|
|
setVal('film-publisher', item.publisher);
|
|
setVal('film-aspect-ratio', item.aspect_ratio);
|
|
setVal('film-ean', item.ean_isbn13);
|
|
setVal('film-discs', item.number_of_discs);
|
|
setVal('film-description', item.description);
|
|
|
|
modal.classList.add('open');
|
|
}
|
|
|
|
function initModals() {
|
|
document.querySelectorAll('.modal-close, .btn-cancel').forEach(btn => {
|
|
btn.addEventListener('click', (e) => {
|
|
const overlay = e.target.closest('.overlay');
|
|
if (overlay) overlay.classList.remove('open');
|
|
});
|
|
});
|
|
document.querySelectorAll('.overlay').forEach(overlay => {
|
|
overlay.addEventListener('click', (e) => {
|
|
if (e.target === overlay) overlay.classList.remove('open');
|
|
});
|
|
});
|
|
}
|
|
|
|
function initForms() {
|
|
const filmForm = document.getElementById('film-form');
|
|
if (filmForm) {
|
|
filmForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const formData = new FormData(filmForm);
|
|
const data = Object.fromEntries(formData.entries());
|
|
data.id = editingItemId;
|
|
data.type = currentAdminTab;
|
|
|
|
const action = editingItemId ? 'update_film' : 'add_film';
|
|
await fetch(`${API_URL}?action=${action}`, {
|
|
method: 'POST',
|
|
headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data)
|
|
});
|
|
|
|
document.getElementById('edit-modal')?.classList.remove('open');
|
|
editingItemId = null;
|
|
loadDashboardData();
|
|
});
|
|
}
|
|
|
|
const csvInput = document.getElementById('csv-file');
|
|
if (csvInput) {
|
|
csvInput.addEventListener('change', async (e) => {
|
|
const file = e.target.files[0];
|
|
if (!file) return;
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
formData.append('type', currentAdminTab);
|
|
|
|
await fetch(`${API_URL}?action=import_csv`, {
|
|
method: 'POST',
|
|
headers: { 'Authorization': localStorage.getItem('token') },
|
|
body: formData
|
|
});
|
|
loadDashboardData();
|
|
});
|
|
}
|
|
} |