namespace Netina.AdminPanel.PWA.Pages; public class CategoriesPageViewModel : BaseViewModel> { private readonly NavigationManager _navigationManager; private readonly ISnackbar _snackbar; private readonly IUserUtility _userUtility; private readonly IDialogService _dialogService; private readonly IRestWrapper _restWrapper; public string Search = string.Empty; public CategoriesPageViewModel(NavigationManager navigationManager, ISnackbar snackbar, IUserUtility userUtility, IRestWrapper restWrapper, IDialogService dialogService) : base(userUtility) { _navigationManager = navigationManager; _snackbar = snackbar; _userUtility = userUtility; _restWrapper = restWrapper; _dialogService = dialogService; } public override async Task InitializeAsync() { try { IsProcessing = true; PageDto.Clear(); var dto = await _restWrapper.ProductCategoryRestApi.ReadAll(true); dto.ForEach(d => PageDto.Add(d)); } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); } catch (Exception e) { _snackbar.Add(e.Message, Severity.Error); } finally { IsProcessing = false; } await base.InitializeAsync(); } public async Task AddProductCategoryClicked() { DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true }; var dialogResult = await _dialogService.ShowAsync("افزودن دسته جدید", maxWidth); var result = await dialogResult.Result; if (!result.Canceled && result.Data is bool and true) { await InitializeAsync(); } } public async Task EditProductCategoryClicked(ProductCategorySDto category) { DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true }; var parameters = new DialogParameters(); parameters.Add(x => x.Category, category); var dialogResult = await _dialogService.ShowAsync($"ویرایش دسته {category.Name}", parameters, maxWidth); var result = await dialogResult.Result; if (!result.Canceled && result.Data is bool and true) { await InitializeAsync(); } } public async Task DeleteProductCategoryAsync(Guid selectedCategoryId) { var reference = await _dialogService.ShowQuestionDialog($"آیا از حذف دسته بندی اطمینان دارید ?"); var result = await reference.Result; if (!result.Canceled) { try { IsProcessing = true; var token = await _userUtility.GetBearerTokenAsync(); await _restWrapper.CrudDtoApiRest(Address.ProductCategoryController) .Delete(selectedCategoryId, token); _snackbar.Add("حذف دسته بندی با موفقیت انجام شد", Severity.Success); await InitializeAsync(); } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); } catch (Exception e) { _snackbar.Add(e.Message, Severity.Error); } finally { IsProcessing = false; } } } public async Task SearchAsync() { try { if (Search.IsNullOrEmpty()) throw new AppException("دسته بندی برای جست جو وارد نشده است"); IsProcessing = true; PageDto.Clear(); var dto = await _restWrapper.ProductCategoryRestApi.ReadAll(Search); dto.ForEach(d => PageDto.Add(d)); } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); } catch (Exception e) { _snackbar.Add(e.Message, Severity.Error); } finally { IsProcessing = false; } } public List SelectedCategories = new(); public void SelectCategory(ProductCategorySDto category) { var selected = SelectedCategories.FirstOrDefault(c => c.Id == category.Id); if (selected != null) { UnSelectCategory(selected); return; } var parent = FindParent(category.ParentId, PageDto.ToList()); if (parent != null) category.Index = parent.Index + 1; category.IsSelected = true; var currentSelectedIndex = SelectedCategories.FirstOrDefault(c => c.Index == category.Index); if (currentSelectedIndex != null) UnSelectCategory(currentSelectedIndex); SelectedCategories.Add(category); } public bool OriginalCategoryVisibility = false; public bool ChangeOriginalCategoryVisibility() => OriginalCategoryVisibility = !OriginalCategoryVisibility; public void AddFastProductCategory(string categoryName, Guid? parentId = null) { if(categoryName.IsNullOrEmpty()) return; ProductCategorySDto category = new ProductCategorySDto { Name = categoryName, IsMain = true}; if (parentId != null) { category.IsMain = false; category.ParentId = parentId.Value; } var command = category.Adapt() with{Files = new()}; Task.Run(async () => { var token = await _userUtility.GetBearerTokenAsync(); if (token == null) return; var response = await _restWrapper.CrudApiRest(Address.ProductCategoryController) .Create(command, token); category.Id = response; }); if (parentId == null) { category.IsMain = true; PageDto.Add(category); } else { var parent = FindParent(parentId.Value, PageDto.ToList()); if (parent != null) { category.Index = parent.Index + 1; parent.Children.Add(category); } } categoryName = string.Empty; } private void UnSelectCategory(ProductCategorySDto category) { category.IsSelected = false; foreach (var selected in SelectedCategories.Where(c => c.ParentId == category.Id).ToList()) UnSelectCategory(selected); SelectedCategories.Remove(category); } private ProductCategorySDto? FindParent(Guid parentId, List categories) { var parent = categories.FirstOrDefault(c => c.Id == parentId); if (parent != null) return parent; foreach (var category in categories) { var founded = FindParent(parentId, category.Children); if (founded != null) return founded; } return null; } }