@page "/Quotes/Index"

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

@inject QuoteService quoteService

<h1>Quotes</h1>

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

<DataGrid 
    TItem="Quote" 
    Data="@quotes" 
    SelectedRow="@selectedQuote"
    SelectedRowChanged="@OnSelectedQuoteChanged"
    ShowPager
    Bordered
    Hoverable
    Sortable
    Filterable
    Striped
    Responsive>
<DataGridCommandColumn />
    <DataGridColumn Field="@nameof(Quote.QuoteId)" Caption="#" Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.QuotationNumber)" Caption="QuotationNumber" Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.QuotationDate)" Caption="Date" Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.ValidUntil)" Caption="ValidUntil"  Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.TotalNet)" Caption="TotalNet" DisplayFormat="{0:C}" DisplayFormatProvider=cultureInfo Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.TotalGross)" Caption="TotalGross"  DisplayFormat="{0:C}" DisplayFormatProvider=cultureInfo Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.QuoteContains3PP)" Caption="3PP" Filterable Sortable/>
    <DataGridColumn Field="@nameof(Quote.QuoteContainsRB)" Caption="RB" Filterable Sortable/>                
</DataGrid>

    <h2>@selectedQuote.QuotationNumber</h2>                
    <DataGrid 
        TItem="LineItem" 
        Data="@lineItemsInSelectedQuote" 
        Bordered
        Hoverable
        Sortable
        Striped
        Responsive>
        <DataGridCommandColumn />
        <DataGridColumn Field="@nameof(LineItem.Position)" Caption="#"/>
        <DataGridColumn Field="@nameof(LineItem.Amount)" Caption="Amount" Editable />
        <DataGridColumn Field="@nameof(LineItem.ProductNumber)" Caption="ProductNumber" Editable />
        <DataGridColumn Field="@nameof(LineItem.OptionNumber)" Caption="OptionNumber" Editable />
        <DataGridColumn Field="@nameof(LineItem.ProductLine)" Caption="ProductLine" Editable />
        <DataGridColumn Field="@nameof(LineItem.ListPrice)" Caption="ListPrice"  DisplayFormat="{0:C}" DisplayFormatProvider=cultureInfo Editable />
        <DataGridColumn Field="@nameof(LineItem.TotalDiscount)" Caption="TotalDiscount"  DisplayFormat="{0:C}" DisplayFormatProvider=cultureInfo Editable />
        <DataGridColumn Field="@nameof(LineItem.Total)" Caption="Total"  DisplayFormat="{0:C}" DisplayFormatProvider=cultureInfo Editable />
    </DataGrid>

@code {
    public string searchQuote = "";
    CultureInfo cultureInfo = new("de-DE");

    Quote selectedQuote = new();
    List<LineItem> lineItemsInSelectedQuote = new();

    List<Quote> quotes = new();

    protected override async Task OnInitializedAsync()
    {
        quotes = await Task.Run(() => quoteService.GetAllQuotesAsync());
        await OnSelectedQuoteChanged(quotes.LastOrDefault() ?? new());

        await base.OnInitializedAsync();
    }

    private async Task OnSelectedQuoteChanged(Quote selectedQuote)
    {
        lineItemsInSelectedQuote = await quoteService.GetLineItemsAsync(selectedQuote);
        this.selectedQuote = selectedQuote;
    }
}