@using CMSMicroservice.Protobuf.Protos.Package
@using CMSMicroservice.Protobuf.Protos.Configuration
@using BackOffice.Common.BaseComponents
@using Microsoft.AspNetCore.Components.Forms
@using Tizzani.MudBlazor.HtmlEditor


<MudDialog>
    <DialogContent>
        <MudStack>
            <MudStack Justify="Justify.Center" AlignItems="AlignItems.Center">
                <Image Src="@_srcImage" Width="200" Height="100" ObjectPosition="ObjectPosition.Center" ObjectFit="ObjectFit.Cover" />

                <MudFileUpload T="IBrowserFile" Accept="image/*" FilesChanged="OnImageFileSelect">
                    <ActivatorContent>
                        <MudButton HtmlTag="label"
                                   Variant="Variant.Filled"
                                   Color="Color.Primary"
                                   StartIcon="@Icons.Material.Filled.Image"
                                   ButtonType="ButtonType.Button"
                                  >
                            انتخاب تصویر
                        </MudButton>
                    </ActivatorContent>
                    <SelectedTemplate>
                        @if (context != null)
                        {
                            <MudText Class="mt-2" Typo="Typo.subtitle2">
                                <MudTooltip Text="@context.Name" Inline="true">
                                    @context.Name
                                </MudTooltip>
                            </MudText>
                        }
                        else
                        {
                            <MudText Class="mt-2" Typo="Typo.subtitle2">فایلی انتخاب نشده</MudText>
                        }
                    </SelectedTemplate>
                </MudFileUpload>
            </MudStack>
            <MudStack Row="true" AlignItems="AlignItems.Center">
                <MudItem xs="6">
                    <MudTextField T="string" @bind-Value="Model.Title" Disabled="_isLoading" Label="عنوان" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="6">
                    <MudNumericField T="long" HideSpinButtons="true" @bind-Value="Model.Price" Disabled="_isLoading" Label="قیمت (ریال)" Variant="Variant.Outlined" />
                </MudItem>
            </MudStack>

            <MudStack Row="true" AlignItems="AlignItems.Center">
                <MudItem xs="4">
                    <MudNumericField T="int" HideSpinButtons="true" @bind-Value="Model.SortOrder" Disabled="_isLoading" Label="ترتیب نمایش" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="4">
                    <MudNumericField T="long" HideSpinButtons="true" @bind-Value="Model.ActivationFee" Format="N0" Immediate="true" Disabled="_isLoading" Label="هزینه فعال‌سازی (ریال)" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="4">
                    <MudNumericField T="double" HideSpinButtons="true" @bind-Value="Model.DiscountMultiplier" Disabled="_isLoading" Label="ضریب تخفیف" Variant="Variant.Outlined" />
                </MudItem>
            </MudStack>

            <MudStack Row="true" AlignItems="AlignItems.Center">
                <MudItem xs="4">
                    <MudNumericField T="double" HideSpinButtons="true" @bind-Value="Model.MagicWalletMultiplier" Disabled="_isLoading" Label="ضریب کیف پول جادویی" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="4">
                    <MudNumericField T="long" HideSpinButtons="true" @bind-Value="Model.MagicWalletMaxDeposit" Format="N0" Immediate="true" Disabled="_isLoading" Label="سقف واریز جادویی" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="4">
                    <MudNumericField T="long" HideSpinButtons="true" @bind-Value="Model.MagicWalletMaxCredit" Format="N0" Immediate="true" Disabled="_isLoading" Label="سقف اعتبار جادویی" Variant="Variant.Outlined" />
                </MudItem>
            </MudStack>

            <MudStack Row="true" AlignItems="AlignItems.Center">
                <MudItem xs="6">
                    <MudNumericField T="int" HideSpinButtons="true" @bind-Value="Model.MaxBalancesPerLeg" Disabled="_isLoading" Label="حداکثر تعادل هر پا" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="6">
                    <MudNumericField T="int" HideSpinButtons="true" @bind-Value="Model.MaxNetworkLevel" Disabled="_isLoading" Label="حداکثر سطح شبکه" Variant="Variant.Outlined" />
                </MudItem>
            </MudStack>

            <MudStack Row="true" AlignItems="AlignItems.Center">
                <MudItem xs="3">
                    <MudCheckBox T="bool" @bind-Value="Model.IsActive" Disabled="_isLoading" Label="فعال" Color="Color.Success" />
                </MudItem>
                <MudItem xs="3">
                    <MudCheckBox T="bool" @bind-Value="Model.IsBasePackage" Disabled="_isLoading" Label="پکیج پایه" Color="Color.Primary" />
                </MudItem>
                <MudItem xs="3">
                    <MudCheckBox T="bool" @bind-Value="Model.SupportsDirectPurchase" Disabled="_isLoading" Label="پرداخت مستقیم" Color="Color.Info" />
                </MudItem>
                <MudItem xs="3">
                    <MudCheckBox T="bool" @bind-Value="Model.SupportsDayaPurchase" Disabled="_isLoading" Label="اعتبار دایا" Color="Color.Warning" />
                </MudItem>
            </MudStack>

            @if (_features.Any())
            {
                <MudText Typo="Typo.subtitle1" Class="mt-4 mb-2"><strong>ویژگی‌های پکیج</strong></MudText>
                <MudStack Row="true" Wrap="Wrap.Wrap">
                    @foreach (var feature in _features)
                    {
                        <MudCheckBox T="bool"
                                     Value="@(_selectedFeatureIds.Contains(feature.Id))"
                                     ValueChanged="@((bool v) => ToggleFeature(feature.Id, v))"
                                     Disabled="_isLoading"
                                     Label="@feature.Title"
                                     Color="Color.Primary" />
                    }
                </MudStack>
            }

            <MudItem xs="12">
                <MudHtmlEditor @bind-Html="Model.Description" MinHeight="300px">
                    <MudHtmlToolbarOptions InsertImage="false" />
                </MudHtmlEditor>
            </MudItem>
        </MudStack>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel" Disabled="_isLoading">لغو</MudButton>
        <MudButton Color="Color.Primary" OnClick="CallUpdateMethod" Disabled="_isLoading">ثبت</MudButton>
    </DialogActions>
</MudDialog>

