@using System.Diagnostics @using FluentValidation; @using Somnia.GuestLog.Models.DTOs; @using Somnia.GuestLog.Services; @using Somnia.GuestLog.Validators; @inject IDataListService dataListService; @inject IValidator stayValidator; @inject IDialogService dialogService; @inject IUserMessageService userMessage; @inject ILocalizerService Localizer; @foreach (string payment in PaymentMethodSet) { @dataListService.GetLocalizedValue(GuestConsts.DataList_PaymentMethods, payment) } @if (FormMode == FormMode.ReadOnly) { @if (Model.StayUploadStatusTime != null) { } } @if (!ReadOnly) { @LanguageLocale.control_StayEntryForm_btnAddGuest } @LanguageLocale.dto_GuestDocument_DocumentType @LanguageLocale.dto_GuestDocument_DocumentNum @LanguageLocale.dto_GuestDocument_Name @LanguageLocale.dto_GuestDocument_Surname @LanguageLocale.dto_GuestDocument_Surname2 @LanguageLocale.general_actions @context.DocumentType @context.DocumentNum @context.Name @context.Surname @context.Surname2 @if (!ReadOnly) { } @if (ReadOnly) { } @code { [Parameter] public StayEntryDTO Model { private get; set; } = new(); [Parameter, EditorRequired] public FormMode FormMode { get; set; } private bool ReadOnly => FormMode == FormMode.ReadOnly; private int stayDetailPanelSize => FormMode == FormMode.ReadOnly ? 9 : 12; private MudForm _form = null!; private const string FieldMarginY = "margin-block: 0.5em"; private IEnumerable PaymentMethodSet { get { if (field is null) { field = dataListService.GetDataListByName(GuestConsts.DataList_PaymentMethods); } return field; } } protected override void OnInitialized() { //Initialize values when not ReadOnly. if (FormMode == FormMode.Create) { Model.DateIn = DateTime.Now; DateTime dateOut = DateTime.Today.AddDays(1); Model.DateOut = new( year: dateOut.Year, month: dateOut.Month, day: dateOut.Day, hour: 12, minute: 0, second: 0 ); //Init Guest List Model.GuestDocuments = []; } } #region Public Methods public async Task ValidateForm() { await _form.ValidateAsync(); if (Model.GuestDocuments is null || Model.GuestDocuments.Count == 0) { userMessage.ShowMessage("At least 1 guest is required!", MessageKind.Warning); return false; } return _form.IsValid; } public StayEntryDTO GetModel() { return Model; } #endregion private static DateTime? GetDateTimeFromDateAndTime(DateTime? date, TimeSpan? time) { DateOnly? dateOnly = date is not null ? DateOnly.FromDateTime(date.Value) : null; TimeOnly? timeOnly = time is not null ? TimeOnly.FromTimeSpan(time.Value) : null; if (dateOnly is not null && timeOnly is not null) { return new DateTime(dateOnly.Value, timeOnly.Value); } return null; } #region Event Handlers private async Task BtnAddGuest() { Debug.Assert(Model.GuestDocuments != null); DialogOptions dialogOptions = new() { MaxWidth = MaxWidth.Medium, FullWidth = true, BackdropClick = false, CloseOnEscapeKey = true, }; var dialog = await dialogService.ShowAsync(title: "", options: dialogOptions); DialogResult? result = await dialog.Result; if (result is null || result.Canceled) { return; } GuestDocumentDTO? resultData = await dialog.GetReturnValueAsync(); if (resultData is null) { userMessage.ShowMessage("Error! No Guest Document selected!", MessageKind.Error); return; } //Check if this guest is already added. if (Model.GuestDocuments.Count > 0) { bool alreadyAdded = Model.GuestDocuments.Any(e => e.Id == resultData.Id); if (alreadyAdded) { userMessage.ShowMessage("GuestDocument already added!", MessageKind.Warning); return; } } Model.GuestDocuments.Add(resultData); } private void BtnDeleteGuest(long guestDocId, int version) { Debug.Assert(Model.GuestDocuments is not null); //Filter out given GuestDocument from model. Model.GuestDocuments = Model.GuestDocuments .Where(e => !(e.Id == guestDocId && e.Version == version)) .ToList(); } private async Task BtnViewGuestDetails(GuestDocumentDTO item) { var parameters = new DialogParameters { { x => x.DocumentDTO, item } }; var options = new DialogOptions() { FullScreen = true, CloseButton = true, CloseOnEscapeKey = true, }; await dialogService.ShowAsync("GuestDoc Details", parameters, options); } #endregion }