@model Watcher.ViewModels.DashboardViewModel
@{
    ViewData["Title"] = "Dashboard";
}

<h1 class="mb-4">
    <i class="bi bi-speedometer2 me-2"></i>Dashboard
</h1>

<div id="dashboard-stats">
    @await Html.PartialAsync("_DashboardStats", Model)
</div>

<div class="row g-4 mt-4">

    <div class="col-12">
        <div class="card p-3">
            <h2 class="h5">
                <i class="bi bi-person-circle me-2"></i>Systeminfo
            </h2>
            <p>
                <i class="bi bi-person-badge me-1"></i>
                Benutzer: <strong>@User.FindFirst("preferred_username")?.Value</strong>
            </p>
            <p>
                <i class="bi bi-clock me-1"></i>
                Letzter Login: <strong>@Model.LastLogin.ToString("g")</strong>
            </p>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        async function loadDashboardStats() {
            try {
                const response = await fetch('/Home/DashboardStats');
                if (response.ok) {
                    console.log("reload")
                    const html = await response.text();
                    document.getElementById('dashboard-stats').innerHTML = html;
                } else {
                    console.error('Fehler beim Nachladen der Dashboard-Statistiken');
                }
            } catch (err) {
                console.error('Netzwerkfehler beim Nachladen der Dashboard-Statistiken:', err);
            }
        }

        // Initial laden und dann alle 30 Sekunden
        loadDashboardStats();
        setInterval(loadDashboardStats, 30000);
    </script>
}
