@rendermode InteractiveServer

@attribute [Authorize]
@using Microsoft.AspNetCore.Authorization
@using WorkManagementTool.Services
@inject SchoolSubjectService SchoolSubjectService

@if (!hasHomeworks)
{
    <button type="button" class="btn btn-danger" @onclick="Delete">-</button>
}

@code {
    [Parameter]
    public int Id { get; set; }

    [Parameter]
    public EventCallback<int> OnDeleted { get; set; }

    private bool hasHomeworks;

    protected override async Task OnInitializedAsync()
    {
        var schoolSubject = await SchoolSubjectService.GetSchoolSubjectByIdAsync(Id);

        hasHomeworks = schoolSubject is not null
            && schoolSubject.Homeworks?.Count > 0;
    }

    private async Task Delete(MouseEventArgs e)
    {
        await SchoolSubjectService.DeleteSchoolSubjectAsync(Id);
        await OnDeleted.InvokeAsync(Id);
    }
}
