@page "/stations/{customerId:int}/filters/{stationId:int}"
@using FilterCair.Shared.Models
@inject FilterCair.Client.Services.API.FilterService FilterService
@inject NavigationManager Nav

<PageTitle>Filterübersicht</PageTitle>

<div class="container py-4 fade-in">
    <h4 class="text-primary text-center mb-4">
        <i class="bi bi-funnel me-2"></i> Filterübersicht
    </h4>

    @if (isLoading)
    {
        <div class="text-center text-muted py-5">
            <div class="spinner-border text-primary" role="status"></div>
            <p class="mt-3">Lade Filter...</p>
        </div>
    }
    else if (filters.Count == 0)
    {
        <div class="alert alert-info text-center">
            Keine Filter für diese Station vorhanden.
        </div>
    }
    else
    {
        var editedFilters = filters.Where(f => f.LetzteWartung.HasValue).OrderByDescending(f => f.LetzteWartung).ToList();
        var pendingFilters = filters.Except(editedFilters).ToList();

        <!-- Bearbeitete Filter -->
        @if (editedFilters.Any())
        {
            <h5 class="text-success mt-4">
                <i class="bi bi-check-circle me-1"></i> Bearbeitet (@editedFilters.Count)
            </h5>
            <div class="row g-3">
                @foreach (var f in editedFilters)
                {
                    <div class="col-12 col-md-6 col-lg-4">
                        <div class="card border-0 shadow-sm filter-card h-100" @onclick="() => OpenFilterForm(f)">
                            <div class="card-body">
                                <h5 class="fw-semibold text-success">
                                    <i class="bi bi-funnel me-1"></i> @f.Bezeichnung
                                </h5>
                                <p class="text-muted small"><strong>Zustand:</strong> @f.Zustand</p>
                                <p class="text-muted small">
                                    <strong>Letzte Wartung:</strong>
                                    @(f.LetzteWartung?.ToString("dd.MM.yyyy") ?? "–")
                                </p>
                            </div>
                            <div class="card-footer bg-light text-end">
                                <button class="btn btn-outline-success btn-sm rounded-pill">
                                    <i class="bi bi-pencil"></i> Bearbeiten
                                </button>
                            </div>
                        </div>
                    </div>
                }
            </div>
        }

        <!-- Noch nicht bearbeitet -->
        @if (pendingFilters.Any())
        {
            <h5 class="text-danger mt-4">
                <i class="bi bi-exclamation-triangle me-1"></i> Offen (@pendingFilters.Count)
            </h5>
            <div class="row g-3">
                @foreach (var f in pendingFilters)
                {
                    <div class="col-12 col-md-6 col-lg-4">
                        <div class="card border-0 shadow-sm filter-card h-100" @onclick="() => OpenFilterForm(f)">
                            <div class="card-body">
                                <h5 class="fw-semibold text-danger">
                                    <i class="bi bi-funnel me-1"></i> @f.Bezeichnung
                                </h5>
                                <p class="text-muted small"><strong>Zustand:</strong> @f.Zustand</p>
                                <p class="text-muted small text-danger">
                                    <strong>Noch nicht bearbeitet</strong>
                                </p>
                            </div>
                            <div class="card-footer bg-light text-end">
                                <button class="btn btn-outline-danger btn-sm rounded-pill">
                                    <i class="bi bi-pencil"></i> Bearbeiten
                                </button>
                            </div>
                        </div>
                    </div>
                }
            </div>
        }
    }
</div>

@code {
    [Parameter] public int customerId { get; set; }
    [Parameter] public int stationId { get; set; }

    private List<FilterModel> filters = new();
    private bool isLoading = true;

    protected override async Task OnInitializedAsync()
    {
        await LoadFilters();
    }

    private async Task LoadFilters()
    {
        isLoading = true;
        filters = await FilterService.GetFiltersByStationAsync(stationId);
        isLoading = false;
    }

    private void OpenFilterForm(FilterModel filter)
    {
        Nav.NavigateTo($"/filterform?filterId={filter.Id}&stationId={filter.StationId}&customerId={customerId}");
    }
}
