📅 Gestione Eventi

📅

Caricamento...

'); w.document.close(); w.print(); } // === Media Helper Functions === function youtubeToEmbed(url) { if (!url) return ''; if (url.includes('/embed/')) return url; let videoId = ''; const match = url.match(/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/shorts\/)([a-zA-Z0-9_-]{11})/); if (match) videoId = match[1]; return videoId ? 'https://www.youtube.com/embed/' + videoId : url; } function embedToYoutubeUrl(embedUrl) { if (!embedUrl) return ''; const match = embedUrl.match(/\/embed\/([a-zA-Z0-9_-]{11})/); return match ? 'https://www.youtube.com/watch?v=' + match[1] : embedUrl; } // ═══ Upload foto sul server ═══ async function uploadPhotosToStorage(files) { const status = document.getElementById('uploadStatus'); const imageList = document.getElementById('imageUrlList'); for (let i = 0; i < files.length; i++) { const file = files[i]; status.innerHTML = `⏳ Upload ${i+1}/${files.length}: ${file.name}...`; try { // Nome unico: timestamp + nome file const safeName = file.name.replace(/[^a-zA-Z0-9._-]/g, '_'); const filePath = `eventi/${Date.now()}_${safeName}`; const { data, error } = await db.storage .from('tour-media') .upload(filePath, file, { cacheControl: '3600', upsert: false }); if (error) throw error; // Ottieni URL pubblico const { data: urlData } = db.storage .from('tour-media') .getPublicUrl(filePath); const publicUrl = urlData.publicUrl; // Aggiungi alla lista (riempi primo campo vuoto o crea nuovo) const emptyInput = imageList.querySelector('.img-url-input:placeholder-shown'); if (emptyInput && !emptyInput.value) { emptyInput.value = publicUrl; } else { const div = document.createElement('div'); div.className = 'media-url-item'; div.innerHTML = ` `; imageList.appendChild(div); } } catch (err) { status.innerHTML = `❌ Errore ${file.name}: ${err.message}`; console.error('Upload error:', err); return; } } status.innerHTML = `✅ ${files.length} foto caricate!`; setTimeout(() => { status.innerHTML = ''; }, 3000); // Reset input file document.getElementById('photoFileInput').value = ''; updateMediaPreview(); } function addImageInput() { const list = document.getElementById('imageUrlList'); const item = document.createElement('div'); item.className = 'media-url-item'; item.innerHTML = ` `; list.appendChild(item); } function removeImageInput(btn) { const list = document.getElementById('imageUrlList'); if (list.children.length > 1) { btn.parentElement.remove(); } else { btn.parentElement.querySelector('input').value = ''; } updateMediaPreview(); } function updateMediaPreview() { const previewDiv = document.getElementById('formMediaPreview'); const videoUrl = document.getElementById('videoTrailer').value.trim(); const imageUrls = Array.from(document.querySelectorAll('.img-url-input')) .map(inp => inp.value.trim()).filter(u => u.length > 0); let html = ''; if (videoUrl) { const embed = youtubeToEmbed(videoUrl); const vidId = embed.split('/embed/')[1]?.split('?')[0] || ''; if (vidId) { html += `
`; } } imageUrls.forEach(url => { html += `
`; }); previewDiv.innerHTML = html; } function switchCardMedia(cardId, index, type, url, thumb) { const main = document.getElementById(cardId); main.parentElement.querySelectorAll('.card-media-thumb').forEach((t, i) => { t.classList.toggle('active', i === index); }); if (type === 'video') { main.innerHTML = `
🎬 Video`; } else { main.innerHTML = `📷 Foto`; } } function playCardVideo(cardId, videoUrl) { document.getElementById(cardId).innerHTML = ``; } loadEvents();