@using BlazingPizza.Services

@page "/checkout"
@inject OrderState OrderState
@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">
        <img src="img/bike.svg" />
        <div>My orders</div>
    </NavLink>
</div>

<div class="main">
    <div class="checkout-cols">
        <div class="checkout-order-details">
            <h4>Review order</h4>
            @foreach (var pizza in Order.Pizzas)
            {
                <p>
                    <string>
                        @(pizza.Size)"
                        @pizza.Special.Name
                        (£@pizza.GetFormattedTotalPrice())
                    </string>
                </p>
            }

            <p>
                <string>
                    Total price:
                    £@Order.GetFormattedTotalPrice()
                </string>
            </p>
        </div>
    </div>

    <button class="checkout-button btn btn-warning" @onclick="PlaceOrder" disabled="@isSubmitting">
        Place order
    </button>
</div>

@code {
    Order Order => OrderState.Order;
    bool isSubmitting;

    async Task PlaceOrder()
    {
        isSubmitting = true;
        var response = await HttpClient.PostAsJsonAsync(NavigationManager.BaseUri + "orders", OrderState.Order);
        var newOrderId = await response.Content.ReadFromJsonAsync<int>();
        OrderState.ResetOrder();

        NavigationManager.NavigateTo("/");
    }
}
