@page "/user"
@page "/users"
@using FoodsharingSiegen.Contracts.Entity
@using FoodsharingSiegen.Server.Dialogs

@inherits FoodsharingSiegen.Server.BaseClasses.FsBase

@code {

    private RenderFragment PopupTitleTemplate(PopupTitleContext<User> value)
    {
        var header = "Benutzer erstellen";
        if (value.EditState == DataGridEditState.Edit) header = "Benutzer bearbeiten";

        return builder =>
        {
            builder.OpenElement(0, "span");
            builder.AddContent(1, header);
            builder.CloseElement();
        };
    }

}

<PageTitle>Benutzerverwaltung - @AppSettings.Terms.Title</PageTitle>

<h2>Benutzerverwaltung <span style="font-size: .5em; line-height: 0;">Admin</span></h2>

<div class="my-2">
    <Button Color="Color.Primary" Disabled="@(SelectedUser == null)" Clicked="async () => await PasswordModal?.Show(SelectedUser!)!"><i class="fa-solid fa-key"></i>&nbsp;setzen</Button>
</div>

<DataGrid TItem="User"
          @ref="UserDataGrid"
          Data="@UserList"
          CommandMode="DataGridCommandMode.Commands"
          EditMode="DataGridEditMode.Popup"
          PopupTitleTemplate="PopupTitleTemplate"
          RowInserted="RowInserted"
          RowUpdated="RowUpdated"
          PageSize="50"
          @bind-SelectedRow="SelectedUser"
          RowDoubleClicked="arg => UserDataGrid?.Edit(arg.Item)!"
          Editable
          Responsive="true">
    <DataGridColumns>
        <DataGridCommandColumn TItem="User" Width="100px" CellClass="@(_ => "px-0 d-flex align-items-center justify-content-center")">
            <NewCommandTemplate>
                <Button Size="Size.ExtraSmall" Color="Color.Success" Clicked="@context.Clicked" Class="mr-1" Style="min-width: auto;">
                    <i class="oi oi-plus"></i>
                </Button>
            </NewCommandTemplate>
            <EditCommandTemplate>
                <Button Size="Size.ExtraSmall" Color="Color.Secondary" Clicked="@context.Clicked" Class="mr-1" Style="min-width: auto;">
                    <i class="oi oi-pencil"></i>
                </Button>
            </EditCommandTemplate>
            <DeleteCommandTemplate>
                <Button Size="Size.ExtraSmall" Color="Color.Danger" Clicked="() => RemoveUserAsync(context.Item)" Class="mr-1" Style="min-width: auto;">
                    <i class="oi oi-trash"></i>
                </Button>
            </DeleteCommandTemplate>
            <ClearFilterCommandTemplate>
                <Button Size="Size.ExtraSmall" Color="Color.Danger" Clicked="@context.Clicked" Style="min-width: auto;">
                    <i class="o"></i> <i class="fas fa-trash"></i>
                </Button>
            </ClearFilterCommandTemplate>
        </DataGridCommandColumn>
        <DataGridCheckColumn TItem="User" Field="@nameof(User.Verified)" Caption="Verifiziert" Editable="true" Width="100px">
            <DisplayTemplate>
                <Check TValue="bool" Checked="context.Verified" Disabled="true" ReadOnly="true"/>
            </DisplayTemplate>
        </DataGridCheckColumn>
        <DataGridColumn TItem="User" Field="@nameof(User.Type)" Caption="Typ" Editable="true" Width="200px">
            <EditTemplate>
                <Select TValue="UserType" SelectedValue="@((UserType)context.CellValue)" SelectedValueChanged="@(v => context.CellValue = v)">
                    @foreach (var enumValue in Enum.GetValues<UserType>())
                    {
                        <SelectItem TValue="UserType" Value="enumValue">@enumValue</SelectItem>
                    }
                </Select>
            </EditTemplate>
        </DataGridColumn>
        <DataGridColumn TItem="User" Field="@nameof(User.Name)" Caption="Name" Editable="true" Width="250px"></DataGridColumn>
        <DataGridColumn TItem="User" Field="@nameof(User.Mail)" Caption="E-Mail" Editable="true"></DataGridColumn>
        <DataGridColumn TItem="User" Field="@nameof(User.GroupsList)" Caption="Gruppen" Editable="true">
            <EditTemplate>
                <Autocomplete TItem="UserGroup"
                              TValue="UserGroup"
                              Size="Size.ExtraSmall"
                              Data="@UserGroups"
                              TextField="@(( item ) => item.ToString())"
                              ValueField="@(( item ) => item)"
                              Multiple="true"
                              SelectedValues="@((List<UserGroup>) context.CellValue)"
                              SelectedValuesChanged="@(v => context.CellValue = v)"
                              @bind-SelectedTexts="SelectedCompanyTexts">
                </Autocomplete>
                <small>Verfügbar: @string.Join(", ", Enum.GetValues<UserGroup>())</small>
            </EditTemplate>
            <DisplayTemplate>
                @if (string.IsNullOrWhiteSpace(context.Groups))
                {
                    <span style="font-style: italic;">Keine Gruppen</span>
                }
                else
                {
                    <span>@string.Join(", ", context.GroupsList)</span>
                }
            </DisplayTemplate>
        </DataGridColumn>
    </DataGridColumns>
</DataGrid>

<SetPasswordModal @ref="PasswordModal" OnPasswortSet="OnPasswordSet"></SetPasswordModal>