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

<MudDialog class="mx-auto">
    <DialogContent>
        <MudStack>
            <MudDivider class="-mt-3" />
            <MudStack Spacing="0">
                <MudText Typo="Typo.h6">اطلاعات کلی</MudText>
                <MudText Typo="Typo.caption">اطلاعات کلی را به دقت وارد کنید</MudText>
            </MudStack>
            <MudGrid>
                <MudItem sm="12" lg="6" md="6">
                    <MudTextField T="string" Label="عنوان صفحه سوالات متداول" @bind-Value="@ViewModel.PageDto.Title" Variant="Variant.Outlined"></MudTextField>
                </MudItem>
                <MudItem sm="12" lg="6" md="6">
                    <MudTextField T="string" Label="اسلاگ صفحه سوالات متداول" @bind-Value="@ViewModel.PageDto.Slug" Variant="Variant.Outlined"></MudTextField>
                </MudItem>
                <MudItem sm="12" lg="12" md="12">
                    <MudText Typo="Typo.h6">سوالات متداول</MudText>
                    <MudText Typo="Typo.caption">می توانید سوالات متداول شهر موردنظر را وارد کنید</MudText>
                    <MudGrid>

                        <MudItem lg="5" md="6">
                            <MudTextField @bind-Value="@ViewModel.Question" T="string" Label="سوال" Variant="Variant.Outlined"></MudTextField>
                        </MudItem>

                        <MudItem lg="5" md="6">
                            <MudTextField @bind-Value="@ViewModel.Answer" T="string" Label="پاسخ" Variant="Variant.Outlined"></MudTextField>
                        </MudItem>

                        <MudItem lg="2" md="12">
                            <MudButton Variant="Variant.Filled"
                                       Size="Size.Large"
                                       Color="Color.Info"
                                       class="mt-2 w-full py-3"
                                       OnClick="ViewModel.AddFaq"
                                       StartIcon="@Icons.Material.Outlined.Add">افزودن</MudButton>
                        </MudItem>

                        <MudItem sm="12">
                            <MudExpansionPanels class="mt-1" Elevation="2">
                                @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.PageDto.Faqs.Remove(item.Key)" />
                                                <MudText>@item.Key</MudText>
                                            </MudStack>
                                        </TitleContent>
                                        <ChildContent>
                                            @item.Value
                                        </ChildContent>
                                    </MudExpansionPanel>
                                }
                            </MudExpansionPanels>
                        </MudItem>

                    </MudGrid>
                </MudItem>
            </MudGrid>
        </MudStack>
    </DialogContent>
    <DialogActions>
        <MudStack Row="true" class="mx-4 mb-2 w-full">
            @if (ViewModel.IsEditing)
            {
                
                <BaseButtonUi class="w-64 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Check"
                              Variant="Variant.Filled" Color="Color.Info"
                              Content="ثبت ویرایش" OnClickCallback="@ViewModel.SubmitUpdateAsync" />
            }
            else
            {
                
                <BaseButtonUi class="w-64 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Check"
                              Variant="Variant.Filled" Color="Color.Success"
                              Content="تایید" OnClickCallback="@ViewModel.SubmitAsync" />
            }
            <MudSpacer/>
            <MudButton Variant="Variant.Outlined" Size="Size.Large" Color="Color.Error" OnClick="Cancel">بستن</MudButton>
        </MudStack>
    </DialogActions>
</MudDialog>

@code
{
    [CascadingParameter]
    MudDialogInstance MudDialog { get; set; }

    void Cancel() => MudDialog.Cancel();
    [Parameter]
    public BaseFaq? Faq { get; set; }

    public FaqActionDialogBoxViewModel ViewModel;

    protected override async Task OnInitializedAsync()
    {
        if (Faq != null)
            ViewModel = new FaqActionDialogBoxViewModel(Faq,UserUtility, Snackbar, RestWrapper, MudDialog);
        else
            ViewModel = new FaqActionDialogBoxViewModel(UserUtility, Snackbar, RestWrapper, MudDialog);

        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}