@page "/admin/categories"
@attribute [Authorize]
@using Components.RazorComponents
@inject IBlogApi _api
<h3>Categories</h3>
<EditForm OnValidSubmit="Save" Model="Item">
    <DataAnnotationsValidator />
    <CustomCssClassProvider ProviderType="BootstrapFieldCssClassProvider" />
    <InputText @bind-Value="@Item.Name"  />
    <ValidationMessage For="@(()=>Item.Name)" />
    <button class="btn btn-success" type="submit">Save</button>
</EditForm>
<ItemList Items="Items" DeleteEvent="@Delete" SelectEvent="@Select" ItemType="Category">
    <ItemTemplate>
        @{
            var item = context as Category;
            if (item != null)
            {
                @item.Name
            }
        }
    </ItemTemplate>
</ItemList>
@code {
    private List<Category> Items { get; set; } = new();
    public Category Item { get; set; } = new();
    protected async override Task OnInitializedAsync()
    {
        Items = (await _api.GetCategoriesAsync()) ?? new();
        await base.OnInitializedAsync();
    }

    private async Task Delete(Category category)
    {
        try
        {
            await _api.DeleteCategoryAsync(category.Id!);
            Items.Remove(category);
        }
        catch { }
    }

    private async Task Save()
    {
        try
        {
            await _api.SaveCategoryAsync(Item);
            if (!Items.Contains(Item))
            {
                Items.Add(Item);
            }
            Item = new Category();
        }
        catch { }
    }

    private Task Select(Category category)
    {
        try
        {
            Item = category;
        }
        catch { }
        return Task.CompletedTask;
    }
}
