@using Radzen.Blazor
@page "/product/categories"
@inject IDialogService DialogService
@inject NavigationManager NavigationManager
@inject IRestWrapper RestWrapper
@inject ISnackbar Snackbar
@inject IUserUtility UserUtility


<MudStack class="w-full p-8 h-screen bg-[--color-background]">
    <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.AddProductCategoryClicked"
                           class="my-auto">افزودن دسته بندی</MudButton>
            </MudStack>
            <MudPaper>
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="ProductCategorySDto" Items="@ViewModel.PageDto" CurrentPage="@ViewModel.CurrentPage" 
                             RowsPerPage="15" Filterable="false" Loading="@ViewModel.IsProcessing"
                             SortMode="@SortMode.None" Groupable="false">
                    <ToolBarContent>
                        <MudTextField T="string" Placeholder="جست جو بر اساس نام" Adornment="Adornment.Start" Immediate="true"
                                      AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" class="my-auto" 
                                      @bind-Value="@ViewModel.Search"
                                      OnAdornmentClick="@ViewModel.SearchAsync"></MudTextField>
                    </ToolBarContent>
                    
                    <Columns>
                        <HierarchyColumn T="ProductCategorySDto" ButtonDisabledFunc="@(x => x.Description.IsNullOrEmpty())" />
                        <PropertyColumn Title="نام دسته" Resizable="false"  Property="arg => arg.Name"/>
                        @* <TemplateColumn Title="توضیحاتــ" Resizable="false" T="ProductCategorySDto">
                            <CellTemplate>
                                <p class="line-clamp-1">@context.Item.Description</p>
                            </CellTemplate>
                        </TemplateColumn> *@
                        <TemplateColumn Resizable="false" Title="دسته اصلی" T="ProductCategorySDto">
                            <CellTemplate>
                                @if (@context.Item.IsMain)
                                {
                                    <p>بلی</p>
                                }
                                else
                                {
                                    <p>خیر</p>

                                }
                            </CellTemplate>
                        </TemplateColumn>
                        <PropertyColumn Title="دسته پدر" Resizable="false" Property="arg => arg.ParentName" />
                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.EditProductCategoryClicked(context.Item)"
                                                   Color="@Color.Info"/>

                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.DeleteProductCategoryAsync(context.Item.Id)"
                                                   Color="@Color.Error"/>
                                </MudStack>
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                    
                    <ChildRowContent>
                        <MudCard>
                            <MudCardHeader>
                                <CardHeaderContent>
                                    <MudText Typo="Typo.h6">توضیحاتــ</MudText>
                                </CardHeaderContent>
                            </MudCardHeader>
                            <MudCardContent>
                                <MudText>@context.Item.Description</MudText>
                            </MudCardContent>
                        </MudCard>
                    </ChildRowContent>
                    <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 CategoriesPageViewModel ViewModel { get; set; }
    protected override async Task OnInitializedAsync()
    {
        ViewModel = new CategoriesPageViewModel(NavigationManager, Snackbar, UserUtility, RestWrapper, DialogService);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }

}
