// VaultSentinel - Account Management Application // Main JavaScript functionality class AccountManager { constructor() { // Check if user is logged in const currentUser = this.loadFromStorage('currentUser'); if (!currentUser) { window.location.href = '../pages/login.html'; return; } this.currentUser = currentUser; this.accounts = []; this.applications = []; this.apiBase = '/api'; this.currentPage = 'dashboard'; this.initPromise = this.init(); } getUserId() { const u = this.currentUser; const detected = u?.UserId ?? u?.userId ?? u?.id ?? u?.ID ?? u?.userid ?? u?.user_id ?? u?.user?.UserId ?? u?.user?.userId; // Fallback: if only username/role exist (no id), use default admin id = 1 return detected ?? 1; } async init() { await this.fetchApplications(); await this.fetchAccounts(); this.setupEventListeners(); this.loadModals(); // Load modals từ file riêng // Render dashboard content (only for index.html) const mainContent = document.getElementById('mainContent'); if (mainContent && window.location.pathname.endsWith('index.html')) { mainContent.innerHTML = this.renderDashboard(); } } async fetchApplications() { const res = await fetch(`${this.apiBase}/applications`); const data = await res.json(); if (data.success) { this.applications = data.data; } else { console.error('Load applications failed:', data.message); } } async fetchAccounts() { const userId = this.getUserId(); if (!userId) return; const res = await fetch(`${this.apiBase}/accounts/user/${userId}`); const data = await res.json(); if (data.success) { this.accounts = data.data; } else { console.error('Load accounts failed:', data.message); } } async loadModals() { try { const response = await fetch('../modals.html'); const modalsHTML = await response.text(); const modalsContainer = document.getElementById('modalsContainer'); if (modalsContainer) { modalsContainer.innerHTML = modalsHTML; // Re-attach event listeners after modals are loaded setTimeout(() => { this.setupAccountRowListeners(); this.setupFormListeners(); }, 50); } } catch (error) { console.error('Lỗi load modals:', error); } } setupEventListeners() { // Modal close buttons document.querySelectorAll('[data-close-modal]').forEach(btn => { btn.addEventListener('click', () => this.closeModals()); }); // Close with Escape key document.addEventListener('keydown', (e) => { if (e.key === 'Escape') { this.closeModals(); } }); // Form submissions const accountForm = document.getElementById('accountForm'); if (accountForm) { accountForm.addEventListener('submit', (e) => this.handleAccountSubmit(e)); } const appForm = document.getElementById('appForm'); if (appForm) { appForm.addEventListener('submit', (e) => this.handleAppSubmit(e)); } // Logout button const logoutBtn = document.getElementById('logoutBtn'); if (logoutBtn) { logoutBtn.addEventListener('click', () => this.handleLogout()); } // Update account display this.updateAccountDisplay(); // Account table row clicks this.setupAccountRowListeners(); } setupFormListeners() { const accountForm = document.getElementById('accountForm'); if (accountForm) { accountForm.addEventListener('submit', (e) => this.handleAccountSubmit(e)); } const appForm = document.getElementById('appForm'); if (appForm) { appForm.addEventListener('submit', (e) => this.handleAppSubmit(e)); } // Close when clicking backdrop outside modal content document.querySelectorAll('.modal-backdrop').forEach(backdrop => { backdrop.addEventListener('click', (evt) => { if (evt.target === backdrop) { this.closeModals(); } }); }); } updateAccountDisplay() { // Use the logged-in user from constructor const usernameEl = document.getElementById('accountUsername'); const roleEl = document.getElementById('accountRole'); if (usernameEl) usernameEl.textContent = this.currentUser?.username || this.currentUser?.Username || 'User'; if (roleEl) roleEl.textContent = this.currentUser?.role || this.currentUser?.Role || 'Administrator'; } handleLogout() { if (confirm('Are you sure you want to logout?')) { this.saveToStorage('currentUser', null); localStorage.clear(); window.location.href = '../pages/login.html'; } } renderDashboard() { return `
Account & Service Management
${username}
${service} • ${owner}
No accounts yet. Create one
Administrative Access Control
| Owner | Username | Service | Actions |
|---|---|---|---|
| ${acc.Email || '-'} | ${acc.AccountUsername || '-'} | ${acc.AppName || '-'} |
No accounts yet. Create one to get started.
Manage and monitor active infrastructure services.
Active
${this.applications.filter(a => a.status === 'online').length}
Total
${this.applications.length}
Health
99.9%
| Name | Type | Description | URL | Status | Actions |
|---|---|---|---|---|---|
|
${app.Icon || 'apps'}
${app.Name}
|
${app.Type} | ${app.Description || '-'} | ${(app.Url || app.url) ? `${app.Url || app.url}` : '-'} |
${(app.Status || app.status) === 'online' ? 'Online' : 'Offline'}
|
|