@page "/customers"
@using FilterCair.Shared.Models
@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">
        @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>
        }
    </div>

    @if (FilteredCustomers.Count() == 0)
    {
        <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>

@code {
    private List<CustomerModel> customers = new();
    private string searchText = "";

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

    protected override void OnInitialized()
    {
        // später aus API laden
        customers = new()
        {
            new() { Id = 1, Name = "Freudenberg Weinheim", Standort = "Weinheim", Ansprechpartner = "Marc Wieland", Telefon = "+49 6201 80 1234" },
            new() { Id = 2, Name = "FilterTech GmbH", Standort = "Kaiserslautern", Ansprechpartner = "Lena Becker", Telefon = "+49 631 987654" },
            new() { Id = 3, Name = "CleanAir Solutions", Standort = "Mannheim", Ansprechpartner = "Tom Fischer", Telefon = "+49 621 555 123" }
        };
    }

    private async Task SelectCustomer(CustomerModel c)
    {
        await State.SetCustomerAsync(c.Name);
        Nav.NavigateTo("/qrscanner"); 
    }

}