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

@* <style>
    .mud-input-label {
        background-color: var(--mud-palette-background-grey);
    }
</style>  *@
<MudStack class="h-full w-full p-3 md:p-8">
    <MudStack Row="true" class="overflow-x-auto pb-3">
        <MudStack Spacing="1" class="min-w-[340px]">
            <MudStack Row="true" class="mb-5">
                <MudText Typo="Typo.h6" class="my-auto"><b>دسته های اصلی</b></MudText>
                <MudSpacer />
                <MudIconButton Icon="@Icons.Material.Filled.Add"
                               Size="@Size.Small"
                               Variant="@Variant.Outlined"
                               OnClick="()=>ViewModel.ChangeOriginalCategoryVisibility()"
                               Color="@Color.Secondary" />
            </MudStack>
            @if (ViewModel.OriginalCategoryVisibility)
            {
                <MudTextField class="-mt-2 mb-3" T="string" Label="نام دسته بندی" Variant="Variant.Text" Immediate="false" ValueChanged="name =>  ViewModel.AddFastProductCategory(name)"></MudTextField>
            }
            @foreach (var item in ViewModel.PageDto)
            {
                <MudCard class="@(item.IsSelected ? "cursor-pointer border-dashed border-2 border-blue-400 " : "cursor-pointer" )" @onclick="()=>ViewModel.SelectCategory(item)">
                    <MudCardHeader>
                        <CardHeaderContent>
                            <MudText Typo="Typo.body1">@item.Name</MudText>
                        </CardHeaderContent>
                        <CardHeaderActions>
                            <MudStack Row="true" Spacing="1">
                                <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                               Size="@Size.Small"
                                               class="mt-1"
                                               Variant="@Variant.Outlined"
                                               OnClick="async () => await ViewModel.EditProductCategoryClicked(item)"
                                               Color="@Color.Info" />

                                <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                               Size="@Size.Small"
                                               class="mt-1"
                                               Variant="@Variant.Outlined"
                                               OnClick="async () => await ViewModel.DeleteProductCategoryAsync(item.Id)"
                                               Color="@Color.Error" />
                            </MudStack>
                        </CardHeaderActions>
                    </MudCardHeader>
                </MudCard>
            }
        </MudStack>


        @foreach (var item in ViewModel.SelectedCategories)
        {
            <MudStack Spacing="1" class="min-w-[340px]">
                <MudStack Row="true" class="mb-5">
                    <MudText Typo="Typo.h6" class="my-auto"><b>@item.Name</b></MudText>
                    <MudSpacer />
                    <MudIconButton Icon="@Icons.Material.Filled.Add"
                                   Size="@Size.Small"
                                   Variant="@Variant.Outlined"
                                   OnClick="()=>item.AddNewCatVisibility = !item.AddNewCatVisibility"
                                   Color="@Color.Secondary" />
                </MudStack>

                @if (item.AddNewCatVisibility)
                {
                    <MudTextField class="-mt-2 mb-3 flex-none" T="string" Label="نام دسته بندی" Variant="Variant.Text" Immediate="false" ValueChanged="name =>  ViewModel.AddFastProductCategory(name,item.Id)"></MudTextField>
                }
                @foreach (var child in item.Children)
                {
                    <MudCard class="@(child.IsSelected ? "cursor-pointer border-dashed border-2 border-blue-400 " : "cursor-pointer" )" @onclick="()=>ViewModel.SelectCategory(child)">
                        <MudCardHeader>
                            <CardHeaderContent>
                                <MudText Typo="Typo.body1">@child.Name</MudText>
                            </CardHeaderContent>
                            <CardHeaderActions>
                                <MudStack Row="true" Spacing="1">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   class="mt-1"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.EditProductCategoryClicked(child)"
                                                   Color="@Color.Info" />

                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   class="mt-1"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.DeleteProductCategoryAsync(child.Id)"
                                                   Color="@Color.Error" />
                                </MudStack>
                            </CardHeaderActions>
                        </MudCardHeader>
                    </MudCard>
                }
            </MudStack>
        }


    </MudStack>
</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();
    }

}