@using BackOffice.Services.Content
@using BackOffice.Pages.Content.Components.SettingsModels
@using Microsoft.AspNetCore.Components.Forms
@using BackOffice.Common.BaseComponents

<MudDialog>
    <DialogContent>
        <MudForm @ref="_form" @bind-IsValid="@_isValid">
            <MudStack Spacing="2">
                <MudTextField @bind-Value="Model.Title"
                              Label="عنوان صفحه"
                              Required="true"
                              Variant="Variant.Outlined" />

                <MudTextField @bind-Value="Model.MetaDescription"
                              Label="توضیحات متا (SEO)"
                              Variant="Variant.Outlined"
                              Lines="2" />

                <MudTextField @bind-Value="Model.HeroTitle"
                              Label="عنوان هیرو"
                              Variant="Variant.Outlined" />

                <MudTextField @bind-Value="Model.HeroSubtitle"
                              Label="زیرعنوان هیرو"
                              Variant="Variant.Outlined"
                              Lines="2" />

                <MudText Typo="Typo.subtitle2">تصویر هیرو</MudText>
                <MudStack Justify="Justify.Center" AlignItems="AlignItems.Center">
                    @if (!string.IsNullOrWhiteSpace(_imagePreview))
                    {
                        <Image Src="@_imagePreview" Width="280" Height="160" ObjectPosition="ObjectPosition.Center" ObjectFit="ObjectFit.Cover" />
                    }
                    else if (!string.IsNullOrEmpty(ExistingHeroImagePath))
                    {
                        <Image Src="@ExistingHeroImagePath" Width="280" Height="160" ObjectPosition="ObjectPosition.Center" ObjectFit="ObjectFit.Cover" />
                    }
                    else
                    {
                        <MudPaper Class="d-flex align-center justify-center" Style="width:280px;height:160px;">
                            <MudText Typo="Typo.caption">تصویری انتخاب نشده است</MudText>
                        </MudPaper>
                    }

                    <MudFileUpload T="IBrowserFile" Accept="image/*" FilesChanged="OnImageSelected">
                        <ActivatorContent>
                            <MudButton HtmlTag="label"
                                       Variant="Variant.Filled"
                                       Color="Color.Primary"
                                       ButtonType="ButtonType.Button"
                                       StartIcon="@Icons.Material.Filled.Image">
                                انتخاب تصویر هیرو
                            </MudButton>
                        </ActivatorContent>
                        <SelectedTemplate>
                            @if (context != null)
                            {
                                <MudText Class="mt-1" Typo="Typo.caption">@context.Name</MudText>
                            }
                        </SelectedTemplate>
                    </MudFileUpload>
                </MudStack>

                <MudDivider Class="my-2" />

                <MudText Typo="Typo.subtitle1" Class="mb-1">
                    <MudIcon Icon="@Icons.Material.Filled.Tune" Size="Size.Small" Class="ml-1" />
                    تنظیمات اختصاصی «@PageTitle»
                </MudText>

                @* ── Typed Settings Editors ── *@
                @switch (Model.PageKey)
                {
                    case "landing":
                        <LandingSettingsEditor Model="_landingSettings" />
                        break;
                    case "about":
                        <AboutSettingsEditor Model="_aboutSettings" />
                        break;
                    case "contact":
                        <ContactSettingsEditor Model="_contactSettings" />
                        break;
                    case "licenses":
                        <LicensesSettingsEditor Model="_licensesSettings" />
                        break;
                    default:
                        <MudTextField @bind-Value="Model.SettingsJson"
                                      Label="تنظیمات JSON"
                                      Variant="Variant.Outlined"
                                      Lines="8"
                                      HelperText="تنظیمات اختصاصی صفحه (JSON)" />
                        break;
                }

                <MudStack Row="true" AlignItems="AlignItems.Center" Spacing="1">
                    <MudSwitch T="bool" @bind-Value="Model.IsActive" Color="Color.Primary" />
                    <MudText Typo="Typo.body2">فعال</MudText>
                </MudStack>
            </MudStack>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="SubmitAsync" Disabled="@(!_isValid || _loading)">
            ذخیره تغییرات
        </MudButton>
        <MudButton Color="Color.Default" Variant="Variant.Text" OnClick="Cancel">
            انصراف
        </MudButton>
    </DialogActions>
</MudDialog>
