let films = []; const API_URL = '../api.php'; let currentPubTab = 'critique'; let activeRatingFilter = 0; async function loadPublicData() { try { const response = await fetch(`${API_URL}?action=get_films`); films = await response.json(); renderPublicGrid(); } catch (error) { console.error("Erreur de récupération :", error); } } function switchPubTab(tabName) { currentPubTab = tabName; activeRatingFilter = 0; const filterBar = document.getElementById('rating-filter-bar'); if(filterBar) filterBar.style.display = (tabName === 'critique') ? 'flex' : 'none'; document.querySelectorAll('.rating-filter-btn').forEach(btn => { btn.classList.remove('active'); btn.querySelectorAll('.rf-star').forEach(s => s.classList.remove('filled')); }); document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); const activeBtn = document.getElementById(`tab-pub-${tabName}s`); if(activeBtn) activeBtn.classList.add('active'); renderPublicGrid(); } function filterByRating(stars) { if (currentPubTab !== 'critique') return; activeRatingFilter = (activeRatingFilter === stars) ? 0 : stars; document.querySelectorAll('.rating-filter-btn').forEach(btn => { const n = parseInt(btn.dataset.stars); btn.classList.toggle('active', n === activeRatingFilter); btn.querySelectorAll('.rf-star').forEach((s, i) => { s.classList.toggle('filled', i < activeRatingFilter); }); }); renderPublicGrid(); } function renderPublicGrid() { const grid = document.getElementById('grid'); const emptyState = document.getElementById('empty-state'); const countLabel = document.getElementById('count-label'); if (!grid) return; grid.innerHTML = ''; let filtered = films.filter(f => f.type === currentPubTab); if (currentPubTab === 'critique' && activeRatingFilter > 0) { filtered = filtered.filter(f => parseInt(f.rating) === activeRatingFilter); } if (countLabel) { countLabel.textContent = `${filtered.length} ${currentPubTab === 'critique' ? 'critique' : 'œuvre'}${filtered.length > 1 ? 's' : ''}`; } if (filtered.length === 0) { if (emptyState) emptyState.style.display = 'block'; return; } if (emptyState) emptyState.style.display = 'none'; filtered.forEach(f => { const card = document.createElement('div'); card.className = 'card'; card.onclick = () => openDetail(f.id); let posterHTML = f.poster ? `
${f.title}
` : `
Pas d'affiche
`; if (f.type === 'critique') { const starsHTML = '★'.repeat(f.rating) + `${'☆'.repeat(5 - f.rating)}`; card.innerHTML = ` ${posterHTML}

${f.title}

${f.year ? f.year + ' · ' : ''}${f.director || 'Réalisateur inconnu'}

${starsHTML}
`; } else { card.innerHTML = ` ${posterHTML}

${f.title}

${f.year ? f.year + ' · ' : ''}${f.director || 'Réalisateur inconnu'}

`; } grid.appendChild(card); }); } function openDetail(id) { const f = films.find(item => item.id == id); if (!f) return; const dPoster = document.getElementById('d-poster'); const dPosterWrap = document.getElementById('d-poster-wrap'); const dTitle = document.getElementById('d-title'); const dMeta = document.getElementById('d-meta'); const dBody = document.getElementById('d-body'); const detailModalLayout = document.getElementById('detail-modal-layout'); const detailOverlay = document.getElementById('detail-overlay'); if (f.poster) { if (dPoster) dPoster.src = f.poster; if (dPosterWrap) { dPosterWrap.style.display = 'block'; dPosterWrap.className = `detail-poster poster-${f.type}`; } if (detailModalLayout) detailModalLayout.classList.remove('no-poster'); } else { if (dPosterWrap) dPosterWrap.style.display = 'none'; if (detailModalLayout) detailModalLayout.classList.add('no-poster'); } if (dTitle) dTitle.textContent = f.title; if (dMeta) dMeta.textContent = `${f.year ? f.year + ' | ' : ''}${f.director || 'Réalisateur inconnu'}`; if (dBody) { // CORRECTION : Les template literals sont maintenant correctement fermés if (f.type === 'critique') { const stars = '★'.repeat(f.rating) + `${'☆'.repeat(5 - f.rating)}`; dBody.innerHTML = `
${stars}

${f.review ? f.review : 'Aucun texte rédigé pour le moment.'}

${f.streaming ? `
Visionnage : ${f.streaming}
` : ''} `; } else { dBody.innerHTML = `
${f.format ? ` ${f.format}` : ''} ${f.length ? ` ${f.length} Min` : ''} ${f.number_of_discs ? ` ${f.number_of_discs} Disque(s)` : ''}
Éditeur ${f.publisher || '—'}
Format Image ${f.aspect_ratio || '—'}
Code Barre (EAN) ${f.ean_isbn13 || '—'}

Synopsis / Notes

${f.description ? f.description : 'Aucune description fournie.'}

`; } } if (detailOverlay) detailOverlay.classList.add('open'); } function closeDetail() { const detailOverlay = document.getElementById('detail-overlay'); if (detailOverlay) detailOverlay.classList.remove('open'); } document.addEventListener('DOMContentLoaded', loadPublicData);