@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

<MudDialog class="w-full">
    <DialogContent>
        <MudStack class="max-h-[50rem] w-full mt-4" Style="overflow-y: scroll">
            <MudGrid class="w-full">
                <MudItem xs="12" md="9">
                    <MudStack>
                        
                        <MudStack Row="true">
                            
                            <MudText Typo="Typo.h6" class="my-auto"><b>اطلاعات سفارش</b></MudText>
                            <MudSpacer/>
                            <MudPaper Elevation="0" class="bg-blue-500 px-4 py-2 my-auto">
                                <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.UserFullName</MudField>
                            </MudItem>

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

                            <MudItem xs="12" sm="4">
                                <MudField Variant="Variant.Outlined" Label="کد تخفیف">@ViewModel.PageDto.DiscountCode</MudField>
                            </MudItem>


                            <MudItem xs="12" sm="8">
                                <MudField Variant="Variant.Outlined" Label="آدرس">@ViewModel.PageDto?.OrderDelivery?.Address</MudField>
                            </MudItem>
                                <MudItem xs="12" sm="4">
                                <MudField Variant="Variant.Outlined" Label="روش ارسال" >@ViewModel.PageDto?.OrderDelivery?.ShippingMethod</MudField>
                            </MudItem>
                        </MudGrid>
                        
                        <MudStack class="mt-3" Row="true">
                            
                                <MudText Typo="Typo.h6"><b>کالاهای این سفارش</b></MudText>
                                <MudSpacer/>
                            <MudPaper Elevation="0" class="border-dashed border-violet-400 border-2 px-3 py-1 my-auto">
                                    <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-solid border-2 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>

                    </MudStack>
                </MudItem>

                <MudItem xs="12" md="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>
                </MudItem>
            </MudGrid>
        </MudStack>
    </DialogContent>
    <DialogActions>
        <MudStack Row="true" class="w-full h-fit mx-4 mb-3 bottom-0">
            
            <BaseButtonUi class="w-64 h-12 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                          Icon="@Icons.Material.Outlined.AirportShuttle"
                          Variant="Variant.Filled" Color="Color.Success"
                          Content="@ViewModel.ConfirmOrderButtonText" OnClickCallback="ViewModel.SubmitConfirmAsync" />
            <BaseButtonUi class="w-52 h-12 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                          Icon="@Icons.Material.Outlined.RemoveCircle"
                          Variant="Variant.Outlined" Color="Color.Error"
                          Content="لغو سفارش" />
            <MudSpacer />

            <BaseButtonUi class="w-52 h-12 rounded-md my-auto" IsProcessing="@ViewModel.IsProcessing"
                          Icon="@Icons.Material.Outlined.Print"
                          Size="Size.Small"
                          Variant="Variant.Outlined" Color="Color.Info"
                          OnClickCallback="@ViewModel.PrintInvoiceAsync"
                          Content="چاپــ فاکتور" />
            <MudButton Variant="Variant.Outlined" Size="Size.Medium" Color="Color.Error" OnClick="ViewModel.Cancel">بستن</MudButton>
        </MudStack>
    </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);
        else
            ViewModel = new OrderActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, Order , PrintingService , JsRuntime);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}
