@model CartWise.Web.ViewModels.Purchase.PurchaseListViewModel
@{
    ViewData["Title"] = "Purchases";
}

<h1>Purchases</h1>

<form asp-controller="Purchase" asp-action="Start" method="post" class="mb-4">
    <button type="submit" class="btn btn-primary">Record a new purchase</button>
</form>

@if (Model.Purchases.Count == 0)
{
    <p class="text-muted">No purchases recorded yet.</p>
}
else
{
    <ul class="list-group">
        @foreach (var purchase in Model.Purchases)
        {
            <li class="list-group-item d-flex justify-content-between align-items-center">
                <a asp-controller="Purchase" asp-action="Details" asp-route-id="@purchase.PurchaseId">
                    @purchase.PurchasedUtc.ToLocalTime().ToString("MMMM d, yyyy")
                    <span class="text-muted">(@purchase.ItemCount item@(purchase.ItemCount == 1 ? "" : "s"))</span>
                </a>
                <span>
                    @if (!purchase.IsCompleted)
                    {
                        <span class="badge bg-warning text-dark">In progress</span>
                    }
                    else if (purchase.Total is not null)
                    {
                        <span>@purchase.Total.Value.ToString("C")</span>
                    }
                </span>
            </li>
        }
    </ul>
}
