let films = []; const API_URL = '../api.php'; let currentPubTab = 'critique'; let activeRatingFilter = 0; let activeStreamingFilter = ''; let searchQuery = ''; let currentPage = 1; const itemsPerPage = 12; let physicalOnlyFilter = false; 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(); // Fixed: S et -> Set films.forEach(f => { // Fixed: = > -> => 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' }); films = await response.json(); generateStreamingSelect(); // Écouteur pour le filtre physique const physicalCheckbox = document.getElementById('physical-only-checkbox'); if (physicalCheckbox) { physicalCheckbox.addEventListener('change', (e) => { physicalOnlyFilter = e.target.checked; 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'; 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 UNIQUENENT 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'); } }); } 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' : ''}`; // Fixed: filtere d -> filtered } if (filtered.length === 0) { if (emptyState) emptyState.style.display = 'block'; // Fixed: di splay -> display 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.title}
` : `
Pas d'affiche
`; if (f.type === 'critique') { const starsHTML = getStarsHTML(f.rating) + `${parseFloat(f.rating)}`; card.innerHTML = ` ${posterHTML}

${f.title}

${f.director || 'Réalisateur inconnu'}

${f.year || '-'}

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

${f.title}

${f.director || 'Réalisateur inconnu'}

${f.year || '-'}

`; } grid.appendChild(card); }); renderPagination(totalPages); } function renderPagination(totalPages) { const container = document.getElementById('pub-pagination'); if (!container) return; container.innerHTML = ''; if (totalPages <= 1) return; const info = document.createElement('span'); info.className = 'pub-pagination-info'; info.textContent = `Page ${currentPage} / ${totalPages}`; container.appendChild(info); const prevBtn = document.createElement('button'); // Fixed: p revBtn -> prevBtn prevBtn.innerHTML = ''; prevBtn.disabled = currentPage === 1; prevBtn.onclick = () => { currentPage--; renderPublicGrid(); window.scrollTo({ top: 0, behavior: 'smooth' }); }; container.appendChild(prevBtn); const maxButtons = 5; let startPage = Math.max(1, currentPage - Math.floor(maxButtons / 2)); // Fixed: curren tPage -> currentPage let endPage = Math.min(totalPages, startPage + maxButtons - 1); if (endPage - startPage + 1 < maxButtons) { startPage = Math.max(1, endPage - maxButtons + 1); } if (startPage > 1) { container.appendChild(createPubPageBtn(1)); if (startPage > 2) container.appendChild(createPubEllipsis()); } for (let i = startPage; i <= endPage; i++) { container.appendChild(createPubPageBtn(i)); } if (endPage < totalPages) { if (endPage < totalPages - 1) container.appendChild(createPubEllipsis()); container.appendChild(createPubPageBtn(totalPages)); } const nextBtn = document.createElement('button'); nextBtn.innerHTML = ''; // Fixed: i nnerHTML -> innerHTML nextBtn.disabled = currentPage === totalPages; nextBtn.onclick = () => { currentPage++; renderPublicGrid(); window.scrollTo({ top: 0, behavior: 'smooth' }); }; container.appendChild(nextBtn); } function createPubPageBtn(num) { const btn = document.createElement('button'); btn.textContent = num; if (num === currentPage) btn.classList.add('active'); btn.onclick = () => { currentPage = num; renderPublicGrid(); window.scrollTo({ top: 0, behavior: 'smooth' }); }; return btn; } function createPubEllipsis() { const span = document.createElement('span'); span.textContent = '...'; span.style.color = 'var(--muted)'; span.style.padding = '0 0.5rem'; return span; } 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'); // Fixed: detailModalLa yout 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}`; // Fixed: 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'}`; // Fixed: Réa lisateur if (dBody) { if (f.type === 'critique') { const stars = getStarsHTML(f.rating) + `${parseFloat(f.rating)}/5`; 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(); const searchInput = document.getElementById('pub-search-input'); if (searchInput) { searchInput.addEventListener('input', (e) => { searchQuery = e.target.value; currentPage = 1; renderPublicGrid(); }); } });