@page "/CustomDescriptions/Index"

@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;

@inject CustomDescriptionService customDescriptionservice

<h1>CustomDescriptions</h1>

<div class="text-center bg-blue-100">
    <input class="border-4 w-1/3 rounded m-6 p-6 h-8
               border-blue-300" @bind-value="searchCustomDescription"
    @bind-value:event="oninput" placeholder="Search by ProductNumber"
    @onchange="SearchCustomDescription_OnChange"/>
</div>

<NavLink class="nav-link" href="CustomDescriptions/Add">
    <span class="oi oi-plus" aria-hidden="true">Add New CustomDescription</span>
</NavLink>


@if (allCustomDescriptions is null)
{
        <p><em>Loading... !</em></p>
}
else
{
    <p>Es wurden @filteredCustomDescriptions.Count CustomDescriptions gefunden.</p> 
    @if (allCustomDescriptions != null)
        {
            <table class="table">
                <thead>
                    <tr>
                        <th>@nameof(CustomDescription.CustomDescriptionId)</th>
                        <th>@nameof(CustomDescription.ProductNumber)</th>
                        <th>@nameof(CustomDescription.OptionNumber)</th>
                        <th>@nameof(CustomDescription.Heading)</th>
                        <th>@nameof(CustomDescription.DescriptionText)</th>
                    </tr>
                </thead>
                <tbody>
                @foreach (CustomDescription customDescription in filteredCustomDescriptions)
                    {<tr>
                        <td>@customDescription.CustomDescriptionId</td>
                        <td>@customDescription.ProductNumber</td>
                        <td>@customDescription.OptionNumber</td>
                        <td>@customDescription.Heading</td>
                        <td>@customDescription.DescriptionText</td>
                        <td>
                            <a class="nav-link" href="CustomDescriptions/Edit/@customDescription.CustomDescriptionId">
                                <span class="oi oi-pencil" aria-hidden="true">Edit</span>
                            </a>
                            <a class="nav-link" href="CustomDescriptions/Delete/@customDescription.CustomDescriptionId">
                                <span class="oi oi-trash" aria-hidden="true">Delete</span>
                            </a>
                        </td>
                    </tr>}
                </tbody>
            </table>
        }
}

@code {

    public string searchCustomDescription = "";
    List<CustomDescription> allCustomDescriptions = new();
    List<CustomDescription> filteredCustomDescriptions = new();

    protected override async Task OnInitializedAsync()
    {
        allCustomDescriptions = await Task.Run(() => customDescriptionservice.GetAllCustomDescriptionsAsync());
        filteredCustomDescriptions = allCustomDescriptions;
    }

    private void SearchCustomDescription_OnChange()
    {
        filteredCustomDescriptions = allCustomDescriptions.Where(cD => cD.ProductNumber.ToLower().Contains(searchCustomDescription.ToLower())).ToList();
    }
}