@page "/materials"
@inject MaterialService MaterialService
@inject NavigationManager Navigation

<PageTitle>Materials</PageTitle>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h1>Materials</h1>
    <a href="materials/new" class="btn btn-primary">Add Material</a>
</div>

@if (loading)
{
    <p><em>Loading...</em></p>
}
else if (!string.IsNullOrEmpty(errorMessage))
{
    <div class="alert alert-danger">
        <strong>Error loading materials:</strong>
        <pre style="white-space: pre-wrap;">@errorMessage</pre>
    </div>
}
else if (materials.Count == 0)
{
    <div class="alert alert-info">
        No materials found. <a href="materials/new">Add your first material</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>Description</th>
                <th style="width: 160px;">Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var material in materials)
            {
                <tr>
                    <td>@material.Shape.GetDisplayName()</td>
                    <td>@material.Type</td>
                    <td>@material.Grade</td>
                    <td>@material.Size</td>
                    <td>@material.Description</td>
                    <td>
                        <div class="d-flex gap-1">
                            <a href="materials/@material.Id" class="btn btn-sm btn-outline-primary">Edit</a>
                            <button class="btn btn-sm btn-outline-danger" @onclick="() => ConfirmDelete(material)">Delete</button>
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

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

@code {
    private List<Material> materials = new();
    private bool loading = true;
    private string? errorMessage;
    private ConfirmDialog deleteDialog = null!;
    private Material? materialToDelete;
    private string deleteMessage = "";

    protected override async Task OnInitializedAsync()
    {
        try
        {
            materials = await MaterialService.GetAllAsync();
        }
        catch (Exception ex)
        {
            errorMessage = ex.ToString();
        }
        finally
        {
            loading = false;
        }
    }

    private void ConfirmDelete(Material material)
    {
        materialToDelete = material;
        deleteMessage = $"Are you sure you want to delete \"{material.Shape} - {material.Size}\"?";
        deleteDialog.Show();
    }

    private async Task DeleteConfirmed()
    {
        if (materialToDelete != null)
        {
            await MaterialService.DeleteAsync(materialToDelete.Id);
            materials = await MaterialService.GetAllAsync();
        }
    }
}
