@page "/customers"
@using FilterCair.Shared.Models
@inject FilterCair.Client.Services.API.CustomerService CustomerService

@inject IJSRuntime JS
@inject NavigationManager Nav
@inject FilterCair.Client.Services.AppState State

<h3>Kundenübersicht</h3>

<div class="container py-4 fade-in">
    <h4 class="mb-4 text-center text-primary">
        <i class="bi bi-buildings me-2"></i> Kunden & Fabriken
    </h4>

    <!-- 🔍 Suchfeld -->
    <div class="mb-4 text-center">
        <input type="text" class="form-control w-50 mx-auto shadow-sm"
               placeholder="🔍 Kunde oder Standort suchen..."
               @bind="searchText"
               @bind:event="oninput" />
    </div>

    <!-- 🏭 Kundenliste -->
    <div class="row g-4 justify-content-center">
        @if (customers == null)
        {
            <p class="text-center text-muted">Lade Kunden...</p>
        }
        else
        {
            @foreach (var c in FilteredCustomers)
            {
                <div class="col-12 col-sm-6 col-lg-4">
                    <div class="card customer-card border-0 shadow-sm h-100"
                         @onclick="@(() => SelectCustomer(c))">
                        <div class="card-body">
                            <h5 class="fw-semibold mb-2 text-primary">
                                <i class="bi bi-building me-1"></i> @c.Name
                            </h5>
                            <p class="text-muted mb-1">
                                <i class="bi bi-geo-alt me-1"></i> @c.Standort
                            </p>
                            <p class="text-muted small mb-0">
                                <i class="bi bi-person me-1"></i> @c.Ansprechpartner
                            </p>
                        </div>
                    </div>
                </div>
            }

            @if (!FilteredCustomers.Any())
            {
                <div class="text-center text-muted mt-5">
                    <i class="bi bi-search display-5 d-block mb-2"></i>
                    Keine passenden Kunden gefunden.
                </div>
            }
        }
    </div>
</div>

@code {
    private List<CustomerModel>? customers;
    private string searchText = "";

    private IEnumerable<CustomerModel> FilteredCustomers =>
        string.IsNullOrWhiteSpace(searchText)
            ? customers ?? new List<CustomerModel>()
            : customers!.Where(c =>
                c.Name.Contains(searchText, StringComparison.OrdinalIgnoreCase) ||
                c.Standort.Contains(searchText, StringComparison.OrdinalIgnoreCase));

    protected override async Task OnInitializedAsync()
    {
        try
        {
            customers = await CustomerService.GetCustomersAsync();
            Console.WriteLine($"UI: {customers?.Count ?? 0} Kunden empfangen");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"UI Fehler: {ex.Message}");
            customers = new();
        }

        // Force UI Update
        StateHasChanged();
    }

    private async Task SelectCustomer(CustomerModel c)
    {
    
        await State.SetCustomerAsync(c.Name);

        Nav.NavigateTo($"/stations/{c.Id}");
    }

}
