@page "/setting/pages"
@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">
    <MudStack class="mx-2 mb-5">
        <MudText Typo="Typo.h4">تنظیمات برگه ها</MudText>
        <MudText Typo="Typo.caption">برگه های وب سایت خود را ویرایش نمایید</MudText>
    </MudStack>

    <MudPaper class="px-5 py-5">
        <MudStack>
            <MudGrid>
                <MudItem xs="12">
                    <MudText class="mb-5 mt-4" Typo="Typo.h6">افزودن لینک حذف شده</MudText>

                    <MudGrid>
                        <MudItem xs="10">
                            <MudTextField @bind-Value="@ViewModel.NewDeletedPage.Url" T="string" Label="لینک" Variant="Variant.Outlined" />
                        </MudItem>
                        <MudItem xs="2">
                            <MudButton class="mt-1 w-full py-3" Variant="Variant.Outlined" Color="Color.Secondary" OnClick="ViewModel.AddDeletedPage">افزودن +</MudButton>
                        </MudItem>
                    </MudGrid>

                </MudItem>
                <MudItem xs="12">


                    <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                                 T="DeletedPageItem" Items="@ViewModel.DeletedPages" CurrentPage="@ViewModel.DeletedPagesCurrentPage"
                                 RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                                 SortMode="@SortMode.None" Groupable="false">
                        <Columns>
                            <PropertyColumn Title="لینک حذف شده" Property="arg => arg.Url" />
                            <TemplateColumn CellClass="d-flex justify-end">
                                <CellTemplate>
                                    <MudStack Row="true">
                                        <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                       Size="@Size.Small"
                                                       Variant="@Variant.Outlined"
                                                       OnClick="async () => await ViewModel.RemoveDeletedPageAsync(context.Item)"
                                                       Color="@Color.Error" />
                                    </MudStack>
                                </CellTemplate>
                            </TemplateColumn>
                        </Columns>
                        <PagerContent>
                            <MudStack Row="true" class="w-full">

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

                        </PagerContent>
                    </MudDataGrid>
                </MudItem>
            </MudGrid>
        </MudStack>
    </MudPaper>
    <MudPaper class="px-5 py-5">
        <MudStack>
            <MudGrid>
                <MudItem xs="12">
                    <MudText class="mb-5 mt-4" Typo="Typo.h6">افزودن ریدایرکت جدید</MudText>

                    <MudGrid>
                        <MudItem xs="5">
                            <MudTextField @bind-Value="@ViewModel.NewRedirectItem.OldUrl" T="string" Label="لینک قدیمی" Variant="Variant.Outlined" />
                        </MudItem>
                        <MudItem xs="5">
                            <MudTextField @bind-Value="@ViewModel.NewRedirectItem.NewUrl" T="string" Label="لینک جدید" Variant="Variant.Outlined" />
                        </MudItem>
                        <MudItem xs="2">
                            <MudButton class="mt-1 w-full py-3" Variant="Variant.Outlined" Color="Color.Secondary" OnClick="ViewModel.AddPageAsync">افزودن +</MudButton>
                        </MudItem>
                    </MudGrid>

                </MudItem>
                <MudItem xs="12">


                    <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                                 T="RedirectItem" Items="@ViewModel.RedirectItems" CurrentPage="@ViewModel.RedirectItemsCurrentPage"
                                 RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                                 SortMode="@SortMode.None" Groupable="false">
                        <Columns>
                            <PropertyColumn Title="لینک قدیمی" Property="arg => arg.OldUrl" />
                            <PropertyColumn Title="لینک جدید" Property="arg => arg.NewUrl" />
                            <TemplateColumn CellClass="d-flex justify-end">
                                <CellTemplate>
                                    <MudStack Row="true">
                                        <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                       Size="@Size.Small"
                                                       Variant="@Variant.Outlined"
                                                       OnClick="async () => await ViewModel.RemovePageAsync(context.Item)"
                                                       Color="@Color.Error" />
                                    </MudStack>
                                </CellTemplate>
                            </TemplateColumn>
                        </Columns>
                        <PagerContent>
                            <MudStack Row="true" class="w-full">

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

                        </PagerContent>
                    </MudDataGrid>
                </MudItem>
            </MudGrid>
        </MudStack>
    </MudPaper>
</MudStack>

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