using Duempelkas.App.Services.Models; using Microsoft.AspNetCore.Components; namespace Duempelkas.App.Pages.Accounts; public partial class AccountDetail { #region Parameters [Parameter] public int AccountId { get; set; } #endregion #region Fields private AccountSummaryDto? account; private AccountBalanceDto? balance; private List? entries; private bool showAddEntry; private bool showAddTransfer; private bool showEditName; private bool showEditCarryover; private bool showYearFilterDialog; private int? selectedYear { get => Period.Year; set => Period.Year = value; } private List availableYears = []; private int? confirmDeleteEntryId; private string? confirmDeleteEntryTitle; private int? confirmRestoreEntryId; private string? confirmRestoreEntryTitle; private string? savedPdfPath; private FileOperationResult? fileResult; private bool fileBusy; private int? savedOutsideYear; private string emptyEntriesMessage = "Noch keine Buchungen vorhanden."; private EntryDto? editingEntry; private EntryDto? editingTransferEntry; #endregion #region Navigation private void NavigateBack() => Navigation.NavigateTo("/"); #endregion #region Lifecycle protected override async Task OnParametersSetAsync() { await LoadAll(); } #endregion #region Data Loading private async Task LoadAll() { account = await AccountService.GetAccountAsync(AccountId); balance = await BalanceQueryService.GetAccountBalanceAsync(AccountId, selectedYear); entries = await EntryService.GetEntriesAsync(AccountId, selectedYear); var entryYears = await EntryService.GetEntryYearsAsync(AccountId); availableYears = Period.Choices(entryYears); emptyEntriesMessage = entryYears.Count == 0 ? "Noch keine Buchungen vorhanden." : "Keine Buchungen in diesem Zeitraum. Wähle ein anderes Jahr oder Alle Jahre."; } private void OpenYearFilterDialog() { showYearFilterDialog = true; } private void CloseYearFilterDialog() { showYearFilterDialog = false; } private async Task SelectFilterYear(int? year) { selectedYear = year; savedOutsideYear = null; showYearFilterDialog = false; await LoadAll(); } #endregion #region Actions private async Task HandleSaveName(string newName) { await AccountService.RenameAccountAsync(AccountId, newName); showEditName = false; await LoadAll(); } private async Task HandleSaveCarryover(decimal newAmount) { await AccountService.UpdateCarryoverAsync(AccountId, newAmount, selectedYear); showEditCarryover = false; await LoadAll(); } private async Task HandleEntryCreated(DateTime savedDate) { savedOutsideYear = selectedYear.HasValue && selectedYear.Value != savedDate.Year ? savedDate.Year : null; showAddEntry = false; await LoadAll(); } private async Task HandleTransferCreated(DateTime savedDate) { savedOutsideYear = selectedYear.HasValue && selectedYear.Value != savedDate.Year ? savedDate.Year : null; showAddTransfer = false; await LoadAll(); } private Task ShowSavedYear() => SelectFilterYear(savedOutsideYear); private void RequestDeleteEntry(int entryId) { confirmDeleteEntryId = entryId; confirmDeleteEntryTitle = entries?.FirstOrDefault(e => e.Id == entryId)?.Title; } private void CancelDeleteConfirm() { confirmDeleteEntryId = null; confirmDeleteEntryTitle = null; } private async Task HandleConfirmDelete() { if (confirmDeleteEntryId.HasValue) { await EntryService.DeleteEntryAsync(confirmDeleteEntryId.Value); confirmDeleteEntryId = null; confirmDeleteEntryTitle = null; await LoadAll(); } } private void RequestRestoreEntry(int entryId) { confirmRestoreEntryId = entryId; confirmRestoreEntryTitle = entries?.FirstOrDefault(e => e.Id == entryId)?.Title; } private void CancelRestoreConfirm() { confirmRestoreEntryId = null; confirmRestoreEntryTitle = null; } private async Task HandleConfirmRestore() { if (confirmRestoreEntryId.HasValue) { await EntryService.RestoreEntryAsync(confirmRestoreEntryId.Value); confirmRestoreEntryId = null; confirmRestoreEntryTitle = null; await LoadAll(); } } private void RequestEditEntry(int entryId) { var entry = entries?.FirstOrDefault(e => e.Id == entryId); if (entry?.IsTransfer == true) { editingTransferEntry = entry; } else { editingEntry = entry; } } private async Task HandleEntryEdited(DateTime savedDate) { savedOutsideYear = selectedYear.HasValue && selectedYear.Value != savedDate.Year ? savedDate.Year : null; editingEntry = null; editingTransferEntry = null; await LoadAll(); } private async Task HandleExport() { if (fileBusy) return; fileBusy = true; fileResult = null; try { var pdf = await PdfStatementService.GenerateStatementAsync(AccountId, selectedYear); var suffix = selectedYear.HasValue ? $"_{selectedYear.Value}" : "_Gesamt"; fileResult = await FileSaveService.SaveFileAsync(pdf, $"{account?.Name}{suffix}.pdf"); savedPdfPath = fileResult.Status == FileOperationStatus.Success ? fileResult.Path : null; } catch (Exception ex) { fileResult = FileOperationResult.Error(ex); } finally { fileBusy = false; } } private async Task HandleOpenSavedPdf() { if (fileBusy) return; fileBusy = true; fileResult = null; try { if (!string.IsNullOrWhiteSpace(savedPdfPath)) { fileResult = await FileSaveService.OpenFileAsync(savedPdfPath); } savedPdfPath = null; } catch (Exception ex) { fileResult = FileOperationResult.Error(ex); } finally { fileBusy = false; } } private void CancelOpenSavedPdf() { savedPdfPath = null; } #endregion #region Helpers private static string FormatAmount(decimal amount) => $"{amount:N2} €"; private string GetFilterLabel() => Period.Label; #endregion }