File size: 2,753 Bytes
7461893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33bb0d3
7461893
 
 
33bb0d3
7461893
 
 
 
 
 
 
 
 
 
 
 
 
 
33bb0d3
7461893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33bb0d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 = '<tr><td colspan="8" class="no-data">No ticket data available</td></tr>';
            return;
        }

        tickets.forEach(ticket => {
            const row = document.createElement('tr');
            row.innerHTML = `
                <td>${ticket.ticket_number}</td>
                <td>${ticket.name}</td>
                <td>${ticket.email}</td>
                <td>${ticket.phone}</td>
                <td>${ticket.tickets_count}</td>
                <td>${ticket.country}</td>
                <td>${ticket.region}</td>
                <td>${formatDate(ticket.timestamp)}</td>
            `;
            ticketsBody.appendChild(row);
        });
    }

    // Update statistics cards
    function updateStats(tickets) {
        totalTicketsEl.textContent = tickets.length;
        
        const totalCount = tickets.reduce((sum, ticket) => sum + ticket.tickets_count, 0);
        totalCountEl.textContent = totalCount;
        
        const uniqueCountries = new Set(tickets.map(ticket => ticket.country));
        uniqueCountriesEl.textContent = uniqueCountries.size;
    }

    // Format date for display
    function formatDate(dateString) {
        const options = { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' };
        return new Date(dateString).toLocaleDateString('en-US', options);
    }

    // Download CSV
    function downloadCSV() {
        window.location.href = '/download_csv';
    }
});