@using Aberwyn.Models
@using Newtonsoft.Json
@model PizzaAdminViewModel

<div id="ordersContainer">
    <div class="card-grid">
        @foreach (var order in Model.ActiveOrders)
        {
            var ingredients = new List<string>();
            try
            {
                if (!string.IsNullOrEmpty(order.IngredientsJson))
                {
                    ingredients = System.Text.Json.JsonSerializer.Deserialize<List<string>>(order.IngredientsJson);
                }
            }
            catch
            {
                ingredients = order.IngredientsJson?.Split('\n').ToList() ?? new List<string>();
            }

            var cardClass = order.Status switch
            {
                "Confirmed" => "border-warning",
                "Finished" => "border-success",
                _ => "border-info"
            };

            <div class="card @cardClass">
                <div class="card-header">@order.CustomerName – @order.PizzaName</div>
                <div class="card-body">
                    <ul class="mb-2">
                        @foreach (var ing in ingredients)
                        {
                            <li>@ing</li>
                        }
                    </ul>
                    <form id="form-@order.Id" method="post" asp-action="UpdatePizzaOrder">
                        <input type="hidden" name="id" value="@order.Id" />
                        <input type="hidden" name="ingredientsJson" value='@System.Text.Json.JsonSerializer.Serialize(ingredients)' />
                        <div class="d-flex gap-2">
                            @if (order.Status == "Unconfirmed")
                            {
                                <input type="hidden" name="status" value="Confirmed" />
                                <button type="submit" class="btn btn-sm btn-outline-primary">Bekräfta</button>
                            }
                            else if (order.Status == "Confirmed")
                            {
                                <input type="hidden" name="status" value="Finished" />
                                <button type="submit" class="btn btn-sm btn-outline-success">✔ Klar</button>
                            }
                            <button type="submit" formaction="@Url.Action("UpdatePizzaOrder", new { status = "Neka", id = order.Id })" class="btn btn-sm btn-outline-danger">Neka</button>
                        </div>
                    </form>
                </div>
            </div>
        }
    </div>

<h3 class="mt-4">Klara beställningar</h3>
<div class="completed-orders-grid">
@foreach (var order in Model.CompletedOrders)
{
    var tooltip = string.Join(", ", JsonConvert.DeserializeObject<List<string>>(order.IngredientsJson ?? "[]"));
    <div class="completed-order-box" title="@tooltip">
        <form class="restore-form" method="post" asp-action="UpdatePizzaOrder">
            <input type="hidden" name="id" value="@order.Id" />
            <input type="hidden" name="status" value="Confirmed" />
            <input type="hidden" name="ingredientsJson" value='@order.IngredientsJson' />
            <button type="submit" class="btn btn-sm btn-outline-warning">↩️</button>
        </form>
        <strong>@order.CustomerName</strong>
        <span>@order.PizzaName</span>
    </div>
}
</div>


</div>
