@page "/students"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@inject AppDbContext Context
@inject IDialogService DialogService
@inject ISnackbar Snackbar

<PageTitle>Students - TSA Chapter Organizer</PageTitle>

<MudText Typo="Typo.h3">Students</MudText>

<MudButton StartIcon="@Icons.Material.Filled.Create" Href="students/create">Create New</MudButton>
<MudButton StartIcon="@AppIcons.EventRank" Href="students/event-ranking">Event Rankings</MudButton>

<MudDataGrid T="Student" ServerData="ServerReload" @ref="_dataGrid" Filterable="true" RowsPerPage="25">
    <Columns>
        @* <PropertyColumn Property="@(e => e.Name)" Title="First Name" SortBy="e => e.FirstName" /> *@
        <TemplateColumn Title="Name" SortBy="e => e.LastName" Sortable="true" >
            <CellTemplate>
                @context.Item.LastNameFirstName 
                @if (context.Item.OfficerRole != null)
                {
                    <MudChip T="string" Icon="@(AppIcons.OfficerRoleIcon(context.Item.OfficerRole.Value))">@context.Item.OfficerRole</MudChip>
                }
            </CellTemplate>
        </TemplateColumn>
        <TemplateColumn Title="Grade (TSA Year)" SortBy="e => e.Grade" Sortable="true">
            <CellTemplate>
                @context.Item.Grade (@context.Item.TsaYear)
            </CellTemplate>
        </TemplateColumn>
        <TemplateColumn>
            <CellTemplate>
                <CrudActions DetailsHref="@($"/students/details?id={context.Item!.Id}")"
                             EditHref="@($"/students/edit?id={context.Item!.Id}")"
                             DeleteOnClick="() => DeleteStudent(context.Item!)">
                </CrudActions>
            </CellTemplate>
        </TemplateColumn>
    </Columns>
    <PagerContent>
        <MudDataGridPager T="Student"></MudDataGridPager>
    </PagerContent>
</MudDataGrid>

@code {
    MudDataGrid<Student> _dataGrid = null!;

    private async Task<GridData<Student>> ServerReload(GridState<Student> state)
    {

        var query = 
            Context.Students.OrderBy(e => e.LastName)
                .Where(state.FilterDefinitions).OrderBy(state.SortDefinitions);

        var totalItems = await query.CountAsync();
        var pagedData = await query.Skip(state.Page * state.PageSize).Take(state.PageSize).ToArrayAsync();

        return new GridData<Student>
        {
            TotalItems = totalItems,
            Items = pagedData
        };
    }

    private async Task DeleteStudent(Student student)
    {
        //_isRowBlocked = true;

        var result = await DialogService
            .ShowMessageBox("Delete student",
                (MarkupString)$"Are you sure want to delete <b>{student.Name}</b>? This cannot be undone.",
                yesText:"Yes",
                noText:"Cancel");

        if (result == true)
        {
            Context.Students.Remove(student!);
            await Context.SaveChangesAsync();
            Snackbar.Add($"Delete event: Delete of Student {student.Name}", Severity.Info);
        }

        //_isRowBlocked = false;
        StateHasChanged();
        _dataGrid.ReloadServerData();
    }
}
