// Veritas Suite Web Portal App Logic const API_BASE = ""; document.addEventListener("DOMContentLoaded", () => { checkServerHealth(); restoreSession(); fetchDownloads(); // Check if we are on admin page if (window.location.pathname === "/admin" || window.location.pathname.endsWith("admin.html")) { checkAdminPageSession(); } }); // Check Server Connection async function checkServerHealth() { const indicator = document.querySelector(".status-indicator"); const statusText = document.getElementById("status-text"); try { const response = await fetch(`${API_BASE}/health`); if (response.ok) { if (indicator) indicator.classList.add("online"); if (statusText) statusText.textContent = "VPS Server: Online (200 OK)"; } else { if (statusText) statusText.textContent = "VPS Server: Fehler"; } } catch (e) { if (statusText) statusText.textContent = "VPS Server: Offline"; } } // Filter Sparten Tools function filterSparten(category) { document.querySelectorAll(".filter-btn").forEach(btn => btn.classList.remove("active")); const activeBtn = Array.from(document.querySelectorAll(".filter-btn")).find(btn => btn.getAttribute("onclick") && btn.getAttribute("onclick").includes(`'${category}'`) ); if (activeBtn) activeBtn.classList.add("active"); const cards = document.querySelectorAll(".tool-card"); cards.forEach(card => { if (category === "all" || card.getAttribute("data-sparte") === category) { card.style.display = "flex"; } else { card.style.display = "none"; } }); } // Restore Session from LocalStorage function restoreSession() { const sessionStr = localStorage.getItem("veritas_session"); if (sessionStr) { try { const session = JSON.parse(sessionStr); if (session && session.user_id) { showLoggedInState(session); } else { showLoggedOutState(); } } catch (e) { localStorage.removeItem("veritas_session"); showLoggedOutState(); } } else { showLoggedOutState(); } } function showLoggedInState(session) { const loggedOutDiv = document.getElementById("dash-logged-out"); const loggedInDiv = document.getElementById("dash-logged-in"); const userEmailSpan = document.getElementById("dash-user-email"); const userStatusBar = document.getElementById("user-status-bar"); const authForms = document.getElementById("auth-forms-container"); const navAdminLink = document.getElementById("nav-admin-link"); const btnAdminPanel = document.getElementById("btn-admin-panel"); if (loggedOutDiv) loggedOutDiv.style.display = "none"; if (loggedInDiv) loggedInDiv.style.display = "block"; if (userEmailSpan) userEmailSpan.textContent = session.email; if (userStatusBar) userStatusBar.style.display = "inline-flex"; if (authForms) authForms.style.display = "none"; // Show Admin Buttons ONLY if logged-in user is an Admin if (session.is_admin) { if (navAdminLink) navAdminLink.style.display = "inline-flex"; if (btnAdminPanel) btnAdminPanel.style.display = "inline-block"; } else { if (navAdminLink) navAdminLink.style.display = "none"; if (btnAdminPanel) btnAdminPanel.style.display = "none"; } refreshDashboard(); } function showLoggedOutState() { const loggedOutDiv = document.getElementById("dash-logged-out"); const loggedInDiv = document.getElementById("dash-logged-in"); const userStatusBar = document.getElementById("user-status-bar"); const authForms = document.getElementById("auth-forms-container"); const navAdminLink = document.getElementById("nav-admin-link"); const btnAdminPanel = document.getElementById("btn-admin-panel"); if (loggedOutDiv) loggedOutDiv.style.display = "block"; if (loggedInDiv) loggedInDiv.style.display = "none"; if (userStatusBar) userStatusBar.style.display = "none"; if (authForms) authForms.style.display = "grid"; if (navAdminLink) navAdminLink.style.display = "none"; if (btnAdminPanel) btnAdminPanel.style.display = "none"; } function logout() { localStorage.removeItem("veritas_session"); showLoggedOutState(); showAlert("Erfolgreich abgemeldet.", "success"); if (window.location.pathname === "/admin" || window.location.pathname.endsWith("admin.html")) { window.location.href = "/portal"; } } // Handle Customer Registration async function handleRegister(event) { event.preventDefault(); const email = document.getElementById("reg-email").value; const password = document.getElementById("reg-password").value; try { const response = await fetch(`${API_BASE}/api/v1/auth/register`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }) }); const data = await response.json(); if (data.success) { showAlert(data.message, "success"); const session = { user_id: data.user_id, email: data.email, token: data.auth_token, is_admin: data.is_admin }; localStorage.setItem("veritas_session", JSON.stringify(session)); showLoggedInState(session); } else { showAlert(data.message, "error"); } } catch (e) { showAlert("Netzwerkfehler bei der Registrierung.", "error"); } } // Handle Customer & Admin Login in Portal async function handleLogin(event) { event.preventDefault(); const email = document.getElementById("login-email").value; const password = document.getElementById("login-password").value; try { const response = await fetch(`${API_BASE}/api/v1/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }) }); const data = await response.json(); if (data.success) { showAlert(data.message, "success"); const session = { user_id: data.user_id, email: data.email, token: data.auth_token, is_admin: data.is_admin }; localStorage.setItem("veritas_session", JSON.stringify(session)); showLoggedInState(session); if (data.is_admin) { showAlert("👑 Willkommen Admin! Du hast Zugriff auf die Admin-Zentrale.", "success"); } } else { showAlert(data.message, "error"); } } catch (e) { showAlert("Netzwerkfehler beim Anmelden.", "error"); } } // Check Admin Page Session (Requires Admin Login in Customer Portal) function checkAdminPageSession() { const sessionStr = localStorage.getItem("veritas_session"); const loggedOutState = document.getElementById("admin-logged-out-state"); const loggedInState = document.getElementById("admin-logged-in-state"); const userEmailDisplay = document.getElementById("admin-user-email-display"); if (sessionStr) { try { const session = JSON.parse(sessionStr); if (session && session.is_admin) { if (loggedOutState) loggedOutState.style.display = "none"; if (loggedInState) loggedInState.style.display = "block"; if (userEmailDisplay) userEmailDisplay.textContent = session.email; fetchAdminStats(); return; } } catch (e) {} } if (loggedOutState) loggedOutState.style.display = "block"; if (loggedInState) loggedInState.style.display = "none"; } function logoutAdmin() { logout(); } // Refresh Customer Dashboard Data async function refreshDashboard() { const sessionStr = localStorage.getItem("veritas_session"); if (!sessionStr) return; const session = JSON.parse(sessionStr); const container = document.getElementById("license-list"); if (!container) return; container.innerHTML = "
Lizenzen werden geladen...
"; try { const response = await fetch(`${API_BASE}/api/v1/account/dashboard?user_id=${session.user_id}`); const data = await response.json(); if (data.success && data.licenses && data.licenses.length > 0) { container.innerHTML = ""; data.licenses.forEach(lic => { const percent = Math.min(100, (lic.activated_devices / lic.max_devices) * 100); const cardHtml = `${lic.license_key}
Keine aktiven Lizenzen für dieses Konto gefunden.
"; } } catch (e) { container.innerHTML = "Fehler beim Laden des Dashboards.
"; } } // Fetch Software Downloads async function fetchDownloads() { const container = document.getElementById("downloads-container"); if (!container) return; try { const response = await fetch(`${API_BASE}/api/v1/downloads/list`); const items = await response.json(); container.innerHTML = ""; items.forEach(item => { const card = `${item.description}
Fehler beim Laden der Download-Liste.
"; } } // Fetch Admin Stats (Sends Authorization Bearer token) async function fetchAdminStats() { const sessionStr = localStorage.getItem("veritas_session"); if (!sessionStr) return; const session = JSON.parse(sessionStr); try { const response = await fetch(`${API_BASE}/api/v1/admin/stats`, { headers: { "Authorization": `Bearer ${session.token}` } }); const data = await response.json(); if (data.success) { const usersEl = document.getElementById("admin-stat-users"); const licsEl = document.getElementById("admin-stat-licenses"); const actsEl = document.getElementById("admin-stat-activations"); if (usersEl) usersEl.textContent = data.total_users; if (licsEl) licsEl.textContent = data.total_licenses; if (actsEl) actsEl.textContent = data.total_activations; const tbody = document.getElementById("admin-licenses-table-body"); if (tbody) { tbody.innerHTML = ""; data.licenses.forEach(lic => { const tr = `${lic.license_key}