@using Netina.AdminPanel.PWA.Extensions
@inject ISnackbar Snackbar
@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
@inject IDialogService DialogService

<MudDialog class="mx-auto" DisableSidePadding="true">
    <DialogContent >
        <MudContainer class="max-h-[30rem] lg:max-h-[35rem] overflow-y-scroll">

            <MudStack>
                <MudStack Spacing="0">

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

                    <MudItem sm="8">
                        <MudTextField Disabled="@ViewModel.PageDto.HasCode.Not()" @bind-Value="@ViewModel.PageDto.Code" T="string" Label="کد تخفیف" Variant="Variant.Outlined" />
                    </MudItem>

                    <MudItem sm="4">
                        <MudSwitch @bind-Value="@ViewModel.PageDto.HasCode" class="mt-2" Size="Size.Large" T="bool" Label="با کد تخفیف باشد" Color="Color.Info" />
                    </MudItem>

                    <MudItem sm="8">
                        <MudTextField @bind-Value="@ViewModel.PageDto.Description" T="string" Label="توضیحاتــ" Variant="Variant.Outlined" />
                    </MudItem>


                    <MudItem sm="4">
                        <MudSwitch @bind-Value="@ViewModel.PageDto.IsForFirstPurchase" class="mt-2" Size="Size.Large" T="bool" Label="مخصوص اولین خرید" Color="Color.Info" />
                    </MudItem>

                    <MudItem sm="8">
                        <MudTextField Disabled="@ViewModel.PageDto.IsInfinity" Format="N0" @bind-Value="@ViewModel.PageDto.Count" T="int" Label="تعداد قابل استفاده" Variant="Variant.Outlined" />
                    </MudItem>

                    <MudItem sm="4">
                        <MudSwitch @bind-Value="@ViewModel.PageDto.IsInfinity" class="mt-2" Size="Size.Large" T="bool" Label="نامحدود باشد ؟" Color="Color.Info" />
                    </MudItem>

                    <MudItem md="4">

                        <MudSelect T="DiscountAmountType" ValueChanged="@ViewModel.AmountTypeChanged" Label="نوع تخفیفـــ" ToStringFunc="b=>b.ToDisplay()" Variant="Variant.Outlined" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem T="DiscountAmountType" Value="DiscountAmountType.Percent" />
                            <MudSelectItem T="DiscountAmountType" Value="DiscountAmountType.Amount" />
                        </MudSelect>
                    </MudItem>

                    <MudItem md="4">
                        <MudTextField Disabled="@ViewModel.IsPercentType.Not()" Format="N0" @bind-Value="@ViewModel.PageDto.DiscountPercent" T="int" Label="درصد تخفیف" Variant="Variant.Outlined" />
                    </MudItem>

                    <MudItem md="4">
                        <MudTextField Disabled="@ViewModel.IsAmountType.Not()" Format="N0" @bind-Value="@ViewModel.PageDto.DiscountAmount" T="long" Label="مبلغ تخفیفــ" Adornment="Adornment.End" AdornmentText="ریالــ" Variant="Variant.Outlined" />
                    </MudItem>


                    <MudItem md="6">
                        <MudTextField @bind-Value="@ViewModel.PageDto.PriceFloor" Format="N0" T="long" Label="کف قیمت برای تخفیف" Adornment="Adornment.End" AdornmentText="ریالــ" Variant="Variant.Outlined" />
                    </MudItem>


                    <MudItem md="6">
                        <MudTextField @bind-Value="@ViewModel.PageDto.PriceCeiling" Format="N0" T="long" Label="سقف قیمت برای تخفیف" Adornment="Adornment.End" AdornmentText="ریالــ" Variant="Variant.Outlined" />
                    </MudItem>

                    <MudItem sm="6">
                        <MudDatePicker @bind-Date="@ViewModel.StartDate" UseShortNames="false" TitleDateFormat="dddd, dd MMMM" Label="تاریخ شروع تخفیفــ" Variant="Variant.Outlined" Culture="@PersianCultureInfo.GetPersianCulture()" />

                    </MudItem>
                    <MudItem sm="6">
                        <MudDatePicker @bind-Date="@ViewModel.ExpireDate" UseShortNames="false" TitleDateFormat="dddd, dd MMMM" Label="تاریخ پایان تخفیفــ" Variant="Variant.Outlined" Culture="@PersianCultureInfo.GetPersianCulture()" />

                    </MudItem>

                </MudGrid>

                <MudStack class="mt-4" Spacing="0">

                    <MudText Typo="Typo.h6">انتخاب حالتــ تخفیفـــ</MudText>
                    <MudText Typo="Typo.caption">تخفیف مورد نظر میتواند برای هر یک از موارد زیر ایجاد شود</MudText>
                </MudStack>
                <MudGrid>

                    <MudItem sm="7">

                        <MudAutocomplete Disabled="@ViewModel.IsCategoryEnable.Not()" ToStringFunc="dto => dto.Name" @bind-Value="@ViewModel.SelectedCategory"
                                         SearchFunc="ViewModel.SearchCategory"
                                         T="ProductCategorySDto"
                                         Label="دسته بندی"
                                         Variant="Variant.Outlined">
                            <ProgressIndicatorInPopoverTemplate>
                                <MudList Clickable="false">
                                    <MudListItem>
                                        <div class="flex flex-row w-full mx-auto">
                                            <MudProgressCircular class="my-auto mr-1 -ml-4" Size="Size.Small" Indeterminate="true" />
                                            <p class="font-bold my-1 mx-auto text-md">منتظر بمانید</p>
                                        </div>
                                    </MudListItem>
                                </MudList>
                            </ProgressIndicatorInPopoverTemplate>
                            <ItemTemplate Context="e">
                                <p>@e.Name</p>
                            </ItemTemplate>
                        </MudAutocomplete>
                    </MudItem>
                    <MudItem sm="5">
                        <MudSwitch @bind-Value="@ViewModel.IsCategoryEnable" class="mt-2" Size="Size.Large" T="bool" Label="تخفیف برای دسته خاص" Color="Color.Info" />
                    </MudItem>
                    <MudItem sm="7">

                        <MudAutocomplete Disabled="@ViewModel.IsProductEnable.Not()" ToStringFunc="dto => dto.PersianName" @bind-Value="@ViewModel.SelectedProduct"
                                         SearchFunc="ViewModel.SearchProduct"
                                         T="ProductSDto"
                                         Label="کــــالا"
                                         Variant="Variant.Outlined">
                            <ProgressIndicatorInPopoverTemplate>
                                <MudList Clickable="false">
                                    <MudListItem>
                                        <div class="flex flex-row w-full mx-auto">
                                            <MudProgressCircular class="my-auto mr-1 -ml-4" Size="Size.Small" Indeterminate="true" />
                                            <p class="font-bold my-1 mx-auto text-md">منتظر بمانید</p>
                                        </div>
                                    </MudListItem>
                                </MudList>
                            </ProgressIndicatorInPopoverTemplate>
                            <ItemTemplate Context="e">
                                <p>@e.PersianName</p>
                            </ItemTemplate>
                        </MudAutocomplete>
                    </MudItem>
                    <MudItem sm="5">
                        <MudSwitch @bind-Value="@ViewModel.IsProductEnable" class="mt-2" Size="Size.Large" T="bool" Label="تخفیف برای محصول خاص" Color="Color.Info" />
                    </MudItem>

                    <MudItem sm="7">
                    </MudItem>
                    <MudItem sm="5">
                        <MudSwitch @bind-Value="@ViewModel.IsAllEnable" class="mt-1.5 mx-auto" Size="Size.Large" T="bool" Label="تخفیف برای تمامی کالاهای " Color="Color.Info" />
                    </MudItem>
                </MudGrid>
            </MudStack>
        </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 DiscountSDto? Discount { get; set; }

    public DiscountActionDialogBoxViewModel ViewModel { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (Discount == null)
            ViewModel = new DiscountActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog);
        else
            ViewModel = new DiscountActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, Discount);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }

}