@using Somnia.GuestLog.Models.DTOs; @using Somnia.GuestLog.Services @inject ILogger logger; @inject IUserMessageService userMessage @inject IGuestDocumentService guestDocumentService; @LanguageLocale.control_GuestDocumentSearchDialog_dialogTitle @LanguageLocale.general_btnSearch @if (!isLoading && results.Count() == 0) { @LanguageLocale.general_noResults } @if (isLoading) { } @if (!isLoading) { @* Only show on first load *@ @if (loadCount == 1) { @LanguageLocale.control_GuestDocumentSearchDialog_txtSuggestedGuests } @foreach (GuestDocumentDTO result in results) { } } @LanguageLocale.general_btnCancel @LanguageLocale.control_GuestDocumentSearchDialog_btnSelectDocument @code { // Dialog Instance injected when open by the DialogService. // This field is null when dialog is not open by the DialogService. [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!; private GuestDocFilterDTO filter { get; set; } = new(); private IEnumerable results = []; private GuestDocumentDTO? SelectedResult { get; set; } private bool isLoading = false; private int loadCount = 0; protected override async Task OnInitializedAsync() { //Load last 5 guests. await LoadGuests(limit: 5); } private void Cancel() => MudDialog.Cancel(); private async Task LoadGuests(int limit = 25) { try { isLoading = true; results = await guestDocumentService.GetDtoListByFilterAsync(filter); isLoading = false; loadCount++; } catch (Exception ex) { logger.LogError(ex, "GuestDocument Search returned an error!"); userMessage.ShowMessage($"Error searching: {ex.Message}", MessageKind.Error); } } #region Event Handlers private async Task BtnSearchGuestDocuments() { if (filter.IsEmpty) { userMessage.ShowMessage("Search can't be empty!", MessageKind.Warning); return; } //Load unlimited guests using current filter. await LoadGuests(); } private async Task btnSelectGuest() { if (SelectedResult is null) { userMessage.ShowMessage("A guest document must be selected", MessageKind.Warning); return; } DialogResult dialogResult = DialogResult.Ok(SelectedResult); //Close the dialog when opened by DialogService. logger.LogDebug("GuestDocumentSearchDialog - Closed MudDialogInstance!"); MudDialog.Close(dialogResult); return; } #endregion }