let films = []; const API_URL = '../api.php'; let currentPubTab = 'critique'; let activeRatingFilter = 0; let activeStreamingFilter = ''; // Nouveau filtre let searchQuery = ''; // Si vous avez ajouté la recherche let currentPage = 1; const itemsPerPage = 12; async function loadPublicData() { try { const response = await fetch(`${API_URL}?action=get_films`); films = await response.json(); renderPublicGrid(); generateStreamingFilters(); // Générer les filtres dynamiquement } catch (error) { console.error("Erreur de récupération :", error); } } // ── GÉNÉRATION DYNAMIQUE DES FILTRES STREAMING ── function generateStreamingFilters() { const container = document.getElementById('streaming-filter-buttons'); if (!container) return; // Extraire toutes les plateformes uniques const platforms = new Set(); films.forEach(f => { if (f.type === 'critique' && f.streaming && f.streaming !== 'Disponible en support physique ou Cinéma') { // Séparer par virgule si plusieurs plateformes f.streaming.split(',').forEach(p => { const platform = p.trim(); if (platform) platforms.add(platform); }); } }); // Trier alphabétiquement const sortedPlatforms = Array.from(platforms).sort(); // Bouton "Toutes" const allBtn = document.createElement('button'); allBtn.className = 'streaming-filter-btn active'; allBtn.textContent = 'Toutes'; allBtn.onclick = () => filterByStreaming(''); container.appendChild(allBtn); // Boutons pour chaque plateforme sortedPlatforms.forEach(platform => { const btn = document.createElement('button'); btn.className = 'streaming-filter-btn'; btn.textContent = platform; btn.onclick = () => filterByStreaming(platform); container.appendChild(btn); }); } function switchPubTab(tabName) { currentPubTab = tabName; activeRatingFilter = 0; activeStreamingFilter = ''; // Reset currentPage = 1; const filterBar = document.getElementById('rating-filter-bar'); if(filterBar) filterBar.style.display = (tabName === 'critique') ? 'flex' : 'none'; const streamingBar = document.getElementById('streaming-filter-bar'); if(streamingBar) streamingBar.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')); }); // Reset filtres streaming document.querySelectorAll('.streaming-filter-btn').forEach(btn => { btn.classList.remove('active'); }); const allBtn = document.querySelector('.streaming-filter-btn'); if(allBtn) allBtn.classList.add('active'); 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(); } // ── NOUVEAU : FILTRE PAR STREAMING ─ function filterByStreaming(platform) { if (currentPubTab !== 'critique') return; activeStreamingFilter = platform; document.querySelectorAll('.streaming-filter-btn').forEach(btn => { btn.classList.toggle('active', btn.textContent === (platform || 'Toutes')); }); 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 = ''; // 1. Filtrage let filtered = films.filter(f => f.type === currentPubTab); if (currentPubTab === 'critique' && activeRatingFilter > 0) { filtered = filtered.filter(f => Math.round(parseFloat(f.rating)) === activeRatingFilter); } // NOUVEAU : Filtre par plateforme if (currentPubTab === 'critique' && activeStreamingFilter) { filtered = filtered.filter(f => { if (!f.streaming || f.streaming === 'Disponible en support physique ou Cinéma') return false; return f.streaming.split(',').map(p => p.trim()).includes(activeStreamingFilter); }); } if (searchQuery) { const q = searchQuery.toLowerCase(); filtered = filtered.filter(f => f.title.toLowerCase().includes(q) || (f.director && f.director.toLowerCase().includes(q)) ); } if (countLabel) { countLabel.textContent = `${filtered.length} ${currentPubTab === 'critique' ? 'critique' : 'œuvre'}${filtered.length > 1 ? 's' : ''}`; } if (filtered.length === 0) { if (emptyState) emptyState.style.display = 'block'; renderPagination(0); return; } if (emptyState) emptyState.style.display = 'none'; // 2. Pagination const totalPages = Math.ceil(filtered.length / itemsPerPage) || 1; if (currentPage > totalPages) currentPage = totalPages; const startIdx = (currentPage - 1) * itemsPerPage; const pageItems = filtered.slice(startIdx, startIdx + itemsPerPage); // 3. Rendu des cartes pageItems.forEach(f => { const card = document.createElement('div'); card.className = 'card'; card.onclick = () => openDetail(f.id); let posterHTML = f.poster ? `
${f.director || 'Réalisateur inconnu'}
${f.director || 'Réalisateur inconnu'}
${f.review ? f.review : 'Aucun texte rédigé pour le moment.'}
${f.description ? f.description : 'Aucune description fournie.'}