// Main JavaScript functionality for Ticket Collection System document.addEventListener('DOMContentLoaded', function() { initializeAdmin(); }); // Initialize admin dashboard functionality function initializeAdmin() { if (window.location.pathname === '/admin') { initializeTableFeatures(); initializeSearch(); initializeFilters(); updateLastUpdated(); } } // Initialize table sorting and filtering features function initializeTableFeatures() { const table = document.getElementById('ticketsTable'); if (!table) return; // Store original table data for filtering window.originalTableData = Array.from(table.querySelectorAll('tbody tr')); updateShowingCount(); } // Table sorting functionality let sortDirection = {}; function sortTable(columnIndex) { const table = document.getElementById('ticketsTable'); const tbody = table.querySelector('tbody'); const rows = Array.from(tbody.querySelectorAll('tr')); // Determine sort direction const currentDir = sortDirection[columnIndex] || 'asc'; const newDir = currentDir === 'asc' ? 'desc' : 'asc'; sortDirection[columnIndex] = newDir; // Update sort icons updateSortIcons(columnIndex, newDir); // Sort rows rows.sort((a, b) => { const aText = a.cells[columnIndex].textContent.trim(); const bText = b.cells[columnIndex].textContent.trim(); // Handle numeric columns if (columnIndex === 3) { // Tickets column const aNum = parseInt(aText) || 0; const bNum = parseInt(bText) || 0; return newDir === 'asc' ? aNum - bNum : bNum - aNum; } // Handle date columns if (columnIndex === 7) { // Timestamp column const aDate = new Date(aText); const bDate = new Date(bText); return newDir === 'asc' ? aDate - bDate : bDate - aDate; } // Handle text columns const comparison = aText.localeCompare(bText); return newDir === 'asc' ? comparison : -comparison; }); // Rebuild table rows.forEach(row => tbody.appendChild(row)); updateShowingCount(); } // Update sort direction icons function updateSortIcons(activeColumn, direction) { const headers = document.querySelectorAll('#ticketsTable thead th'); headers.forEach((header, index) => { const icon = header.querySelector('i.fa-sort, i.fa-sort-up, i.fa-sort-down'); if (icon) { if (index === activeColumn) { icon.className = direction === 'asc' ? 'fas fa-sort-up' : 'fas fa-sort-down'; } else { icon.className = 'fas fa-sort'; } } }); } // Search functionality function initializeSearch() { const searchInput = document.getElementById('searchInput'); if (!searchInput) return; searchInput.addEventListener('input', function() { const searchTerm = this.value.toLowerCase(); filterTable(); }); } // Filter functionality function initializeFilters() { const countryFilter = document.getElementById('countryFilter'); if (!countryFilter) return; countryFilter.addEventListener('change', function() { filterTable(); }); } // Combined filter and search function function filterTable() { if (!window.originalTableData) return; const searchTerm = document.getElementById('searchInput')?.value.toLowerCase() || ''; const countryFilter = document.getElementById('countryFilter')?.value || ''; const tbody = document.querySelector('#ticketsTable tbody'); // Clear current table tbody.innerHTML = ''; let visibleCount = 0; window.originalTableData.forEach(row => { const rowText = row.textContent.toLowerCase(); const countryCell = row.cells[5].textContent.trim(); const matchesSearch = searchTerm === '' || rowText.includes(searchTerm); const matchesCountry = countryFilter === '' || countryCell === countryFilter; if (matchesSearch && matchesCountry) { tbody.appendChild(row.cloneNode(true)); visibleCount++; } }); // Update showing count document.getElementById('showingCount').textContent = visibleCount; // Show empty state if no results if (visibleCount === 0) { showEmptySearchResults(); } } // Show empty search results function showEmptySearchResults() { const tbody = document.querySelector('#ticketsTable tbody'); const emptyRow = document.createElement('tr'); emptyRow.innerHTML = `
No tickets match your search criteria