@page "/management/faqs"
@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">
            <MudStack Row="true" class="mb-8">
                <MudText Typo="Typo.h4">سوالات متداول فروشگاه من</MudText>
                <MudSpacer />
                <MudButton Variant="Variant.Filled" Size="Size.Large" Color="Color.Success" OnClick="@ViewModel.SaveAsync">ذخیره سوالات</MudButton>
            </MudStack>
            <MudGrid>
                <MudItem sm="4">
                    <MudTextField T="string" Label="سوال" @bind-Value="@ViewModel.Question" Variant="Variant.Outlined"></MudTextField>
                </MudItem>
                <MudItem sm="6">
                    <MudTextField T="string" Label="پاسخ" @bind-Value="@ViewModel.Answer" Variant="Variant.Outlined"></MudTextField>
                </MudItem>
                <MudItem>
                    <MudButton class="mt-2 py-3" EndIcon="@Icons.Material.Outlined.Add" 
                               Variant="Variant.Outlined" Size="Size.Large" Color="Color.Info" OnClick="@ViewModel.AddNewQuestion">افزودن</MudButton>
                </MudItem>
            </MudGrid>
            <MudPaper>
                <MudExpansionPanels class="mt-8">
                    @foreach (var item in ViewModel.PageDto.Faqs)
                    {
                        <MudExpansionPanel>

                            <TitleContent>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Outlined.Delete"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Error"
                                                   OnClick="()=>ViewModel.RemoveQuestion(item.Key)" />
                                    <MudText>@item.Key</MudText>
                                    </MudStack>
                                </TitleContent>
                                <ChildContent>
                                    @item.Value
                            </ChildContent>
                        </MudExpansionPanel>
                    }
                </MudExpansionPanels>
            </MudPaper>
        </MudItem>
    </MudGrid>
</MudStack>

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