@page "/LineItems/Index"

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

@inject LineItemService lineItemservice

<h1>LineItems</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="searchLineItem"
    @bind-value:event="oninput" placeholder="Search by ProductNumber"
    @onchange="SearchLineItem_OnChange"/>
</div>

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


@if (allLineItems is null)
{
        <p><em>Loading... !</em></p>
}
else
{
    <p>Es wurden @filteredLineItems.Count LineItems gefunden.</p> 
    @if (allLineItems != null)
        {
            <table class="table">
                <thead>
                    <tr>
                        <th>@nameof(LineItem.LineItemId)</th>
                        <th>@nameof(LineItem.QuoteId)</th>
                        <th>@nameof(LineItem.Position)</th>
                        <th>@nameof(LineItem.Amount)</th>
                        <th>@nameof(LineItem.ProductNumber)</th>
                        <th>@nameof(LineItem.OptionNumber)</th>
                        <th>@nameof(LineItem.SapShortDescription)</th>
                        <th>@nameof(LineItem.SapLongDescription)</th>
                        <th>@nameof(LineItem.ProductLine)</th>
                        <th>@nameof(LineItem.TotalDiscount)</th>
                        <th>@nameof(LineItem.SalesDiscount)</th>
                        <th>@nameof(LineItem.PromotionalDiscount)</th>
                        <th>@nameof(LineItem.ContractualDiscount)</th>
                        <th>@nameof(LineItem.DemoDiscount)</th>
                        <th>@nameof(LineItem.ListPrice)</th>
                        <th>@nameof(LineItem.ExtendedListPrice)</th>
                        <th>@nameof(LineItem.NetPrice)</th>
                        <th>@nameof(LineItem.Total)</th>
                      </tr>
                </thead>
                <tbody>
                @foreach (LineItem LineItem in filteredLineItems)
                    {<tr>
                        <td>@LineItem.LineItemId</td>
                        <td>@LineItem.QuoteId</td>
                        <td>@LineItem.Position</td>
                        <td>@LineItem.Amount</td>
                        <td>@LineItem.ProductNumber</td>
                        <td>@LineItem.OptionNumber</td>
                        <td>@LineItem.SapShortDescription</td>
                        <td>@LineItem.SapLongDescription</td>
                        <td>@LineItem.ProductLine</td>
                        <td>@LineItem.TotalDiscount</td>
                        <td>@LineItem.SalesDiscount</td>
                        <td>@LineItem.SalesDiscount</td>
                        <td>@LineItem.PromotionalDiscount</td>
                        <td>@LineItem.ContractualDiscount</td>
                        <td>@LineItem.DemoDiscount</td>
                        <td>@LineItem.ListPrice.ToString("C", cultureInfo)</td>
                        <td>@LineItem.ExtendedListPrice.ToString("C", cultureInfo)</td>
                        <td>@LineItem.NetPrice.ToString("C", cultureInfo)</td>
                        <td>@LineItem.Total.ToString("C", cultureInfo)</td>
                        <td>
                            <a class="nav-link" href="LineItems/Edit/@LineItem.LineItemId">
                                <span class="oi oi-pencil" aria-hidden="true">Edit</span>
                            </a>
                            <a class="nav-link" href="LineItems/Delete/@LineItem.LineItemId">
                                <span class="oi oi-trash" aria-hidden="true">Delete</span>
                            </a>
                        </td>
                    </tr>}
                </tbody>
            </table>
        }
}

@code {

    public string searchLineItem = "";
    List<LineItem> allLineItems = new();
    List<LineItem> filteredLineItems = new();
    CultureInfo cultureInfo = new("de-DE");

    protected override async Task OnInitializedAsync()
    {
        allLineItems = await Task.Run(() => lineItemservice.GetLineItemsAsync());
        filteredLineItems = allLineItems;
    }

    private void SearchLineItem_OnChange()
    {
        filteredLineItems = allLineItems.Where(cD => cD.ProductNumber!.ToLower().Contains(searchLineItem.ToLower())).ToList();
    }
}