@inherits ComponentBase
@using Microsoft.AspNetCore.Identity;

<TableContainer T="InsightUser" 
    @ref="_container" 
    @bind-Search="_search"
    Title="@_title" 
    Breadcrumbs="@_breadcrumbs" 
    Data="LoadDataAsync"
    OnAdd="()=>_createDialog?.Toggle()">
    <Header>
        <MudTh>
            <MudTableSortLabel SortLabel="Email" T="InsightUser">
                Email
            </MudTableSortLabel>
        </MudTh>
        <MudTh>
            <MudTableSortLabel SortLabel="2FA" T="InsightUser">
                2FA
            </MudTableSortLabel>
        </MudTh>
        <MudTh>
            <MudTableSortLabel SortLabel="Lockout" T="InsightUser">
                Lockout
            </MudTableSortLabel>
        </MudTh>
    </Header>
    <RowTemplate>
        <MudTd DataLabel="Email">
            <MudLink Href="@Navigation.Management.Accounts.DetailsHref(context?.Id.ToString())" Typo="Typo.inherit" Underline="Underline.None">
                @context?.Email
            </MudLink>
        </MudTd>
        <MudTd DataLabel="2FA">
            <MudCheckBox Checked="@context?.TwoFactorEnabled" Disabled DisableRipple />
        </MudTd>
        <MudTd DataLabel="Lockout">
            <MudCheckBox Checked="@(context?.LockoutEnd is not null)" Disabled DisableRipple />
        </MudTd>
    </RowTemplate>
    <ActionTemplate>
        <MudMenuItem OnClick="@(()=>OnEdit(context))">
            Edit
        </MudMenuItem>
        <MudMenuItem OnClick="@(()=>OnDelete(context))">
            Delete
        </MudMenuItem>
    </ActionTemplate>
</TableContainer>

<CascadingValue Name="Account" Value="@_selected">
     <AccountCreateDialog @ref="_createDialog" OnChanges="OnRefreshAsync" />
     <AccountEditDialog @ref="_editDialog" OnChanges="OnRefreshAsync" />
     <AccountDeleteDialog @ref="_deleteDialog" OnChanges="OnRefreshAsync" />
</CascadingValue>

@code {
    private AccountCreateDialog? _createDialog;
    private AccountEditDialog? _editDialog;
    private AccountDeleteDialog? _deleteDialog;

    private void OnEdit(InsightUser model)
    {
        _selected = model;
        _editDialog?.Toggle();
    }

    private void OnDelete(InsightUser model)
    {
        _selected = model;
        _deleteDialog?.Toggle();
    }
}