@page "/stock"
@inject StockItemService StockItemService
@inject NavigationManager Navigation
@using CutList.Core.Formatting
@using CutList.Web.Data.Entities

<PageTitle>Stock Items</PageTitle>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h1>Stock Items</h1>
    <a href="stock/new" class="btn btn-primary">Add Stock Item</a>
</div>

@if (loading)
{
    <p><em>Loading...</em></p>
}
else if (stockItems.Count == 0)
{
    <div class="alert alert-info">
        No stock items found. <a href="stock/new">Add your first stock item</a>.
    </div>
}
else
{
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th>Shape</th>
                <th>Type</th>
                <th>Grade</th>
                <th>Size</th>
                <th>Length</th>
                <th>On Hand</th>
                <th style="width: 160px;">Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in stockItems)
            {
                <tr>
                    <td>@item.Material.Shape.GetDisplayName()</td>
                    <td>@item.Material.Type</td>
                    <td>@item.Material.Grade</td>
                    <td>@item.Material.Size</td>
                    <td>@ArchUnits.FormatFromInches((double)item.LengthInches)</td>
                    <td>
                        @if (item.QuantityOnHand > 0)
                        {
                            <span class="badge bg-success">@item.QuantityOnHand</span>
                        }
                        else
                        {
                            <span class="badge bg-secondary">0</span>
                        }
                    </td>
                    <td>
                        <div class="d-flex gap-1">
                            <a href="stock/@item.Id" class="btn btn-sm btn-outline-primary">Edit</a>
                            <button class="btn btn-sm btn-outline-danger" @onclick="() => ConfirmDelete(item)">Delete</button>
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

<ConfirmDialog @ref="deleteDialog"
               Title="Delete Stock Item"
               Message="@deleteMessage"
               ConfirmText="Delete"
               OnConfirm="DeleteConfirmed" />

@code {
    private List<StockItem> stockItems = new();
    private bool loading = true;
    private ConfirmDialog deleteDialog = null!;
    private StockItem? itemToDelete;
    private string deleteMessage = "";

    protected override async Task OnInitializedAsync()
    {
        stockItems = await StockItemService.GetAllAsync();
        loading = false;
    }

    private void ConfirmDelete(StockItem item)
    {
        itemToDelete = item;
        deleteMessage = $"Are you sure you want to delete \"{item.Material.DisplayName} - {ArchUnits.FormatFromInches((double)item.LengthInches)}\"?";
        deleteDialog.Show();
    }

    private async Task DeleteConfirmed()
    {
        if (itemToDelete != null)
        {
            await StockItemService.DeleteAsync(itemToDelete.Id);
            stockItems = await StockItemService.GetAllAsync();
        }
    }
}
