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.review ? f.review : 'Aucun texte rédigé pour le moment.'}
${f.description ? f.description : 'Aucune description fournie.'}