AdminPanel/NetinaShop.AdminPanel.PWA/Dialogs/OrderActionDialogBox.razor.cs

163 lines
5.7 KiB
C#

namespace NetinaShop.AdminPanel.PWA.Dialogs;
public class OrderActionDialogBoxViewModel : BaseViewModel<OrderLDto>
{
private readonly ISnackbar _snackbar;
private readonly IRestWrapper _restWrapper;
private readonly IUserUtility _userUtility;
private readonly IDialogService _dialogService;
private readonly MudDialogInstance _mudDialog;
public OrderActionDialogBoxViewModel(ISnackbar snackbar, IRestWrapper restWrapper, IUserUtility userUtility, IDialogService dialogService, MudDialogInstance mudDialog)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
}
public OrderActionDialogBoxViewModel(ISnackbar snackbar,
IRestWrapper restWrapper,
IUserUtility userUtility,
IDialogService dialogService,
MudDialogInstance mudDialog,
OrderSDto order)
{
_snackbar = snackbar;
_restWrapper = restWrapper;
_userUtility = userUtility;
_dialogService = dialogService;
_mudDialog = mudDialog;
PageDto.Id = order.Id;
IsEditing = true;
}
public void Cancel() => _mudDialog.Cancel();
public string ConfirmOrderButtonText { get; set; } = string.Empty;
public bool IsEditing = false;
public override async Task InitializeAsync()
{
if (IsEditing && PageDto.Id != default)
{
try
{
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
IsProcessing = true;
var order = await _restWrapper.OrderRestApi.ReadOne(PageDto.Id, token);
PageDto = order;
switch (PageDto.OrderStatus)
{
case OrderStatus.Submitted:
ConfirmOrderButtonText = "ثبت پرداختی سفارش";
break;
case OrderStatus.Paid:
ConfirmOrderButtonText = "ثبت سفارش";
break;
case OrderStatus.Processing:
ConfirmOrderButtonText = "ثبت ارسال سفارش";
break;
case OrderStatus.Delivered:
ConfirmOrderButtonText = "ثبت انجام سفارش";
break;
default:
throw new ArgumentOutOfRangeException();
}
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
_mudDialog.Cancel();
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
_mudDialog.Cancel();
}
finally
{
IsProcessing = false;
}
};
}
public async Task SubmitConfirmAsync()
{
var options = new DialogOptions { CloseOnEscapeKey = true };
var parameters = new DialogParameters<QuestionDialog>();
parameters.Add(x => x.ContentText, "آیا از حذف دسته بندی اطمینان دارید ?");
var dialogReference = await _dialogService.ShowAsync<QuestionDialog>("حذف شرح حال", parameters, options);
var result = await dialogReference.Result;
if (!result.Canceled)
{
try
{
var token = await _userUtility.GetBearerTokenAsync();
if (token == null)
throw new Exception("Token is null");
IsProcessing = true;
OrderStatus nextOrderStatus = OrderStatus.Canceled;
switch (PageDto.OrderStatus)
{
case OrderStatus.OrderBag:
return;
break;
case OrderStatus.Submitted:
nextOrderStatus = OrderStatus.Paid;
break;
case OrderStatus.Paid:
nextOrderStatus = OrderStatus.Processing;
break;
case OrderStatus.Processing:
nextOrderStatus = OrderStatus.Delivered;
break;
case OrderStatus.Delivered:
nextOrderStatus = OrderStatus.Done;
break;
case OrderStatus.Done:
return;
break;
case OrderStatus.Canceled:
return;
break;
default:
throw new ArgumentOutOfRangeException();
}
await _restWrapper.OrderRestApi.ConfirmOrderStepAsync(PageDto.Id, nextOrderStatus, token);
_snackbar.Add($"ویرایش سفارش {PageDto.FactorCode} با موفقیت انجام شد", Severity.Success);
_mudDialog.Close();
}
catch (ApiException ex)
{
var exe = await ex.GetContentAsAsync<ApiResult>();
_snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
_mudDialog.Cancel();
}
catch (Exception e)
{
_snackbar.Add(e.Message, Severity.Error);
_mudDialog.Cancel();
}
finally
{
IsProcessing = false;
}
}
}
}