diff --git a/js/public.js b/js/public.js index 0d77cbf..f4e0244 100644 --- a/js/public.js +++ b/js/public.js @@ -2,6 +2,9 @@ let films = []; const API_URL = '../api.php'; let currentPubTab = 'critique'; let activeRatingFilter = 0; +let searchQuery = ''; +let currentPage = 1; +const itemsPerPage = 12; async function loadPublicData() { try { @@ -16,8 +19,13 @@ async function loadPublicData() { function switchPubTab(tabName) { currentPubTab = tabName; activeRatingFilter = 0; + currentPage = 1; + searchQuery = ''; + const searchInput = document.getElementById('pub-search-input'); + if (searchInput) searchInput.value = ''; + const filterBar = document.getElementById('rating-filter-bar'); - if(filterBar) filterBar.style.display = (tabName === 'critique') ? 'flex' : 'none'; + if (filterBar) filterBar.style.display = (tabName === 'critique') ? 'flex' : 'none'; document.querySelectorAll('.rating-filter-btn').forEach(btn => { btn.classList.remove('active'); @@ -25,13 +33,15 @@ function switchPubTab(tabName) { }); document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); const activeBtn = document.getElementById(`tab-pub-${tabName}s`); - if(activeBtn) activeBtn.classList.add('active'); + if (activeBtn) activeBtn.classList.add('active'); renderPublicGrid(); } function filterByRating(stars) { if (currentPubTab !== 'critique') return; activeRatingFilter = (activeRatingFilter === stars) ? 0 : stars; + currentPage = 1; + document.querySelectorAll('.rating-filter-btn').forEach(btn => { const n = parseInt(btn.dataset.stars); btn.classList.toggle('active', n === activeRatingFilter); @@ -48,23 +58,41 @@ function renderPublicGrid() { 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 => parseInt(f.rating) === activeRatingFilter); + filtered = filtered.filter(f => Math.round(parseFloat(f.rating)) === activeRatingFilter); } + 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'; - - filtered.forEach(f => { + + // 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); @@ -73,21 +101,39 @@ function renderPublicGrid() { ? `
${f.director || 'Réalisateur inconnu'}
+ +${f.director || 'Réalisateur inconnu'}
+