const API_URL = '../api.php'; let films = []; let currentPubTab = 'critique'; let activeRatingFilter = 0; let activeStreamingFilter = ''; let searchQuery = ''; let currentPage = 1; const itemsPerPage = 12; function getStarsHTML(rating) { const r = parseFloat(rating) || 0; const full = Math.floor(r); const hasHalf = (r - full) >= 0.5; const empty = 5 - Math.ceil(r); let html = '★'.repeat(full); if (hasHalf) html += '★'; html += `${'☆'.repeat(empty)}`; return html; } function generateStreamingSelect() { const select = document.getElementById('pub-streaming-select'); if (!select) return; const platforms = new Set(); films.forEach(f => { if (f.type === 'critique' && f.streaming && f.streaming !== 'Disponible en support physique ou Cinéma' && f.streaming.trim() !== '') { f.streaming.split(',').forEach(p => { const platform = p.trim(); if (platform.length > 0) platforms.add(platform); }); } }); const sortedPlatforms = Array.from(platforms).sort(); select.innerHTML = ''; sortedPlatforms.forEach(platform => { const option = document.createElement('option'); option.value = platform; option.textContent = platform; select.appendChild(option); }); select.addEventListener('change', (e) => { activeStreamingFilter = e.target.value; currentPage = 1; renderPublicGrid(); }); } async function loadPublicData() { try { const response = await fetch(`${API_URL}?action=get_films`, { cache: 'default' }); const data = await response.json(); // ✅ CORRECTION : Fusionner les deux tableaux en un seul films = [...(data.critique || []), ...(data.videotheque || [])]; generateStreamingSelect(); // Écouteur pour le filtre physique const physicalCheckbox = document.getElementById('physical-only-checkbox'); if (physicalCheckbox) { physicalCheckbox.addEventListener('change', () => { currentPage = 1; renderPublicGrid(); }); } renderPublicGrid(); } catch (error) { console.error("Erreur de récupération :", error); } } function switchPubTab(tabName) { currentPubTab = tabName; activeRatingFilter = 0; activeStreamingFilter = ''; currentPage = 1; const select = document.getElementById('pub-streaming-select'); if (select) select.value = ''; const ratingBar = document.getElementById('rating-filter-bar'); if (ratingBar) ratingBar.style.display = (tabName === 'critique') ? 'flex' : 'none'; if (select) select.style.display = (tabName === 'critique') ? 'block' : 'none'; // 🔥 Cacher la case physique en mode vidéothèque const physicalBox = document.querySelector('.physical-filter-box'); if (physicalBox) { physicalBox.style.display = (tabName === 'critique') ? 'block' : '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); // 🔥 FILTRE PHYSIQUE UNIQUEMENT (CORRIGÉ) const physicalFilter = document.getElementById('physical-only-checkbox'); if (physicalFilter && physicalFilter.checked) { filtered = filtered.filter(f => { // Pour les critiques : vérifier le champ streaming if (f.type === 'critique') { return f.streaming && f.streaming.toLowerCase().includes('support physique'); } // Pour la vidéothèque : tout est physique par défaut return true; }); } if (currentPubTab === 'critique' && activeRatingFilter > 0) { filtered = filtered.filter(f => { const rating = parseFloat(f.rating) || 0; return Math.round(rating) === activeRatingFilter; }); } if (currentPubTab === 'critique' && activeStreamingFilter) { const filterLower = activeStreamingFilter.toLowerCase(); filtered = filtered.filter(f => { if (!f.streaming || f.streaming === 'Disponible en support physique ou Cinéma') return false; const platforms = f.streaming.split(',').map(p => p.trim().toLowerCase()); return platforms.includes(filterLower); }); } 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'; 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); 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.'}
Réalisation : ${f.director ? f.director : 'Non renseignée'}
Casting : ${f.actors ? f.actors : 'Non renseigné'}
${f.description ? f.description : 'Aucun synopsis disponible pour ce support.'}