@inject MesaFabApprovalAuthStateProvider authStateProvider
@inject NavigationManager navigationManager
@inject IPCRBService pcrbService
@inject IUserService userService
@inject IECNService ecnService
@inject ISnackbar snackbar
@if (DocNumberIsNA()) {
} else {
}
@if (!string.IsNullOrWhiteSpace(document.DocNumbers) && ((DocNumberIsNA() && !string.IsNullOrWhiteSpace(document.Comment)) ||
(!DocNumberIsNA() && ecnNoIsValid))) {
@if (saveInProcess) {
Processing
} else {
Save
}
}
Cancel
@code {
[CascadingParameter]
MudDialogInstance MudDialog { get; set; }
[Parameter]
public required PCR3Document document { get; set; }
private string[] errors = { };
private bool complete = false;
private bool saveInProcess = false;
private bool ecnNoIsValid = true;
protected override async Task OnParametersSetAsync() {
complete = document.CompletedByID > 0;
}
private async Task Save() {
saveInProcess = true;
try {
if (authStateProvider.CurrentUser is null) {
await authStateProvider.Logout();
navigationManager.NavigateTo("login");
return;
}
if (!complete) {
document.CompletedByID = 0;
document.CompletedBy = null;
document.CompletedDate = DateTimeUtilities.MAX_DT;
}
if (complete && document.CompletedByID <= 0) {
document.CompletedByID = authStateProvider.CurrentUser.UserID;
document.CompletedBy = authStateProvider.CurrentUser;
document.CompletedDate = DateTime.Now;
}
if (string.IsNullOrWhiteSpace(document.DocNumbers)) {
throw new Exception("Document Numbers cannot be empty");
}
if (!DocNumberIsNA() && !ecnNoIsValid)
throw new Exception($"{document.ECNNumber} is not a valid ECN#");
if (DocNumberIsNA() && string.IsNullOrWhiteSpace(document.Comment))
throw new Exception("you must provide a comment");
await pcrbService.UpdatePCR3Document(document);
await pcrbService.GetPCR3DocumentsForPlanNumber(document.PlanNumber, true);
saveInProcess = false;
MudDialog.Close(DialogResult.Ok(document));
} catch (Exception ex) {
saveInProcess = false;
snackbar.Add($"Unable to save document, because {ex.Message}", Severity.Error);
}
}
private void Cancel() {
MudDialog.Close(DialogResult.Cancel());
}
private bool DocNumberIsNA() {
if (document.DocNumbers.ToLower().Equals("na") ||
document.DocNumbers.ToLower().Equals("n/a") ||
document.DocNumbers.ToLower().Equals("n a") ||
document.DocNumbers.ToLower().Equals("not applicable")) {
return true;
}
return false;
}
private async Task ECNNoIsValid(int ecnNumber) {
string? result = await ecnService.ECNNumberIsValidStr(ecnNumber);
if (result is null) ecnNoIsValid = true;
else ecnNoIsValid = false;
StateHasChanged();
return result;
}
}