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 ElectricityBillDialog : ComponentBase { private static readonly CultureInfo SwissNumbers = CreateSwissNumbers(); private EditContext editContext = null!; // FluentDatePicker needs DateTime?; the model stores DateOnly (see ElectricityBill). private DateTime? periodFrom; private DateTime? periodTo; [CascadingParameter] public FluentDialog Dialog { get; set; } = null!; [Parameter] public ElectricityBill Content { get; set; } = null!; [Inject] private IElectricityBillRepository electricityBillRepository { get; set; } = null!; [Inject] private IDialogService dialogService { 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 electricityBillRepository.SaveElectricityBillAsync(Content); await Dialog.CloseAsync(Content); } } private async Task CancelAsync() { await Dialog.CancelAsync(); } private async Task ShowGuideAsync() { await dialogService.ShowDialogAsync(GuideImage, new DialogParameters() { Width = "680px", Title = "Woher kommen die Werte?", PreventDismissOnOverlayClick = true, PreventScroll = true, }); } 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; } }