/** * Admin Users Management Module * Handles user CRUD operations, email broadcasting, and user administration */ // User management state let allUsersData = []; let filteredUsersData = []; let currentSearchTerm = ''; let currentFilterType = 'all'; let eventListenersSetup = false; let usersInitialized = false; // User Management Functions async function loadUsers() { const loadingEl = document.getElementById('users-loading'); const emptyEl = document.getElementById('users-empty'); const tableBody = document.getElementById('users-table-body'); if (loadingEl) loadingEl.style.display = 'block'; if (emptyEl) emptyEl.style.display = 'none'; if (tableBody) tableBody.innerHTML = ''; try { const response = await fetch('/api/users'); const data = await response.json(); if (loadingEl) loadingEl.style.display = 'none'; if (data.success && data.users) { allUsersData = data.users; filteredUsersData = [...allUsersData]; displayUsers(filteredUsersData); updateSearchResults(); // Make sure event listeners are set up after loading users if (!eventListenersSetup) { setupUserEventListeners(); } usersInitialized = true; } else { throw new Error(data.error || 'Failed to load users'); } } catch (error) { console.error('Error loading users:', error); if (loadingEl) loadingEl.style.display = 'none'; if (emptyEl) { emptyEl.textContent = 'Failed to load users'; emptyEl.style.display = 'block'; } window.adminCore.showStatus('Failed to load users', 'error'); } } function displayUsers(users) { const container = document.querySelector('.users-list'); if (!container) return; // Find or create the users table container, preserving the header let usersTableContainer = container.querySelector('.users-table-container'); if (!usersTableContainer) { // If container doesn't exist, create it after the header const header = container.querySelector('.users-list-header'); usersTableContainer = document.createElement('div'); usersTableContainer.className = 'users-table-container'; if (header && header.nextSibling) { container.insertBefore(usersTableContainer, header.nextSibling); } else if (header) { container.appendChild(usersTableContainer); } else { container.appendChild(usersTableContainer); } } if (!users || users.length === 0) { const hasFilter = currentSearchTerm || currentFilterType !== 'all'; const emptyMessage = hasFilter ? 'No users match your search criteria.' : 'No users found.'; usersTableContainer.innerHTML = `