Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const trackBtn = document.getElementById('track-btn');
+ const priceHistory = document.getElementById('price-history');
+ const sortPriceBtn = document.getElementById('sort-price');
+ const sortDateBtn = document.getElementById('sort-date');
+
+ let prices = [];
+
+ trackBtn.addEventListener('click', async () => {
+ const from = document.getElementById('from').value.toUpperCase();
+ const to = document.getElementById('to').value.toUpperCase();
+ const date = document.getElementById('date').value;
+
+ if (!from || !to || !date) {
+ alert('Please fill in all fields');
+ return;
+ }
+
+ try {
+ // Note: Replace with actual API key and endpoint
+ const response = await fetch(`https://api.aviationstack.com/v1/flights?access_key=YOUR_API_KEY&dep_iata=${from}&arr_iata=${to}&flight_date=${date}`);
+ const data = await response.json();
+
+ if (data.data) {
+ const price = {
+ date: new Date(),
+ amount: Math.floor(Math.random() * 1000) + 200, // Simulated price for demo
+ from,
+ to
+ };
+
+ prices.push(price);
+ updatePriceList();
+ updateChart();
+ }
+ } catch (error) {
+ console.error('Error fetching flight data:', error);
+ alert('Unable to fetch flight prices. Please try again later.');
+ }
+ });
+
+ function updatePriceList() {
+ priceHistory.innerHTML = '';
+ prices.forEach(price => {
+ const li = document.createElement('li');
+ li.innerHTML = `
+ ${price.from} → ${price.to}
+ ¥${price.amount}
+ ${price.date.toLocaleString()}
+ `;
+ priceHistory.appendChild(li);
+ });
+ }
+
+ function updateChart() {
+ // Implementation for chart visualization would go here
+ // Using canvas context to draw price history graph
+ const canvas = document.getElementById('priceChart');
+ const ctx = canvas.getContext('2d');
+ // Clear previous chart
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ // Draw new chart based on prices array
+ }
+
+ sortPriceBtn.addEventListener('click', () => {
+ prices.sort((a, b) => a.amount - b.amount);
+ updatePriceList();
+ });
+
+ sortDateBtn.addEventListener('click', () => {
+ prices.sort((a, b) => b.date - a.date);
+ updatePriceList();
+ });
+ });