@page "/myorders"
@inject HttpClient HttpClient
@inject NavigationManager NavigationManager

<div class="top-bar">
    <a class="logo" href="">
        <img src="img/logo.svg" />
    </a>

    <NavLink class="nav-tab" href="" Match="NavLinkMatch.All">
        <img src="/img/pizza-slice.svg" />
        <div>Get Pizza</div>
    </NavLink>
    <NavLink class="nav-tab" href="myorders" Match="NavLinkMatch.All">
        <img src="img/bike.svg" />
        <div>My orders</div>
    </NavLink>
</div>

<div class="main">
    @if (_ordersWithStatus == null)
    {
        <text>Loading...</text>
    }
    else if (!_ordersWithStatus.Any())
    {
        <h2>No orders placed</h2>
        <a class="btn btn-success" href="">Order some pizza</a>
    }
    else
    {
        <div class="list-group orders-list">
            @foreach (var item in _ordersWithStatus)
            {
                <div class="list-group-item">
                    <div class="col">
                        <h5>@item.Order.CreatedTime.ToLongDateString()</h5>
                        Items:
                        <strong>@item.Order.Pizzas.Count()</strong>
                        Total price:
                        <string>@item.Order.GetFormattedTotalPrice()</string>
                    </div>
                    <div class="col">
                        Status: <strong>@item.StatusText</strong>
                    </div>
                    @if (item.StatusText != "Delivered")
                    {
                        <div class="col flex-grow-0">
                            <a class="btn btn-success" href="myorders/">
                                Track >
                            </a>
                        </div>
                    }
                </div>
            }
        </div>
    }
</div>

@code {
    List<OrderWithStatus> _ordersWithStatus = new List<OrderWithStatus>();

    protected override async Task OnParametersSetAsync()
    {
        _ordersWithStatus = await HttpClient.GetFromJsonAsync<List<OrderWithStatus>>($"{NavigationManager.BaseUri}orders");
    }
}
