document.addEventListener('DOMContentLoaded', function() {
const trainSearchForm = document.getElementById('train-search-form');
if (trainSearchForm) {
trainSearchForm.addEventListener('submit', function(e) {
e.preventDefault();
const from = document.getElementById('from-station').value;
const to = document.getElementById('to-station').value;
const date = document.getElementById('search-date').value.replace(/-/g, '');
const resultDiv = document.getElementById('train-search-result');
resultDiv.innerHTML = '
';
fetch(`api/train_search_handler.php?from=${from}&to=${to}&date=${date}`)
.then(response => response.json())
.then(data => {
if(data.error) {
resultDiv.innerHTML = `${data.error}
`;
} else if (data.ResponseCode == 200 && data.Trains.length > 0) {
// This part is a placeholder for when the API supports it.
resultDiv.innerHTML = `Found trains! (Display logic to be implemented)
`;
} else {
resultDiv.innerHTML = `${data.Message || 'No trains found.'}
`;
}
})
.catch(err => {
console.error('Error:', err);
resultDiv.innerHTML = `An error occurred.
`;
});
});
}
});