using Duempelkas.App.Services.Models; using Microsoft.AspNetCore.Components; namespace Duempelkas.App.Components.Dialogs; public partial class AddTransferDialog { #region Parameters [Parameter] public int SourceAccountId { get; set; } [Parameter] public EntryDto? EditEntry { get; set; } [Parameter] public EventCallback OnSave { get; set; } [Parameter] public EventCallback OnCancel { get; set; } #endregion [Parameter] public int? ContextYear { get; set; } private bool confirmDifferentYear; private int? confirmedYear; private bool OutsideYear => ContextYear.HasValue && date.Year != ContextYear.Value; #region Fields private List accounts = new(); private int targetAccountId; private string dateText = DateTime.Today.ToString("yyyy-MM-dd"); private DateTime date => Duempelkas.App.Services.BookingInput.Date(dateText); private string title = string.Empty; private string amountText = string.Empty; private bool ValidAmount => Duempelkas.App.Services.BookingInput.TryAmount(amountText, out _); private decimal amount => Duempelkas.App.Services.BookingInput.TryAmount(amountText, out var value) ? value : 0; private bool initialized; private int? initializedEntryId; #endregion #region Properties private bool CanSave => targetAccountId > 0 && targetAccountId != SourceAccountId && !string.IsNullOrWhiteSpace(title) && ValidAmount && date != default && (!OutsideYear || (confirmDifferentYear && confirmedYear == date.Year)); #endregion #region Lifecycle protected override async Task OnParametersSetAsync() { if (!accounts.Any()) { accounts = await AccountService.GetAllAccountsAsync(); } if (initialized && initializedEntryId == EditEntry?.Id) return; initialized = true; initializedEntryId = EditEntry?.Id; if (EditEntry != null) { targetAccountId = EditEntry.LinkedAccountId ?? 0; dateText = EditEntry.Date.ToString("yyyy-MM-dd"); title = EditEntry.Title; amountText = EditEntry.Amount.ToString("0.00", System.Globalization.CultureInfo.GetCultureInfo("de-DE")); } } #endregion #region Actions private async Task Save() { if (!CanSave) return; if (EditEntry != null) { await EntryService.UpdateTransferAsync(EditEntry.Id, targetAccountId, date, title.Trim(), amount); } else { await EntryService.CreateTransferAsync(SourceAccountId, targetAccountId, date, title.Trim(), amount); } await OnSave.InvokeAsync(date); } private async Task Cancel() => await OnCancel.InvokeAsync(); #endregion }