document.addEventListener('DOMContentLoaded', function() { // Only initialize filters if we're on the dashboard if (document.querySelector('table')) { initSearch(); initStatusFilter(); } // Keep theme toggle for all pages initThemeToggle(); // Load saved theme preference const savedTheme = localStorage.getItem('theme') || 'light'; document.documentElement.setAttribute('data-theme', savedTheme); // Add sorting functionality const tableHeaders = document.querySelectorAll('th'); tableHeaders.forEach(header => { header.addEventListener('click', () => { const table = header.closest('table'); const index = Array.from(header.parentElement.children).indexOf(header); sortTable(table, index); }); }); // Add settings modal functionality const settingsIcon = document.querySelector('.settings-icon'); if (settingsIcon) { settingsIcon.addEventListener('click', function(e) { e.preventDefault(); createSettingsModal(); }); } }); function sortTable(table, column) { // Remove existing sort indicators from all headers const headers = table.querySelectorAll('th'); headers.forEach(header => { header.classList.remove('sort-asc', 'sort-desc'); }); const rows = Array.from(table.querySelectorAll('tbody tr')); // Determine current sort direction const currentDirection = table.dataset.sortColumn === column ? (table.dataset.sortDirection === 'asc' ? 'desc' : 'asc') : 'asc'; // Store current sorting column and direction table.dataset.sortColumn = column; table.dataset.sortDirection = currentDirection; rows.sort((a, b) => { const aValue = a.children[column].textContent.trim(); const bValue = b.children[column].textContent.trim(); // Try numeric sorting first, fallback to string comparison const numA = parseFloat(aValue); const numB = parseFloat(bValue); if (!isNaN(numA) && !isNaN(numB)) { return currentDirection === 'asc' ? numA - numB : numB - numA; } // String comparison return currentDirection === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue); }); // Add sort indicator to the current header const currentHeader = headers[column]; currentHeader.classList.add(currentDirection === 'asc' ? 'sort-asc' : 'sort-desc'); // Reorder rows in the tbody const tbody = table.querySelector('tbody'); rows.forEach(row => tbody.appendChild(row)); } // Add this to the DOMContentLoaded event listener to persist sorting on page load document.addEventListener('DOMContentLoaded', function() { const table = document.querySelector('table'); if (table) { const savedSortColumn = localStorage.getItem('sortColumn'); const savedSortDirection = localStorage.getItem('sortDirection'); if (savedSortColumn !== null && savedSortDirection !== null) { const headers = table.querySelectorAll('th'); const columnIndex = Array.from(headers).findIndex(header => header.textContent.toLowerCase().replace(' ', '_') === savedSortColumn ); if (columnIndex !== -1) { table.dataset.sortColumn = columnIndex; table.dataset.sortDirection = savedSortDirection; const header = headers[columnIndex]; header.classList.add(savedSortDirection === 'asc' ? 'sort-asc' : 'sort-desc'); } } } }); // Modify the existing event listeners for table headers document.addEventListener('DOMContentLoaded', function() { const tableHeaders = document.querySelectorAll('th'); tableHeaders.forEach((header, index) => { header.addEventListener('click', () => { const table = header.closest('table'); sortTable(table, index); // Save sorting preferences const columnName = header.textContent.toLowerCase().replace(' ', '_'); localStorage.setItem('sortColumn', columnName); localStorage.setItem('sortDirection', table.dataset.sortDirection); }); }); }); function createSettingsModal() { // Create modal backdrop const backdrop = document.createElement('div'); backdrop.className = 'settings-modal-backdrop'; backdrop.innerHTML = `