@page "/products"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]

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

<MudStack class="w-full p-8 h-screen bg-[--mud-palette-background-grey]">
    <MudGrid>
        <MudItem xs="12">
            <MudStack Row="true" class="mb-5">
                <MudText Typo="Typo.h4">محصولاتــــ</MudText>
                <MudChip Color="Color.Info" Variant="Variant.Outlined">124 عدد</MudChip>
                <MudSpacer/>
                <MudButton Variant="Variant.Filled"
                           DisableElevation="true"
                           StartIcon="@Icons.Material.Outlined.Add" 
                           Color="Color.Secondary" 
                           OnClick="@ViewModel.AddProductClicked"
                           class="my-auto">افزودن محصول</MudButton>
                @* <MudButton Variant="Variant.Filled"
                           DisableElevation="true"
                           StartIcon="@Icons.Material.Outlined.Add"
                           Color="Color.Error"
                           OnClick="@ViewModel.AddDigikalaProductClicked"
                           class="my-auto">افزودن محصول از دیجیکالا</MudButton> *@
            </MudStack>
            <MudPaper class="!max-h-[80vh] overflow-auto">
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             
                             T="ProductSDto" Items="@ViewModel.PageDto" CurrentPage="@ViewModel.CurrentPage"
                             RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                             
                             SortMode="@SortMode.None" Groupable="false">

                    <ToolBarContent>
                        <MudGrid Row="true">
                            
                            <MudItem sm="6">
                                <MudTextField T="string" Placeholder="جست جو بر اساس نام" Adornment="Adornment.Start" Immediate="true"
                                              Clearable="true"
                                              ValueChanged="@ViewModel.SearchChanged"
                                              AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" class="my-auto"
                                              OnAdornmentClick="@ViewModel.SearchAsync"></MudTextField>
                            </MudItem>

                            <MudItem sm="6">
                                <MudAutocomplete class="-mt-0.5" Required="true" ToStringFunc="dto => dto.Name"
                                                 T="ProductCategorySDto"
                                                 Label="بر اساس دسته بندی"
                                                 OnClearButtonClick="ViewModel.ClearProductCategorySearch"
                                                 SearchFunc="ViewModel.SearchProductCategory"
                                                 ValueChanged="ViewModel.ProductCategorySelected"
                                                 Clearable="true">
                                    <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>
                        </MudGrid>
                    </ToolBarContent>
                    <Columns>
                        
                        <TemplateColumn T="ProductSDto" Title="نمایش">
                            <CellTemplate>
                                <MudCheckBox ValueChanged="delegate(bool flag) { context.Item.BeDisplayed = flag; ViewModel.DisplayedChanged(context.Item); }" Color="Color.Secondary" Value="@context.Item.BeDisplayed"></MudCheckBox>
                            </CellTemplate>
                        </TemplateColumn>
                        <PropertyColumn Title="نام محصول" Property="arg => arg.PersianName"/>
                        <PropertyColumn Title="دسته بندی" Property="arg => arg.CategoryName"/>
                        <PropertyColumn Title="برند" Property="arg => arg.BrandName" />
                        <TemplateColumn T="ProductSDto" Title="پیشنهاد ویژه است">
                            <CellTemplate>
                                @if (@context.Item.IsSpecial)
                                {
                                    <p>بلی</p>
                                }
                                else
                                {
                                    <p>خیر</p>

                                }
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn T="ProductSDto" Title="قیمتــ">
                            <CellTemplate>
                                <p>@context.Item.Cost.ToString("N0") ریالــ</p>
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Info"
                                                   OnClick="async()=>await ViewModel.EditProductClicked(context.Item)"/>
                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.DeleteProductAsync(context.Item.Id)"
                                                   Color="@Color.Error"/>
                                </MudStack>
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                    <PagerContent>
                        <MudStack Row="true" class="w-full">

                            <MudPagination Rectangular="true" Variant="Variant.Filled" Count="@ViewModel.PageCount"
                                           SelectedChanged="@ViewModel.ChangePageAsync" class="my-4 mx-auto"/>
                        </MudStack>

                    </PagerContent>
                </MudDataGrid>
            </MudPaper>
        </MudItem>
    </MudGrid>
</MudStack>

@code 
{
    public ProductsPageViewModel ViewModel { get; set; }
    protected override async Task OnInitializedAsync()
    {
        ViewModel = new ProductsPageViewModel(NavigationManager, Snackbar, UserUtility, RestWrapper, DialogService);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}
