@page "/SdvShop/Shop/ShoppingCart"

<ShoppingCartBanner></ShoppingCartBanner>

@if (this.products != null)
{
    <section class="cart_area">
        <div class="container">
            <div class="cart_inner">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">Product</th>
                                <th scope="col">Price</th>
                                <th scope="col">Quantity</th>
                                <th scope="col">Total</th>
                                <th scope="col">Remove</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var product in this.products)
                            {
                                <BasketProductCard Product="product"
                                                   OnRemovingProduct="@this.RemoveProductClickHandler">
                                </BasketProductCard>
                            }
                            <tr>
                                <td></td>
                                <td></td>
                                <td>
                                    <h5>Subtotal</h5>
                                </td>
                                <td>
                                    <h5>@this.products.Sum(x => x.TotalProductPrice).ToString("F2")</h5>
                                </td>
                            </tr>
                            <tr class="out_button_area">
                                <td></td>
                                <td></td>
                                <td></td>
                                <td>
                                    <div class="checkout_btn_inner">
                                        <a class="gray_btn" href="/SdvShop/Shop">Continue Shopping</a>
                                        <a class="main_btn" href="/SdvShop/Shop/Checkout">Proceed to checkout</a>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>
}

@code {
    private ICollection<ProductInCartViewModel> products = new List<ProductInCartViewModel>();

    private void RemoveProductClickHandler(string productId)
    {
        this.ShoppingCartState.RemoveProduct(productId);
        this.NavigationManager.NavigateTo($"/SdvShop/Shop/Product/{productId}");
    }

    protected override async Task OnInitializedAsync()
    {
        this.products = await this.ShoppingCartState.ExtractAllProducts();
    }
}