@page "/filterform"
@inject IJSRuntime JS
@inject NavigationManager NavManager
@using FilterCair.Shared.Models
@using Microsoft.AspNetCore.Components.Forms
@inject FilterCair.Client.Services.API.FilterService FilterService

<PageTitle>Filterdaten erfassen</PageTitle>

<div class="container py-4 fade-in">
    <div class="card shadow-sm border-0 rounded-4">
        <div class="card-body p-4">

            <h4 class="mb-4 text-center text-primary">
                <i class="bi bi-funnel me-2"></i> Filterdaten erfassen
            </h4>

            <EditForm Model="@filter" OnValidSubmit="SaveForm">
                <DataAnnotationsValidator />
                <ValidationSummary />

                <div class="row g-3">

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Bezeichnung</label>
                        <InputText class="form-control form-control-lg" @bind-Value="filter.Bezeichnung" placeholder="z. B. Filter Nordhalle" />
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Typ</label>
                        <InputText class="form-control form-control-lg" @bind-Value="filter.Typ" placeholder="z. B. Aktivkohle" />
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Seriennummer</label>
                        <InputText class="form-control form-control-lg" @bind-Value="filter.Seriennummer" placeholder="z. B. SN-2024-0012" />
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Zustand</label>
                        <InputSelect class="form-select form-select-lg" @bind-Value="filter.Zustand">
                            <option value="">– bitte wählen –</option>
                            <option>In Ordnung</option>
                            <option>Verschmutzt</option>
                            <option>Defekt</option>
                        </InputSelect>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Einbaudatum</label>
                        <InputDate class="form-control form-control-lg" @bind-Value="filter.Einbaudatum" />
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Letzte Wartung</label>
                        <InputDate class="form-control form-control-lg" @bind-Value="filter.LetzteWartung" />
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">QR-Code</label>
                        <InputText class="form-control form-control-lg" @bind-Value="filter.QRCode" placeholder="z. B. FC-Q12345" />
                    </div>

                </div>

                <div class="d-grid mt-4">
                    <button type="submit" class="btn btn-primary btn-lg rounded-pill shadow-sm">
                        <i class="bi bi-save me-2"></i> Speichern
                    </button>
                </div>
            </EditForm>

            @if (showToast)
            {
                <div class="toast-container position-fixed bottom-0 end-0 p-3">
                    <div class="toast align-items-center text-bg-success border-0 show fade-in-toast">
                        <div class="d-flex">
                            <div class="toast-body">
                                ✅ Filterdaten erfolgreich gespeichert!
                            </div>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>

@code {
    private FilterModel filter = new();
    private bool showToast;

    private async Task SaveForm()
    {
        bool success = await FilterService.SaveFilterAsync(filter);
        if (success)
        {
            await JS.InvokeVoidAsync("showToast", "✅ Offline gespeichert – wird synchronisiert!", "success");
            NavManager.NavigateTo($"/stations/{filter.CustomerId}/filters/{filter.StationId}");
        }
        else
        {
            await JS.InvokeVoidAsync("showToast", "Fehler", "error");
        }
    }


    protected override void OnInitialized()
    {
        var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
        var query = System.Web.HttpUtility.ParseQueryString(uri.Query);
        var stationIdStr = query.Get("stationId");
        if (int.TryParse(stationIdStr, out int stationId))
        {
            filter.StationId = stationId;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
        var query = System.Web.HttpUtility.ParseQueryString(uri.Query);

        if (int.TryParse(query.Get("customerId"), out int customerId))
            filter.CustomerId = customerId;

        if (int.TryParse(query.Get("stationId"), out int stationId))
            filter.StationId = stationId;

        if (int.TryParse(query.Get("filterId"), out int filterId))
        {
            var existing = await FilterService.GetFilterByIdAsync(filterId);
            if (existing != null)
                filter = existing;
        }
    }



}
