@page "/"
@inject IJSRuntime JS
@inject NavigationManager Nav

<PageTitle>FilterCair Dashboard</PageTitle>

<div class="container py-5">
    <div class="text-center mb-5">
        <h2 class="fw-bold text-primary mb-2">
            <i class="bi bi-wind me-2"></i> Willkommen bei FilterCair
        </h2>
        <p class="text-secondary fs-5">FÜÜS - Filter Überprüf- und Überwachungssoftware</p>

        <span class="badge rounded-pill px-3 py-2 mt-3 fs-6"
              style="background-color:@(isOnline ? "#198754" : "#dc3545")">
            @(isOnline ? "🟢 Online" : "🔴 Offline")
        </span>
    </div>

    <!--  Dashboard-Kacheln -->
    <div class="row g-4 justify-content-center">
        <div class="col-12 col-sm-6 col-lg-4">
            <div class="card dashboard-tile h-100" @onclick="@(() => Nav.NavigateTo("/qrscanner"))">
                <div class="card-body d-flex flex-column justify-content-center align-items-center text-center">
                    <i class="bi bi-qr-code-scan display-5 mb-3 text-primary"></i>
                    <h5 class="fw-semibold mb-2">QR-Code scannen</h5>
                    <p class="text-muted small mb-0">Starte den Scan und erfasse Filterdaten direkt vor Ort.</p>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-4">
            <div class="card dashboard-tile h-100" @onclick="@(() => Nav.NavigateTo("/filterform"))">
                <div class="card-body d-flex flex-column justify-content-center align-items-center text-center">
                    <i class="bi bi-funnel display-5 mb-3 text-primary"></i>
                    <h5 class="fw-semibold mb-2">Filter erfassen</h5>
                    <p class="text-muted small mb-0">Erfasse technische Daten oder ergänze Wartungsnotizen.</p>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-4">
            <div class="card dashboard-tile h-100" @onclick="@ShowOfflineData">
                <div class="card-body d-flex flex-column justify-content-center align-items-center text-center">
                    <i class="bi bi-database-down display-5 mb-3 text-primary"></i>
                    <h5 class="fw-semibold mb-2">Offline-Daten</h5>
                    <p class="text-muted small mb-0">Zeige gespeicherte Daten, wenn du ohne Verbindung arbeitest.</p>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-4">
            <div class="card dashboard-tile h-100" @onclick="@(() => Nav.NavigateTo("/tasks"))">
                <div class="card-body d-flex flex-column justify-content-center align-items-center text-center">
                    <i class="bi bi-list-check display-5 mb-3 text-primary"></i>
                    <h5 class="fw-semibold mb-2">Meine Aufgaben</h5>
                    <p class="text-muted small mb-0">Übersicht aller offenen Wartungen und Filterprüfungen.</p>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-4">
            <div class="card dashboard-tile h-100" @onclick="@(() => Nav.NavigateTo("/customers"))">
                <div class="card-body d-flex flex-column justify-content-center align-items-center text-center">
                    <i class="bi bi-buildings display-5 mb-3 text-primary"></i>
                    <h5 class="fw-semibold mb-2">Kundenübersicht</h5>
                    <p class="text-muted small mb-0">Wähle den Kundenstandort aus, um Filterdaten zu erfassen.</p>
                </div>
            </div>
        </div>




    </div>

    <!-- 📍 Standortanzeige -->
    <div class="text-center mt-5 text-muted small">
        📍 Aktueller Standort: @currentLocation
    </div>
</div>

@code {
    private bool isOnline = true;
    private string currentLocation = "unbekannt";

    protected override async Task OnInitializedAsync()
    {
        await JS.InvokeVoidAsync("FilterCair.registerNetworkHandler", DotNetObjectReference.Create(this));
        isOnline = await JS.InvokeAsync<bool>("FilterCair.isOnline");
        currentLocation = await JS.InvokeAsync<string>("FilterCair.getLocation");
    }

    [JSInvokable]
    public void UpdateNetworkStatus(bool online)
    {
        isOnline = online;
        StateHasChanged();
    }

    private void ShowOfflineData()
    {
        Console.WriteLine("Offline-Daten anzeigen");
    }
}
