/** * Admin Users Management Module * Handles user CRUD operations, email broadcasting, and user administration */ // User management state let allUsersData = []; // 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) { displayUsers(data.users); } 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) { usersTableContainer.innerHTML = '
'; return; } const tableHtml = `| Name | Role | Created | Actions | |
|---|---|---|---|---|
| ${window.adminCore.escapeHtml(user.email || user.Email || 'N/A')} | ${window.adminCore.escapeHtml(user.name || user.Name || 'N/A')} | ${userType.charAt(0).toUpperCase() + userType.slice(1)} ${expirationInfo} | ${formattedDate} |
|