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

<MudDialog class="mx-auto">
    <DialogContent>
        <MudContainer class="h-full p-0">
            <MudTabs Outlined="true" Elevation="0" Rounded="true" Centered="true">
                <MudTabPanel Text="اطلاعات کلی" Icon="@Icons.Material.Outlined.Info">

                    <MudStack>
                        <MudStack Spacing="0">
                            <MudText Typo="Typo.h6">اطلاعات کلی</MudText>
                            <MudText Typo="Typo.caption">اطلاعات کلی زیر محصول را به دقت وارد کنید</MudText>
                        </MudStack>
                        <MudGrid>

                            <MudItem xs="12" lg="4" md="6">
                                <MudSelect Variant="Variant.Outlined" T="ProductDiversity"
                                           ToStringFunc="type => type.ToDisplay()"
                                           Label="نوع تنوع"
                                           @bind-Value="@ViewModel.PageDto.Diversity">
                                    <MudSelectItem T="ProductDiversity" Value="ProductDiversity.None"></MudSelectItem>
                                    <MudSelectItem T="ProductDiversity" Value="ProductDiversity.Color"></MudSelectItem>
                                    <MudSelectItem T="ProductDiversity" Value="ProductDiversity.Guarantee"></MudSelectItem>
                                    <MudSelectItem T="ProductDiversity" Value="ProductDiversity.Size"></MudSelectItem>
                                </MudSelect>
                            </MudItem>
                            <MudItem xs="12" lg="4" md="6">
                                <MudTextField @bind-Value="@ViewModel.PageDto.DiversityValue" T="string" Label="تنوع" Format="N0" Variant="Variant.Outlined" />
                            </MudItem>

                            <MudItem xs="12" lg="4" md="6">
                                <MudTextField @bind-Value="@ViewModel.PageDto.Cost" T="double" Label="قیمت محصول" Adornment="Adornment.End" Format="N0" AdornmentText="ریالــ" Variant="Variant.Outlined" />
                            </MudItem>
                            <MudItem xs="12" lg="4" md="6">
                                <MudTextField @bind-Value="@ViewModel.PageDto.PackingCost" T="double" Label="مبلغ بسته بندی" Format="N0" Adornment="Adornment.End" AdornmentText="ریالــ" Variant="Variant.Outlined" />
                            </MudItem>
                            <MudItem xs="12" lg="4" md="6">
                                <MudTextField @bind-Value="@ViewModel.PageDto.Stock" T="int" Format="N0" Label="موجودی انبار" Variant="Variant.Outlined" />
                            </MudItem>
                            <MudItem xs="12" lg="4" md="6">
                                <MudTextField @bind-Value="@ViewModel.PageDto.MaxOrderCount" T="int" Format="N0" Label="بیشترین خرید" Variant="Variant.Outlined" />
                            </MudItem>
                            <MudItem xs="12" lg="4" md="6">
                                <MudSelect T="bool" @bind-Value="@ViewModel.PageDto.HasExpressDelivery" Label="آیا ارسال سریع دارد ؟" ToStringFunc="b=>b.ToPersianString()" Variant="Variant.Outlined" AnchorOrigin="Origin.BottomCenter">
                                    <MudSelectItem T="bool" Value="true" />
                                    <MudSelectItem T="bool" Value="false" />
                                </MudSelect>
                            </MudItem>
                        </MudGrid>
                    </MudStack>
                </MudTabPanel>
            </MudTabs>
        </MudContainer>
    </DialogContent>
    <DialogActions>
        <MudStack Row="true" class="w-full mx-4 mb-2">

            @if (ViewModel.IsEditing)
            {
                <BaseButtonUi class="w-64 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Check"
                              Variant="Variant.Filled" Color="Color.Success"
                              Content="ثبت ویرایش" OnClickCallback="ViewModel.SubmitEditAsync" />
            }
            else
            {
                <BaseButtonUi class="w-64 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Check"
                              Variant="Variant.Filled" Color="Color.Success"
                              Content="تایید" OnClickCallback="ViewModel.SubmitCreateAsync" />
            }
            <MudSpacer />
            <MudButton Variant="Variant.Outlined" Size="Size.Large" Color="Color.Error" OnClick="ViewModel.Cancel">بستن</MudButton>
        </MudStack>
    </DialogActions>
</MudDialog>
@code {

    [CascadingParameter]
    MudDialogInstance MudDialog { get; set; }

    [Parameter]
    public SubProductSDto? SubProduct { get; set; }

    [Parameter]
    public string ProductName { get; set; } = string.Empty;

    [Parameter]
    public Guid ProductId { get; set; }


    public SubProductActionDialogBoxViewModel ViewModel { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (ProductId == default || string.IsNullOrEmpty(ProductName))
            return;

        ViewModel = SubProduct == null ? new SubProductActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, ProductId, ProductName) :
            new SubProductActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, ProductId, ProductName, SubProduct);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}