abdullahalioo commited on
Commit
7461893
·
verified ·
1 Parent(s): 25ace4b

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +73 -38
script.js CHANGED
@@ -1,42 +1,77 @@
1
- $(document).ready(function() {
2
- // Download Excel button
3
- $('#downloadBtn').click(function() {
4
- window.location.href = '/download_excel';
5
- });
6
-
7
- // View ticket modal
8
- $('.view-btn').click(function() {
9
- const ticketId = $(this).data('id');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- // In a real app, you would fetch the ticket details from the server
12
- // For now, we'll just show a sample modal
13
- $('#viewTicketModal').modal('show');
14
- });
15
-
16
- // Delete ticket button
17
- $('.delete-btn').click(function() {
18
- const ticketId = $(this).data('id');
19
- if (confirm('Are you sure you want to delete this ticket?')) {
20
- // In a real app, you would send a DELETE request to the server
21
- alert('Ticket would be deleted here (implementation needed)');
22
  }
23
- });
24
-
25
- // Refresh button
26
- $('.refresh-btn').click(function() {
27
- location.reload();
28
- });
29
-
30
- // Search functionality
31
- $('.search-box input').on('keyup', function() {
32
- const value = $(this).val().toLowerCase();
33
- $('table tbody tr').filter(function() {
34
- $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
 
 
35
  });
36
- });
37
-
38
- // Simulate loading data
39
- setTimeout(function() {
40
- $('.table-responsive').addClass('loaded');
41
- }, 500);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  });
 
1
+ document.addEventListener('DOMContentLoaded', function() {
2
+ // DOM Elements
3
+ const ticketsBody = document.getElementById('ticketsBody');
4
+ const refreshBtn = document.getElementById('refreshBtn');
5
+ const downloadBtn = document.getElementById('downloadBtn');
6
+ const totalTicketsEl = document.getElementById('total-tickets');
7
+ const totalCountEl = document.getElementById('total-count');
8
+ const uniqueCountriesEl = document.getElementById('unique-countries');
9
+
10
+ // Load data when page loads
11
+ fetchData();
12
+
13
+ // Event listeners
14
+ refreshBtn.addEventListener('click', fetchData);
15
+ downloadBtn.addEventListener('click', downloadCSV);
16
+
17
+ // Fetch data from server
18
+ function fetchData() {
19
+ fetch('/get_tickets')
20
+ .then(response => response.json())
21
+ .then(data => {
22
+ renderTable(data.tickets);
23
+ updateStats(data.tickets);
24
+ })
25
+ .catch(error => {
26
+ console.error('Error fetching data:', error);
27
+ alert('Error fetching data. Please try again.');
28
+ });
29
+ }
30
+
31
+ // Render table with ticket data
32
+ function renderTable(tickets) {
33
+ ticketsBody.innerHTML = '';
34
 
35
+ if (tickets.length === 0) {
36
+ ticketsBody.innerHTML = '<tr><td colspan="8" class="no-data">No ticket data available</td></tr>';
37
+ return;
 
 
 
 
 
 
 
 
38
  }
39
+
40
+ tickets.forEach(ticket => {
41
+ const row = document.createElement('tr');
42
+ row.innerHTML = `
43
+ <td>${ticket.ticket_number}</td>
44
+ <td>${ticket.name}</td>
45
+ <td>${ticket.email}</td>
46
+ <td>${ticket.phone}</td>
47
+ <td>${ticket.tickets_count}</td>
48
+ <td>${ticket.country}</td>
49
+ <td>${ticket.region}</td>
50
+ <td>${formatDate(ticket.timestamp)}</td>
51
+ `;
52
+ ticketsBody.appendChild(row);
53
  });
54
+ }
55
+
56
+ // Update statistics cards
57
+ function updateStats(tickets) {
58
+ totalTicketsEl.textContent = tickets.length;
59
+
60
+ const totalCount = tickets.reduce((sum, ticket) => sum + ticket.tickets_count, 0);
61
+ totalCountEl.textContent = totalCount;
62
+
63
+ const uniqueCountries = new Set(tickets.map(ticket => ticket.country));
64
+ uniqueCountriesEl.textContent = uniqueCountries.size;
65
+ }
66
+
67
+ // Format date for display
68
+ function formatDate(dateString) {
69
+ const options = { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' };
70
+ return new Date(dateString).toLocaleDateString('en-US', options);
71
+ }
72
+
73
+ // Download CSV
74
+ function downloadCSV() {
75
+ window.location.href = '/download_csv';
76
+ }
77
  });