@page "/school-subjects/creation"

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

<h3>SchoolSubjectCreation</h3>

<EditForm Model="@Model" OnValidSubmit="Submit" FormName="school-subject-creation">
    <ValidationSummary />
    <DataAnnotationsValidator />

    <div class="form-group">
        <label for="name" class="form-label">Name</label>
        <InputText class="form-control" @bind-value="Model!.Name" />
    </div>
    <div class="form-group">
        <label for="description" class="form-label">Description</label>
        <InputTextArea class="form-control" @bind-value="Model!.Description"></InputTextArea>
    </div>
    <button type="submit" class="btn btn-primary">Create School Subject</button>
</EditForm>

@code {
    [SupplyParameterFromForm]
    private SchoolSubject? Model { get; set; }

    protected override void OnInitialized()
    {
        if (Model is null) 
        {
            Model = new SchoolSubject
            {
                Name = null!,
                Description = null,
            };
        }
    }

    private async Task Submit()
    {
        await SchoolSubjectService.AddSchoolSubjectAsync(Model!);
        NavigationManager.NavigateTo("/school-subjects");
    }
}
