@page "/school-subjects"

@rendermode InteractiveServer

@using WorkManagementTool.Services
@using WorkManagementTool.Data.Entities;
@inject SchoolSubjectService SchoolSubjectService

<h3>SchoolSubjectListing</h3>

<div>
    <NavLink class="btn btn-primary" href="school-subjects/creation" Match="NavLinkMatch.All">
       Create
    </NavLink>
</div>

@if (schoolSubjects.Count == 0)
{
    <p>No school subjects found.</p>
}
else
{
    <ul>
        @foreach (var schoolSubject in schoolSubjects)
        {
            <li>
                <SchoolSubjectDisplay Id="@schoolSubject.Id" />
                <div class="btn-group">
                    <SchoolSubjectDeletion Id="@schoolSubject.Id" OnDeleted="HandleDeletion" />
                    <NavLink href="@($"/homeworks/{schoolSubject.Id}")" class="btn btn-primary">Manage homeworks</NavLink>
                </div>
            </li>
        }
    </ul>
}

@code {
    private List<SchoolSubject> schoolSubjects = new();

    private async Task HandleDeletion(int deletedIt)
    {
        schoolSubjects.RemoveAll(s => s.Id == deletedIt);
        StateHasChanged();
    }

    protected override async Task OnInitializedAsync()
    {
        schoolSubjects = await SchoolSubjectService.GetAllSchoolSubjectsAsync();
    }
}
