using System.Globalization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Microsoft.FluentUI.AspNetCore.Components; using Schaad.Accounting.Interfaces; using Schaad.Accounting.Models; namespace Schaad.Accounting.UI.Components.Pages.Dialogs; public partial class WaterBillDialog : ComponentBase { private static readonly CultureInfo SwissNumbers = CreateSwissNumbers(); private EditContext editContext = null!; // FluentDatePicker needs DateTime?; the model stores DateOnly (see WaterBill). private DateTime? periodFrom; private DateTime? periodTo; [CascadingParameter] public FluentDialog Dialog { get; set; } = null!; [Parameter] public WaterBill Content { get; set; } = null!; [Inject] private IWaterBillRepository waterBillRepository { get; set; } = null!; protected override void OnInitialized() { periodFrom = Content.PeriodFrom == default ? null : Content.PeriodFrom.ToDateTime(TimeOnly.MinValue); periodTo = Content.PeriodTo == default ? null : Content.PeriodTo.ToDateTime(TimeOnly.MinValue); editContext = new EditContext(Content); } private async Task SaveAsync() { if (periodFrom is null || periodTo is null) { return; } Content.PeriodFrom = DateOnly.FromDateTime(periodFrom.Value); Content.PeriodTo = DateOnly.FromDateTime(periodTo.Value); if (editContext.Validate()) { await waterBillRepository.SaveWaterBillAsync(Content); await Dialog.CloseAsync(Content); } } private async Task CancelAsync() { await Dialog.CancelAsync(); } private static string M3(decimal value) => value.ToString("#,0.00", SwissNumbers) + " m³"; private static string Chf(decimal value) => value.ToString("#,0.00", SwissNumbers) + " CHF"; private static CultureInfo CreateSwissNumbers() { var culture = new CultureInfo("de-CH"); culture.NumberFormat.NumberGroupSeparator = "'"; return culture; } }