@model WebVentaCoche.Models.CartViewModel

@{
    ViewData["Title"] = "Carrito de Compras";
}

<div class="container mt-4">
    <h1 class="mb-4">Carrito</h1>

    @if (Model.Products == null || !Model.Products.Any())
    {
        <p>No hay productos en tu carrito.</p>
    }
    else
    {
        <p>Productos en el carrito: @Model.Products.Count</p>
        <hr />

        <table class="table">
            <thead>
                <tr>
                    <th>Imagen</th>
                    <th>Producto</th>
                    <th>Precio</th>
                    <th>Cantidad</th>
                    <th>Subtotal</th>
                    <th></th> 
                </tr>
            </thead>
            <tbody>
                @foreach (var cartItem in Model.Products)
                {
                    <tr>
                        <td>
                            @if (!string.IsNullOrEmpty(cartItem.Product.ImagePath))
                            {
                                <img src="@cartItem.Product.ImagePath"
                                     alt="@cartItem.Product.Name"
                                     class="img-thumbnail"
                                     style="max-height:100px;" />
                            }
                            else
                            {
                                <img src="/images/default.png"
                                     alt="Imagen no disponible"
                                     class="img-thumbnail"
                                     style="max-height:100px;" />
                            }
                        </td>

                        <td>@cartItem.Product.Name</td>

                        <td>@cartItem.Product.Price €</td>

                        <td>
                            <form asp-action="UpdateQuantity" asp-controller="Cart" method="post">
                                <input type="hidden" name="productId" value="@cartItem.Product.Id" />
                                <input type="number" name="quantity" value="@cartItem.Amount"
                                       min="1" class="form-control d-inline" style="width:70px;" />
                                <button type="submit" class="btn btn-sm btn-secondary ms-1">
                                    Actualizar
                                </button>
                            </form>
                        </td>

                        <td>@cartItem.Subtotal €</td>

                        <td>
                            <form asp-action="Remove" asp-controller="Cart" method="post">
                                <input type="hidden" name="id" value="@cartItem.Product.Id" />
                                <button type="submit" class="btn btn-sm btn-danger">
                                    Eliminar
                                </button>
                            </form>
                        </td>
                    </tr>
                }
            </tbody>
        </table>

        <div class="text-end">
            <h4>Total: @Model.Total €</h4>

            <div class="mt-3">
                <form asp-action="Clear" asp-controller="Cart" method="post" class="d-inline">
                    <button type="submit" class="btn btn-warning">
                        Vaciar Carrito
                    </button>
                </form>

                <form asp-action="Purchase" asp-controller="Cart" method="post" class="d-inline">
                    @Html.AntiForgeryToken()
                    <button type="submit" class="btn btn-primary ms-2">
                        Proceder al Pago
                    </button>
                </form>


            </div>
        </div>
    }
</div>
