@typeparam ItemType
@using System.Collections.Generic
@inject IJSRuntime jsRuntime

<h3>List</h3>
<table>
    <Virtualize Items="@Items" Context="item">
        <tr>
            <td>
                <button class="btn btn-primary" @onclick="@(()=>{SelectEvent.InvokeAsync(item); })">                  Select</button>
            </td>
            <td>@ItemTemplate(item)</td>
            <td><button class="btn btn-danger" @onclick="@(async ()=>{ if (await ShouldDelete()) { await DeleteEvent.InvokeAsync(item); } })">Delete</button></td>
        </tr>
    </Virtualize>
</table>


@code{
    [Parameter]
    public List<ItemType> Items { get; set; } = new List<ItemType>();
    [Parameter, EditorRequired]
    public required RenderFragment<ItemType> ItemTemplate { get; set; }
    [Parameter]
    public EventCallback<ItemType> DeleteEvent { get; set; }
    [Parameter]
    public EventCallback<ItemType> SelectEvent { get; set; }
    
    IJSObjectReference jsmodule;
    private async Task<bool> ShouldDelete()
    {
        jsmodule = await jsRuntime.InvokeAsync<IJSObjectReference>("import", "/_content/Components/RazorComponents/ItemList.razor.js");
        return await jsmodule.InvokeAsync<bool>("showConfirm", "Are you sure?");
    }

}