@using Color = MudBlazor.Color
@using Size = MudBlazor.Size

@inject ISnackbar Snackbar
@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
@inject IDialogService DialogService
@inject IPrintingService PrintingService
@inject IJSRuntime JsRuntime
@inject IConfiguration Configuration

<MudDialog class="w-full overflow-y-scroll p-0">
    <DialogContent>
        <MudStack class="mt-2 h-full w-full p-0">

            <MudGrid class="w-full" Spacing="0">
                <MudItem xs="12" md="9">

                    <MudStack Row="true">

                        <MudText Typo="Typo.h6" class="my-auto"><b>اطلاعات سفارش</b></MudText>
                        <MudSpacer />
                        <MudPaper Elevation="0" class="my-auto bg-blue-500 px-4 py-2">
                            <MudText Typo="Typo.body2" class="text-white">@ViewModel.PageDto.OrderStatus.ToDisplay()</MudText>
                        </MudPaper>
                    </MudStack>

                    <MudGrid>
                        <MudItem xs="12" sm="4">
                            <MudField Variant="Variant.Outlined" Label="نام و نام خانوادگی">@ViewModel.PageDto.CustomerFullName</MudField>
                        </MudItem>

                        <MudItem xs="12" sm="4">
                            <MudField Variant="Variant.Outlined" Label="شماره تماس">@ViewModel.PageDto.CustomerPhoneNumber</MudField>
                        </MudItem>

                        <MudItem xs="12" sm="4">
                            <MudField Variant="Variant.Outlined" Label="کد تخفیف">@ViewModel.PageDto.DiscountCode</MudField>
                        </MudItem>
                        <MudItem xs="12" sm="6">
                            <MudField Variant="Variant.Outlined" Label="آدرس">@ViewModel.PageDto?.OrderDelivery?.Address</MudField>
                        </MudItem>

                        <MudItem xs="12" sm="3">
                            <MudField Variant="Variant.Outlined" Label="روش ارسال">@ViewModel.PageDto?.OrderDelivery?.ShippingMethod</MudField>
                        </MudItem>
                        <MudItem xs="12" sm="3">
                            <MudField Variant="Variant.Outlined" Label="کدرهگیری ارسال">@ViewModel.PageDto?.OrderDelivery?.TrackingCode</MudField>
                        </MudItem>
                    </MudGrid>

                    <MudStack class="mt-3" Row="true">

                        <MudText Typo="Typo.h6"><b>کالاهای این سفارش</b></MudText>
                        <MudSpacer />
                        <MudPaper Elevation="0" class="border-2 my-auto border-dashed border-violet-400 px-3 py-1">
                            <MudText Typo="Typo.caption" class="text-violet-500"><b>@ViewModel.PageDto.OrderProducts.Sum(op => op.Count) قلم کالا</b></MudText>
                        </MudPaper>
                    </MudStack>

                    <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                                 Elevation="0"
                                 Bordered="true"
                                 T="OrderProductSDto" Items="@ViewModel.PageDto.OrderProducts"
                                 Filterable="false" Loading="@ViewModel.IsProcessing"
                                 SortMode="@SortMode.None" Groupable="false"
                                 class="border-2 border-solid border-gray-200">
                        <Columns>

                            <TemplateColumn T="OrderProductSDto" Title="نام کالا">

                                <CellTemplate>
                                    <b>@context.Item.ProductName</b>
                                </CellTemplate>
                            </TemplateColumn>

                            <TemplateColumn T="OrderProductSDto" Title="قیمت کالا">

                                <CellTemplate>
                                    <p>@context.Item.ProductFeeWithDiscount.ToString("N0") ریالــ</p>
                                </CellTemplate>
                            </TemplateColumn>
                            <PropertyColumn Title="تعداد" Property="arg => arg.Count" />

                            <TemplateColumn T="OrderProductSDto" Title="قیمت کل">

                                <CellTemplate>
                                    <p>@context.Item.ProductCost.ToString("N0") ریالــ</p>
                                </CellTemplate>
                            </TemplateColumn>
                        </Columns>
                    </MudDataGrid>

                </MudItem>

                <MudItem xs="12" md="3">

                    <MudStack class="sm:mr-3">
                        <MudText Typo="Typo.h6" class="mb-2"><b>مبــــالغ</b></MudText>
                        <MudPaper class="bg-[--mud-palette-background-grey] rounded-md">
                            <MudStack class="p-3">

                                <MudStack Row="true">
                                    <MudText class="my-auto" Typo="Typo.body2">جمع کالاها : </MudText>
                                    <MudSpacer />
                                    <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.TotalProductsPrice.ToString("N0")</b> ریالــ</MudText>
                                </MudStack>

                                <MudDivider />

                                <MudStack Row="true">
                                    <MudText class="my-auto" Typo="Typo.body2">مالیات : </MudText>
                                    <MudSpacer />
                                    <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.TaxesPrice.ToString("N0")</b> ریالــ</MudText>
                                </MudStack>

                                <MudDivider />

                                <MudStack Row="true">
                                    <MudText class="my-auto" Typo="Typo.body2">بسته بندی : </MudText>
                                    <MudSpacer />
                                    <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.PackingPrice.ToString("N0")</b> ریالــ</MudText>
                                </MudStack>

                                <MudDivider />

                                <MudStack Row="true">
                                    <MudText class="my-auto" Typo="Typo.body2">هزینه ارسال : </MudText>
                                    <MudSpacer />
                                    <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.DeliveryPrice.ToString("N0")</b> ریالــ</MudText>
                                </MudStack>

                            </MudStack>

                            <MudPaper class="mud-theme-dark rounded-md">

                                <MudStack class="p-3">

                                    <MudStack Row="true">
                                        <MudText class="my-auto" Typo="Typo.body2">جمع کل : </MudText>
                                        <MudSpacer />
                                        <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.TotalPriceWithoutDiscount.ToString("N0")</b> ریالــ</MudText>
                                    </MudStack>

                                    <MudDivider />

                                    <MudStack Row="true">
                                        <MudText class="my-auto" Typo="Typo.body2">مقدار تخفیف : </MudText>
                                        <MudSpacer />
                                        <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.DiscountPrice.ToString("N0")</b> ریالــ</MudText>
                                    </MudStack>

                                    <MudDivider />

                                    <MudStack Row="true">
                                        <MudText class="my-auto" Typo="Typo.body2">مبلغ قابل پرداخت : </MudText>
                                        <MudSpacer />
                                        <MudText Typo="Typo.body1"><b>@ViewModel.PageDto.TotalPrice.ToString("N0")</b> ریالــ</MudText>
                                    </MudStack>

                                </MudStack>
                            </MudPaper>
                        </MudPaper>

                        <MudText Typo="Typo.h6" class="mt-3"><b>پرداختــ ها</b></MudText>

                        <MudPaper Elevation="0" class="bg-[--mud-palette-background-grey] rounded-md">
                            <MudStack class="p-3">

                                @foreach (var item in ViewModel.PageDto.Payments)
                                {
                                    <MudStack Row="true">
                                        <MudText class="my-auto" Typo="Typo.body2">@item.Type.ToDisplay()</MudText>
                                        <MudText class="my-auto" Typo="Typo.body2"> - </MudText>
                                        <MudText class="my-auto" Typo="Typo.body2">@item.Status.ToDisplay()</MudText>
                                        <MudSpacer />
                                        <MudText Typo="Typo.body1"><b>@item.Amount.ToString("N0")</b> ریالــ</MudText>
                                    </MudStack>
                                }

                            </MudStack>
                        </MudPaper>
                    </MudStack>
                </MudItem>
            </MudGrid>
        </MudStack>
    </DialogContent>
    <DialogActions>
        <MudGrid class="bottom-0 mx-4 mb-3 h-fit w-full">

            <MudItem xs="12" sm="6" md="6">
                <BaseButtonUi class="h-12 w-full rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.AirportShuttle"
                              Variant="Variant.Filled" Color="Color.Success"
                              Content="@ViewModel.ConfirmOrderButtonText"
                              OnClickCallback="ViewModel.SubmitConfirmAsync" />
            </MudItem>
            <MudItem xs="12" sm="6" md="2">
                <BaseButtonUi class="h-12 w-full rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.RemoveCircle"
                              Variant="Variant.Outlined" Color="Color.Error"
                              OnClickCallback="ViewModel.CancelAsync"
                              Content="لغو سفارش" />
            </MudItem>

            <MudItem xs="12" sm="6" md="3">
                <BaseButtonUi class="my-auto h-12 w-full rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Print"
                              Size="Size.Small"
                              Variant="Variant.Outlined" Color="Color.Info"
                              OnClickCallback="@ViewModel.PrintInvoiceAsync"
                              Content="چاپــ فاکتور" />
            </MudItem>
            <MudItem xs="12" sm="6" md="1">
                <BaseButtonUi class="my-auto h-12 w-full rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Size="Size.Small"
                              Variant="Variant.Outlined" Color="Color.Error"
                              OnClickCallback="@ViewModel.Cancel"
                              Content="بستن" />
            </MudItem>
        </MudGrid>
    </DialogActions>
</MudDialog>

@code {

    [CascadingParameter]
    MudDialogInstance MudDialog { get; set; }

    [Parameter]
    public OrderSDto? Order { get; set; }

    public OrderActionDialogBoxViewModel ViewModel { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (Order == null)
            ViewModel = new OrderActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, PrintingService, JsRuntime,Configuration);
        else
            ViewModel = new OrderActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, Order, PrintingService, JsRuntime,Configuration);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}
