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

<h1>Purchase — @Model.PurchasedUtc.ToLocalTime().ToString("MMMM d, yyyy")</h1>
<p class="text-muted">@(Model.IsCompleted ? "Completed" : "In progress")</p>

@if (Model.Items.Count == 0)
{
    <p class="text-muted">No items yet.</p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th scope="col">Item</th>
                <th scope="col">Quantity</th>
                <th scope="col">Price</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Items)
            {
                <tr>
                    <td>@item.Description</td>
                    <td>@item.Quantity.ToString("0.###")@(string.IsNullOrEmpty(item.Unit) ? "" : $" {item.Unit}")</td>
                    <td>@item.LinePrice.ToString("C")</td>
                </tr>
            }
        </tbody>
    </table>
}

@if (!Model.IsCompleted)
{
    <form asp-controller="Purchase" asp-action="AddItem" asp-route-id="@Model.PurchaseId" method="post" class="row g-2 mb-4">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="col-12 col-sm-5">
            <label asp-for="NewItem.Description" class="visually-hidden"></label>
            <input asp-for="NewItem.Description" class="form-control" placeholder="Item" autofocus />
            <span asp-validation-for="NewItem.Description" class="text-danger"></span>
        </div>
        <div class="col-4 col-sm-2">
            <label asp-for="NewItem.Quantity" class="visually-hidden"></label>
            <input asp-for="NewItem.Quantity" class="form-control" placeholder="Qty" inputmode="decimal" />
            <span asp-validation-for="NewItem.Quantity" class="text-danger"></span>
        </div>
        <div class="col-4 col-sm-2">
            <label asp-for="NewItem.Unit" class="visually-hidden"></label>
            <input asp-for="NewItem.Unit" class="form-control" placeholder="Unit" />
            <span asp-validation-for="NewItem.Unit" class="text-danger"></span>
        </div>
        <div class="col-4 col-sm-2">
            <label asp-for="NewItem.LinePrice" class="visually-hidden"></label>
            <input asp-for="NewItem.LinePrice" class="form-control" placeholder="Price" inputmode="decimal" />
            <span asp-validation-for="NewItem.LinePrice" class="text-danger"></span>
        </div>
        <div class="col-12 col-sm-1">
            <button type="submit" class="btn btn-primary w-100">Add</button>
        </div>
    </form>

    <form asp-controller="Purchase" asp-action="Complete" asp-route-id="@Model.PurchaseId" method="post">
        <button type="submit" class="btn btn-success" @(Model.Items.Count == 0 ? "disabled" : "")>Complete purchase</button>
    </form>
}
else
{
    <dl class="row">
        <dt class="col-sm-3">Subtotal</dt>
        <dd class="col-sm-9">@(Model.Subtotal?.ToString("C") ?? "—")</dd>
        <dt class="col-sm-3">Tax</dt>
        <dd class="col-sm-9">@(Model.Tax?.ToString("C") ?? "—")</dd>
        <dt class="col-sm-3">Total</dt>
        <dd class="col-sm-9">@(Model.Total?.ToString("C") ?? "—")</dd>
    </dl>
}

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
