namespace Netina.AdminPanel.PWA.Pages; public class OrdersPageViewModel : BaseViewModel { private readonly NavigationManager _navigationManager; private readonly ISnackbar _snackbar; private readonly IUserUtility _userUtility; private readonly IDialogService _dialogService; private readonly IBrowserViewportService _browserViewportService; private readonly IRestWrapper _restWrapper; public string Search = string.Empty; public int MainGridCurrentPage = 0; public int MainGridPageCount = 1; public ObservableCollection MainOrders { get; } = new ObservableCollection(); public OrdersPageViewModel(NavigationManager navigationManager, ISnackbar snackbar, IUserUtility userUtility, IRestWrapper restWrapper, IDialogService dialogService, IBrowserViewportService browserViewportService) : base(userUtility) { _navigationManager = navigationManager; _snackbar = snackbar; _userUtility = userUtility; _restWrapper = restWrapper; _dialogService = dialogService; _browserViewportService = browserViewportService; } public override async Task InitializeAsync() { await base.InitializeAsync(); if (!IsPermitted) return; try { var token = await _userUtility.GetBearerTokenAsync(); if (token == null) throw new Exception("Token is null"); IsProcessing = true; MainOrders.Clear(); var dto = await _restWrapper.OrderRestApi.ReadAll(MainGridCurrentPage, token); dto.ForEach(d => MainOrders.Add(d)); if (MainOrders.Count == 15) MainGridPageCount = 2; var orderDashboardDto = await _restWrapper.DashboardApiRest.GetOrdersDashboardAsync(token); PageDto = orderDashboardDto; } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); if (ex.StatusCode == HttpStatusCode.Unauthorized) { await _userUtility.LogoutAsync(); _navigationManager.NavigateTo("login", true, true); } _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 ChangePageAsync(int page) { MainGridCurrentPage = page - 1; if (MainGridCurrentPage > MainGridPageCount - 2) { try { IsProcessing = true; var token = await _userUtility.GetBearerTokenAsync(); if (token == null) throw new Exception("Token is null"); var dto = await _restWrapper.OrderRestApi.ReadAll(MainGridCurrentPage, FactorCodeSearch, null, OrderStatusSearch?.Value, null, token); dto.ForEach(d => MainOrders.Add(d)); if (MainOrders.Count == 15) MainGridPageCount = MainGridCurrentPage + 2; } 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 ShowClicked(OrderSDto order) { DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.False, NoHeader = true, FullWidth = true, DisableBackdropClick = true }; var breakPoint = await _browserViewportService.GetCurrentBreakpointAsync(); if (breakPoint == Breakpoint.Xs) maxWidth = new DialogOptions { FullScreen = true, CloseButton = true }; var parameters = new DialogParameters(); parameters.Add(x => x.Order, order); var dialogResult = await _dialogService.ShowAsync($" سفارش {order.FactorCode}", parameters, maxWidth); var result = await dialogResult.Result; if (!result.Canceled && result.Data is bool and true) { await InitializeAsync(); } } public string? FactorCodeSearch { get; set; } = null; public async Task SearchAsync() { try { var token = await _userUtility.GetBearerTokenAsync(); if (token == null) throw new Exception("Token is null"); IsProcessing = true; MainOrders.Clear(); List dto; dto = await _restWrapper.OrderRestApi.ReadAll(MainGridCurrentPage, FactorCodeSearch, null, OrderStatusSearch?.Value, null, token); dto.ForEach(d => MainOrders.Add(d)); if (MainOrders.Count == 15) MainGridPageCount = 2; var orderDashboardDto = await _restWrapper.DashboardApiRest.GetOrdersDashboardAsync(token); PageDto = orderDashboardDto; } catch (ApiException ex) { var exe = await ex.GetContentAsAsync(); if (ex.StatusCode == HttpStatusCode.Unauthorized) { await _userUtility.LogoutAsync(); _navigationManager.NavigateTo("login", true, true); } _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); } catch (Exception e) { _snackbar.Add(e.Message, Severity.Error); } finally { IsProcessing = false; } } public FilterOptionDto? OrderStatusSearch; public List> OrderStatusFilterOptions = new List> { new FilterOptionDto{Title = OrderStatus.Paid.ToDisplay(),Value = OrderStatus.Paid}, new FilterOptionDto{Title = OrderStatus.Submitted.ToDisplay(),Value = OrderStatus.Submitted}, new FilterOptionDto{Title = OrderStatus.Delivered.ToDisplay(),Value = OrderStatus.Delivered}, new FilterOptionDto{Title = OrderStatus.Done.ToDisplay(),Value = OrderStatus.Done}, new FilterOptionDto{Title = OrderStatus.Canceled.ToDisplay(),Value = OrderStatus.Canceled}, }; public async Task>> OrderStatusSearchAsync(string orderStatus) { if (orderStatus.IsNullOrEmpty()) return OrderStatusFilterOptions; return OrderStatusFilterOptions.Where(o => o.Title == orderStatus).ToList(); } public async Task SearchByOrderStatusAsync(FilterOptionDto? arg) { OrderStatusSearch = arg; await SearchAsync(); } }