document.addEventListener('DOMContentLoaded', function() { // DOM Elements const ticketsBody = document.getElementById('ticketsBody'); const refreshBtn = document.getElementById('refreshBtn'); const downloadBtn = document.getElementById('downloadBtn'); const totalTicketsEl = document.getElementById('total-tickets'); const totalCountEl = document.getElementById('total-count'); const uniqueCountriesEl = document.getElementById('unique-countries'); // Load data when page loads fetchData(); // Event listeners refreshBtn.addEventListener('click', fetchData); downloadBtn.addEventListener('click', downloadCSV); // Fetch data from server function fetchData() { fetch('/get_tickets') .then(response => response.json()) .then(data => { renderTable(data.tickets); updateStats(data.tickets); }) .catch(error => { console.error('Error fetching data:', error); alert('Error fetching data. Please try again.'); }); } // Render table with ticket data function renderTable(tickets) { ticketsBody.innerHTML = ''; if (tickets.length === 0) { ticketsBody.innerHTML = '