/* ===================================================================== postfach.js — Logik für postfach.html ===================================================================== */ const API_BASE_URL = "https://dogfather-universe-postfach.dogfather1608.workers.dev"; const { getSession, sessionHasPermission, apiCall, doLogin, doLogout } = window.AdminAuth; const STATUS_OPTIONS = ["neu", "in_bearbeitung", "beantwortet"]; function $(id) { return document.getElementById(id); } function showApp(session) { $("gate-container").style.display = "none"; $("session-bar").style.display = "block"; $("inbox-section").style.display = "block"; $("role-badge").textContent = session.isOwner ? "Eingeloggt als: Dogi (Owner)" : `Eingeloggt als: ${session.name} (${session.roleName})`; const canSeeTeam = session.isOwner || sessionHasPermission(session, "TEAM_VIEW"); $("team-link").style.display = canSeeTeam ? "inline-flex" : "none"; if (session.isOwner) { $("live-toggle-section").style.display = "block"; loadLiveStatus(); } loadInbox(); } function showGate() { $("gate-container").style.display = "block"; $("session-bar").style.display = "none"; $("live-toggle-section").style.display = "none"; $("inbox-section").style.display = "none"; } /* ---------------- Bewerbungen ---------------- */ async function loadInbox() { const session = getSession(); const includeArchived = $("show-archived").checked; const res = await apiCall(API_BASE_URL, "/admin/applications/list", { includeArchived }); if (!res.ok) { if (!getSession()) showGate(); return; } renderInbox(res.applications, session); } function renderInbox(applications, session) { const list = $("inbox-list"); if (!applications.length) { list.innerHTML = `
Keine Bewerbungen in dieser Ansicht.
`; return; } list.innerHTML = applications .map((a) => { const fields = a.data ? Object.entries(a.data).map(([k, v]) => `${k}: ${v}
`).join("") : `Keine Berechtigung, den vollen Inhalt zu öffnen.
`; const date = new Date(a.createdAt).toLocaleString("de-DE"); const actions = []; if (sessionHasPermission(session, "APPLICATIONS_TAKE_OVER")) { actions.push(``); } if (sessionHasPermission(session, "APPLICATIONS_MARK_ANSWERED")) { actions.push(``); } if (sessionHasPermission(session, "APPLICATIONS_ARCHIVE") && !a.archived) { actions.push(``); } if (sessionHasPermission(session, "APPLICATIONS_RESTORE") && a.archived) { actions.push(``); } if (sessionHasPermission(session, "APPLICATIONS_DELETE")) { actions.push(``); } if (sessionHasPermission(session, "NOTES_READ")) { actions.push(``); } const statusSelect = sessionHasPermission(session, "APPLICATIONS_CHANGE_STATUS") ? `` : `${a.status}`; return `${date}
Lädt…
Konnte Notizen nicht laden.
`; return; } box.innerHTML = res.notes .map((n) => `Noch keine Notizen.
`; } /* ---------------- Live-Status ---------------- */ function renderLiveStatus(live) { $("admin-live-dot").classList.toggle("is-live", !!live); $("admin-live-text").textContent = live ? "Live jetzt!" : "Aktuell offline"; } async function loadLiveStatus() { try { const res = await fetch(`${API_BASE_URL}/live-status`); const data = await res.json(); if (res.ok && data.ok) renderLiveStatus(data.live); } catch { /* keine Blockade */ } } async function setLiveStatus(live) { const res = await apiCall(API_BASE_URL, "/live-status/set", { live }); if (res.ok) renderLiveStatus(res.live); } /* ---------------- Login / Logout ---------------- */ document.addEventListener("DOMContentLoaded", () => { const existing = getSession(); if (existing) showApp(existing); else showGate(); $("code-gate").addEventListener("submit", async (e) => { e.preventDefault(); const status = $("gate-status"); status.className = "form-status show"; status.textContent = "Prüfe Code …"; const res = await doLogin(API_BASE_URL, $("code-input").value); if (res.ok) { status.className = "form-status"; showApp(getSession()); } else { status.textContent = res.error || "Falscher Code."; status.className = "form-status show error"; } }); $("logout-btn").addEventListener("click", async () => { await doLogout(API_BASE_URL); showGate(); }); $("refresh-btn").addEventListener("click", loadInbox); $("show-archived").addEventListener("change", loadInbox); $("go-live-btn").addEventListener("click", () => setLiveStatus(true)); $("go-offline-btn").addEventListener("click", () => setLiveStatus(false)); });