@inject IMRBService mrbService @inject ISnackbar snackbar @inject ICustomerService customerService @if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) { @foreach (string customer in customerNames) { } @foreach (string customer in customerNames) { } } else { @foreach (string customer in customerNames) { } } @if (mrbAction.Action.Equals("Scrap")) { } @if (processingSave) { Processing } else { Save } @if (mrbAction is not null && mrbAction.ActionID > 0) { @if (processingDelete) { Processing } else { Delete } } Cancel @code { [CascadingParameter] MudDialogInstance MudDialog { get; set; } [Parameter] public MRBAction mrbAction { get; set; } private IEnumerable? actions = null; private MRBAction lastAction = null; private IEnumerable customerNames = new List(); private string convertFromCustomer = string.Empty; private string convertFromPart = string.Empty; private string convertToCustomer = string.Empty; private string convertToPart = string.Empty; private bool isVisible { get; set; } private string[] errors = { }; private bool processingSave = false; private bool processingDelete = false; protected override async Task OnParametersSetAsync() { isVisible = true; if (mrbAction is null) { snackbar.Add("MRB action cannot be null", Severity.Warning); MudDialog.Cancel(); } else { actions = (await mrbService.GetMRBActionsForMRB(mrbAction.MRBNumber, false)).OrderByDescending(a => a.ActionID); if (actions is not null && actions.Count() > 0) { if (string.IsNullOrWhiteSpace(mrbAction.Action)) mrbAction.Action = actions.First().Action; if (string.IsNullOrWhiteSpace(mrbAction.Customer)) mrbAction.Customer = actions.First().Customer; if (string.IsNullOrWhiteSpace(mrbAction.LotNumber)) mrbAction.LotNumber = actions.First().LotNumber; if (string.IsNullOrWhiteSpace(mrbAction.PartNumber)) mrbAction.PartNumber = actions.First().PartNumber; if (string.IsNullOrWhiteSpace(mrbAction.Justification)) mrbAction.Justification = actions.First().Justification; if (mrbAction.Quantity == 0) mrbAction.Quantity = actions.First().Quantity; if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) { string[] convertFrom = actions.First().ConvertFrom.Split(" "); if (convertFrom.Length > 1) { convertFromCustomer = convertFrom[0]; foreach (string partStr in convertFrom.Skip(1)) convertFromPart += partStr; } string[] convertTo = actions.First().ConvertTo.Split(" "); if (convertTo.Length > 1) { convertToCustomer = convertTo[0]; foreach (string partStr in convertTo.Skip(1)) convertToPart += partStr; } } } } if (customerNames is null || customerNames.Count() <= 0) customerNames = await customerService.GetAllCustomerNames(); StateHasChanged(); } private bool FormIsValid() { bool actionIsValid = mrbAction.Action.Equals("Block") || mrbAction.Action.Equals("Convert") || mrbAction.Action.Equals("Other") || mrbAction.Action.Equals("Recall") || mrbAction.Action.Equals("Scrap") || mrbAction.Action.Equals("Unblock") || mrbAction.Action.Equals("Waiver"); actionIsValid = actionIsValid && !string.IsNullOrWhiteSpace(mrbAction.Customer) && !string.IsNullOrWhiteSpace(mrbAction.PartNumber) && !string.IsNullOrWhiteSpace(mrbAction.LotNumber); actionIsValid = actionIsValid && mrbAction.Quantity > 0; if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) { actionIsValid = actionIsValid && !string.IsNullOrWhiteSpace(convertFromCustomer) && !string.IsNullOrWhiteSpace(convertFromPart) && !string.IsNullOrWhiteSpace(convertToCustomer) && !string.IsNullOrWhiteSpace(convertToPart); } if (mrbAction.Action.Equals("Scrap", StringComparison.InvariantCultureIgnoreCase)) actionIsValid = actionIsValid && !string.IsNullOrWhiteSpace(mrbAction.Justification); return actionIsValid; } private async void SaveMRBAction() { processingSave = true; try { if (!FormIsValid()) throw new Exception("You must complete the form before saving!"); if (mrbAction.Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) { mrbAction.ConvertFrom = $"{convertFromCustomer} {convertFromPart}"; mrbAction.ConvertTo = $"{convertToCustomer} {convertToPart}"; } if (mrbAction.MRBNumber > 0) { if (mrbAction.ActionID <= 0) { await mrbService.CreateMRBAction(mrbAction); snackbar.Add("MRB action created", Severity.Success); } else { await mrbService.UpdateMRBAction(mrbAction); snackbar.Add("MRB action updated", Severity.Success); } actions = (await mrbService.GetMRBActionsForMRB(mrbAction.MRBNumber, true)).OrderByDescending(a => a.ActionID); } else { snackbar.Add("MRB action saved", Severity.Success); } StateHasChanged(); MudDialog.Close(DialogResult.Ok(mrbAction)); } catch (Exception ex) { snackbar.Add(ex.Message, Severity.Error); } processingSave = false; } private async void DeleteMRBAction() { processingDelete = true; try { if (mrbAction is null) throw new Exception("MRB action cannot be null!"); if (mrbAction.ActionID <= 0) throw new Exception("You cannot delete an action before creating it!"); if (mrbAction.MRBNumber <= 0) throw new Exception("Invalid MRB number!"); await mrbService.DeleteMRBAction(mrbAction); snackbar.Add("MRB action successfully deleted", Severity.Success); StateHasChanged(); MudDialog.Close(DialogResult.Ok(null)); } catch (Exception ex) { snackbar.Add(ex.Message, Severity.Error); } processingDelete = false; } private void Cancel() { MudDialog.Cancel(); } private async Task> PartNumberSearch(string value, CancellationToken token) { if (actions is null) return new List { value }; if (string.IsNullOrWhiteSpace(value)) return new string[0]; HashSet partNumbers = new(); partNumbers.Add(value); foreach (MRBAction action in actions) { if (action.PartNumber.Contains(value, StringComparison.InvariantCultureIgnoreCase)) partNumbers.Add(action.PartNumber); } return partNumbers; } private async Task> LotNumberSearch(string value, CancellationToken token) { if (actions is null) return new List { value }; if (string.IsNullOrWhiteSpace(value)) return new string[0]; HashSet lotNumbers = new(); lotNumbers.Add(value); foreach (MRBAction action in actions) { if (action.LotNumber.Contains(value, StringComparison.InvariantCultureIgnoreCase)) lotNumbers.Add(action.LotNumber); } return lotNumbers; } }