function getQueryParameter(name) { const urlParams = new URLSearchParams(window.location.search); return urlParams.get(name); } async function fetchDepartures(afkorting) { const apiUrl = `/vertrektijden/proxy.php?Afkorting=${encodeURIComponent(afkorting)}`; const response = await fetch(apiUrl); if (response.ok) { const data = await response.json(); displayDepartures(data); } else { console.error('Failed to fetch data:', response.status); document.getElementById('departures').innerHTML = `
Fout bij ophalen vertrektijden
`; } } function formatDelay(seconds) { if (!seconds || seconds < 1) return "Op tijd"; let minutes = Math.floor(seconds / 60); return `+${minutes} min`; } function getDelayClass(delay) { if (!delay || delay < 60) return "bg-success"; if (delay < 300) return "bg-warning"; return "bg-danger"; } function isKingsdayNow() { const parts = new Intl.DateTimeFormat('nl-NL', { timeZone: 'Europe/Amsterdam', month: 'numeric', day: 'numeric', hour: 'numeric', hour12: false }).formatToParts(new Date()); const get = (type) => parseInt(parts.find(p => p.type === type).value); const month = get('month'); const day = get('day'); const hour = get('hour'); console.log("NL:", { month, day, hour }); if (month !== 4) return false; if (day === 26 && hour >= 16) return true; if (day === 27) return true; if (day === 28 && hour < 4) return true; return false; } console.log(new Date()); console.log("Kingsday:", isKingsdayNow()); function displayDepartures(data) { const departuresDiv = document.getElementById('departures'); departuresDiv.innerHTML = ''; const isKingsday = isKingsdayNow(); console.log("Kingsday NOW:", isKingsdayNow()); if (data.error) { departuresDiv.innerHTML = `
${data.error}
`; return; } let html = `
`; if (data.departures && data.departures.length > 0) { data.departures.forEach(departure => { const cleanDestinationPlanned = (departure.destination_planned || '').replace(/[{}]/g, '').trim(); const cleanDestinationActual = (departure.destination_actual || '').replace(/[{}]/g, '').trim(); const cleanVia = (departure.via || '').replace(/[{}]/g, '').trim(); const time = new Date(departure.departure_time) .toLocaleTimeString('nl-NL', { hour: '2-digit', minute: '2-digit' }); const delayClass = getDelayClass(departure.delay); const isCancelled = departure.cancelled === true; /* ===================== BUS CHECK ====================== */ const ritTekst = ` ${departure.company ?? ''} ${departure.type ?? ''} ${departure.line_number ?? ''} `.toLowerCase(); const isBus = ritTekst.includes('snelbus') || ritTekst.includes('stopbus'); const isMetro = ritTekst.includes('metro'); /* ===================== MATERIEEL ====================== */ let materialParts = []; if (isBus) { materialParts.push(` `); } else if (isMetro) { materialParts.push(` `); } else { const ritNummer = parseInt(departure.service_number, 10); if (ritNummer >= 20200 && ritNummer <= 20299) { materialParts.push(` `); } else if (departure.wings) { departure.wings.forEach(wing => { if (!wing.material) return; wing.material.forEach(mat => { let srcType = mat.type .replace(/\//g, "-") .replace(/ /g, "-"); const imgStyle = mat.remains_behind ? 'filter: grayscale(100%)' : ''; materialParts.push(`
${mat.number ?? ''} ${mat.remains_behind ? `Niet instappen` : ''} ${mat.added ? `Wordt gekoppeld` : ''}
`); }); }); } } /* Alles in één scrollbare rij, scrollbaar zodra > 400px */ let materialHtml = ''; if (materialParts.length) { materialHtml = `
${materialParts.join('')}
`; } /* ===================== REMARKS ====================== */ let remarksHtml = ''; if (departure.remarks) { let remarksArray = Array.isArray(departure.remarks) ? departure.remarks : [departure.remarks]; remarksArray.forEach(r => { const text = (r.text || r).replace(/[{}]/g, '').trim(); if (!text) return; remarksHtml += `
${text}
`; }); } /* ===================== BESTEMMING ====================== */ let bestemmingHtml = ` ${cleanDestinationActual || cleanDestinationPlanned} `; /* ===================== PLATFORM ====================== */ let platformBadge = departure.platform_actual === departure.platform_planned ? `${departure.platform_actual}` : `${departure.platform_actual}`; /* ===================== ROW ====================== */ html += ` `; }); } else { html += ` `; } html += `
Tijd Bestemming Spoor Rit
${time}
${ isCancelled ? `Geannuleerd` : `${formatDelay(departure.delay)}` }
${ isCancelled ? '' : ` -- ` }
${!isCancelled && isKingsdayNow() === true ? '👑' : ''} ${bestemmingHtml}
${cleanVia ? `Via ${cleanVia}` : ''}
${materialHtml}
${remarksHtml}
${platformBadge} ${departure.company} ${departure.line_number ? `${departure.line_number}` : departure.type} ${departure.service_number}
Geen vertrekken bekend
`; departuresDiv.innerHTML = html; startCountdowns(); } function startCountdowns() { const elements = document.querySelectorAll(".countdown"); function update() { const now = new Date().getTime(); elements.forEach(el => { const departureTime = new Date(el.dataset.departure).getTime(); const delay = parseInt(el.dataset.delay || 0) * 1000; const realDeparture = departureTime + delay; let diff = Math.floor((realDeparture - now) / 1000); if (diff <= 0) { el.innerHTML = "Vertrokken"; el.className = "text-danger fw-bold"; return; } if (diff < 60) { el.innerHTML = "Vertrekt binnenkort"; el.className = "text-warning fw-bold"; return; } const minutes = Math.floor(diff / 60); const seconds = diff % 60; el.innerHTML = `${minutes}m ${seconds}s`; el.className = "text-success"; }); } update(); setInterval(update, 1000); } /* =============================== INIT ================================ */ async function init() { const stationNaam = getQueryParameter('Naam'); if (!stationNaam) { document.getElementById('departures').innerHTML = `
Geen station opgegeven
`; return; } const response = await fetch(`/vertrektijden/getAfkorting.php?Naam=${encodeURIComponent(stationNaam)}`); if (!response.ok) { document.getElementById('departures').innerHTML = `
Fout bij ophalen station
`; return; } const data = await response.json(); if (!data.afkorting) { document.getElementById('departures').innerHTML = `
Station niet gevonden
`; return; } fetchDepartures(data.afkorting); setInterval(() => fetchDepartures(data.afkorting), 10000); } init();