document.addEventListener('DOMContentLoaded', function() {
const liveStationForm = document.getElementById('live-station-form');
if (liveStationForm) {
liveStationForm.addEventListener('submit', function(e) {
e.preventDefault();
const stationCode = document.getElementById('station-code').value;
const hours = document.getElementById('hours').value;
const resultDiv = document.getElementById('live-station-result');
resultDiv.innerHTML = '
';
fetch(`api/live_station_handler.php?station_code=${stationCode}&hours=${hours}`)
.then(response => response.json())
.then(data => {
if(data.ResponseCode == 200 && data.Trains.length > 0) {
let trainsHtml = '';
data.Trains.forEach(train => {
trainsHtml += `
| ${train.TrainNo} |
${train.TrainName} |
${train.Source} |
${train.Destination} |
${train.ExpectedArrival} |
${train.Platform} |
`;
});
resultDiv.innerHTML = `
Trains arriving at ${data.Station.StationName}
| Train No | Train Name | From | To | ETA | Platform |
${trainsHtml}
`;
} else {
resultDiv.innerHTML = `${data.Message || 'No trains found for this station in the selected time frame.'}
`;
}
})
.catch(err => {
console.error('Error:', err);
resultDiv.innerHTML = `An error occurred.
`;
});
});
}
});