@page "/setting/users"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]

@inject IDialogService DialogService
@inject NavigationManager NavigationManager
@inject ISnackbar Snackbar
@inject IUserUtility UserUtility
@inject IRestWrapper RestWrapper

<MudStack class="h-full w-full p-8">
    <MudGrid>
        
        <MudItem xs="12" md="6">
            <MudStack Row="true" class="mb-5">
                <MudText Typo="Typo.h4">کاربـــــران</MudText>
                <MudChip Color="Color.Info" Variant="Variant.Outlined">124 عدد</MudChip>
                <MudSpacer />
                <MudButton Variant="Variant.Filled"
                           DisableElevation="true"
                           StartIcon="@Icons.Material.Outlined.Add"
                           Color="Color.Secondary"
                           OnClick="@ViewModel.AddUserClicked"
                           class="my-auto">افزودن کاربر</MudButton>
            </MudStack>
            <MudPaper class="!max-h-[80vh] overflow-auto">
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="ApplicationUserSDto" Items="@ViewModel.Users" CurrentPage="@ViewModel.UsersCurrentPage"
                             RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                             SortMode="@SortMode.None" Groupable="false">

                    <ToolBarContent>
                        <MudTextField T="string" Placeholder="جست جو بر اساس شماره تلفن" Adornment="Adornment.Start" Immediate="true"
                                      Clearable="true"
                                      ValueChanged="@ViewModel.SearchUserChanged"
                                      AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" class="my-auto"
                                      OnAdornmentClick="@ViewModel.SearchUserAsync"></MudTextField>
                    </ToolBarContent>
                    <Columns>
                        <PropertyColumn Title="نام و نام خانوادگی" Property="arg => arg.FullName" />
                        <PropertyColumn Title="شماره تماس" Property="arg => arg.PhoneNumber" />
                        <PropertyColumn Title="نقش" Property="arg => arg.RoleName" />
                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Info"
                                                   OnClick="async()=>await ViewModel.EditUserClicked(context.Item)" />
                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.DeleteUserAsync(context.Item.Id)"
                                                   Color="@Color.Error" />
                                </MudStack>
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                    <PagerContent>
                        <MudStack Row="true" class="w-full">

                            <MudPagination Rectangular="true" Variant="Variant.Filled" Count="@ViewModel.UsersPageCount"
                                           SelectedChanged="@ViewModel.ChangeUserPageAsync" class="mx-auto my-4" />
                        </MudStack>

                    </PagerContent>
                </MudDataGrid>
            </MudPaper>
        </MudItem>

        <MudItem xs="12" md="6">
            <MudStack Row="true" class="mb-5">
                <MudText Typo="Typo.h4">نــــــقش ها</MudText>
                <MudChip Color="Color.Info" Variant="Variant.Outlined">124 عدد</MudChip>
                <MudSpacer/>
                <MudButton Variant="Variant.Filled"
                           DisableElevation="true"
                           StartIcon="@Icons.Material.Outlined.Add"
                           Color="Color.Secondary"
                           OnClick="@ViewModel.AddRoleClicked"
                           class="my-auto">افزودن نقش</MudButton>
            </MudStack>
            <MudPaper class="!max-h-[80vh] overflow-auto">
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="ApplicationRole" Items="@ViewModel.Roles" CurrentPage="@ViewModel.RolesCurrentPage"
                             RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                             SortMode="@SortMode.None" Groupable="false">

                    <Columns>
                        <PropertyColumn Title="نام" Property="arg => arg.PersianName" />
                        <PropertyColumn Title="توضیحاتـــ" Property="arg => arg.Description"/>
                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async()=>await ViewModel.EditRoleClicked(context.Item)"
                                                   Color="@Color.Info"/>
                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   OnClick="async () => await ViewModel.DeleteRoleAsync(context.Item.Id)"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Error"/>
                                </MudStack>
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                    <PagerContent>
                        <MudStack Row="true" class="w-full">

                            <MudPagination Rectangular="true" Variant="Variant.Filled" Count="@ViewModel.RolesPageCount"
                                           SelectedChanged="@ViewModel.ChangeRolePageAsync" class="mx-auto my-4"/>
                        </MudStack>

                    </PagerContent>
                </MudDataGrid>
            </MudPaper>
        </MudItem>

    </MudGrid>
</MudStack>

@code
{
    public UserSettingsPageViewModel ViewModel { get; set; }
    protected override async Task OnInitializedAsync()
    {
        ViewModel = new UserSettingsPageViewModel(NavigationManager, Snackbar, UserUtility, RestWrapper, DialogService);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}
