const STORAGE_KEY = 'mon-cinema-films';
let films = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
let currentPubTab = 'critique';
let activeRatingFilter = 0; // 0 = tous
function switchPubTab(tabName) {
currentPubTab = tabName;
activeRatingFilter = 0;
document.querySelectorAll('.rating-filter-btn').forEach(btn => {
btn.classList.remove('active');
btn.querySelectorAll('.rf-star').forEach(s => s.classList.remove('filled'));
});
const tabCritiques = document.getElementById('tab-pub-critiques');
const tabVideotheque = document.getElementById('tab-pub-videotheque');
if (tabCritiques && tabVideotheque) {
if (tabName === 'critique') {
tabCritiques.classList.add('active');
tabVideotheque.classList.remove('active');
} else {
tabVideotheque.classList.add('active');
tabCritiques.classList.remove('active');
}
}
renderPublicGrid();
}
function filterByRating(stars) {
activeRatingFilter = (activeRatingFilter === stars) ? 0 : stars;
// Update button states
document.querySelectorAll('.rating-filter-btn').forEach(btn => {
const n = parseInt(btn.dataset.stars);
btn.classList.toggle('active', n === activeRatingFilter);
// Highlight filled stars up to active filter
btn.querySelectorAll('.rf-star').forEach((s, i) => {
s.classList.toggle('filled', activeRatingFilter > 0 && 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;
// Show rating filter only on critique tab
const filterBar = document.getElementById('rating-filter-bar');
if (filterBar) filterBar.style.display = currentPubTab === 'critique' ? 'flex' : 'none';
const filtered = films.filter(f => {
const fType = f.type || 'critique';
if (fType !== currentPubTab) return false;
if (currentPubTab === 'critique' && activeRatingFilter > 0) {
return (f.rating || f.note || 1) === activeRatingFilter;
}
return true;
});
if (countLabel) {
if (currentPubTab === 'critique') {
const suffix = activeRatingFilter > 0 ? ` · filtrées ${activeRatingFilter}★` : '';
countLabel.textContent = filtered.length + (filtered.length > 1 ? ' critiques' : ' critique') + suffix;
} else {
countLabel.textContent = filtered.length + (filtered.length > 1 ? ' films physiques' : ' film physique');
}
}
if (filtered.length === 0) {
grid.innerHTML = '';
if (emptyState) {
emptyState.style.display = 'block';
emptyState.querySelector('p').textContent = currentPubTab === 'critique'
? "Aucune critique pour l'instant."
: "Aucun film dans la vidéothèque pour l'instant.";
}
return;
}
if (emptyState) emptyState.style.display = 'none';
grid.innerHTML = filtered.map(f => {
const posterUrl = f.poster || f.image || f.affiche || '';
const movieTitle = f.title || f.titre || 'Sans titre';
const movieYear = f.year || f.annee || f.publish_date || '—';
const movieDirector = f.director || f.creators || f.realisateur || '—';
const posterHtml = posterUrl
? ``
: `
${reviewText}
${description}