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

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

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

<MudButton StartIcon="@Icons.Material.Filled.Create" Href="events/create">Create New</MudButton>
<MudButton StartIcon="@Icons.Material.Filled.Print" Href="events/printout">Printable Descriptions</MudButton>

<MudDataGrid T="EventDefinition" ServerData="ServerReload" @ref="_dataGrid" Filterable="true" RowsPerPage="50" >
    <Columns>
        <PropertyColumn Property="@(e => e.Name)" Title="Event Name" Sortable="true" />
        <PropertyColumn Property="@(e => e.EventFormat)" Title="Event Format" />

        <TemplateColumn Title="Team Size" CellStyle="white-space:nowrap">
            <CellTemplate>
                <MudTooltip Text="@context.Item.Eligibility">
                [@context.Item.MinTeamSize&nbsp;-&nbsp;@context.Item.MaxTeamSize]
                </MudTooltip>
            </CellTemplate>
        </TemplateColumn>
        <PropertyColumn Property="@(e => e.ChapterEligibilityCountState)" Title="State#" />
        <TemplateColumn Title="Attributes" Sortable="false">
            <CellTemplate>
                <EventAttributes EventDefinition="context.Item"></EventAttributes>
            </CellTemplate>
        </TemplateColumn>
        
        <PropertyColumn Property="@(e => e.LevelOfEffort)" Title="Level of Effort" />
        <TemplateColumn>
            <CellTemplate>
                <CrudActions DetailsHref="@($"/events/details?id={context.Item!.Id}")"
                             EditHref="@($"/events/edit?id={context.Item!.Id}")"
                             DeleteOnClick="() => DeleteEventDefinition(context.Item!)">
                </CrudActions>
            </CellTemplate>
        </TemplateColumn>
    </Columns>
    <PagerContent>
        <MudDataGridPager T="EventDefinition"></MudDataGridPager>
    </PagerContent>
</MudDataGrid>

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

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

        var query = Context.Events.OrderBy(e => e.Name).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<EventDefinition>
        {
            TotalItems = totalItems,
            Items = pagedData
        };
    }

    private async Task DeleteEventDefinition(EventDefinition evt)
    {
        //_isRowBlocked = true;

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

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

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