using System.Globalization; using Duempelkas.App.Services.Models; namespace Duempelkas.App.Pages; public partial class Dashboard { #region Fields private List? accounts; private bool showAddAccount; private bool showEditClubName; private bool showRestoreConfirm; private string clubName = string.Empty; private string? operationMessage; private string operationMessageClass = "alert-info"; private string? savedPdfPath; #endregion #region Properties private string DisplayClubName => string.IsNullOrWhiteSpace(clubName) ? "Mein Verein" : clubName; private decimal TotalClubBalance => accounts?.Sum(a => a.TotalBalance) ?? 0m; #endregion #region Lifecycle protected override async Task OnInitializedAsync() { await LoadClubName(); await LoadAccounts(); } #endregion #region Data Loading private async Task LoadAccounts() { accounts = await AccountService.GetAllAccountsAsync(); } private async Task LoadClubName() { clubName = await SettingsService.GetClubNameAsync() ?? string.Empty; } #endregion #region Actions private async Task HandleAccountCreated(string name) { await AccountService.CreateAccountAsync(name); showAddAccount = false; await LoadAccounts(); } private async Task HandleSaveClubName(string newName) { await SettingsService.SetClubNameAsync(newName); showEditClubName = false; await LoadClubName(); } private async Task HandleBackupAsync() { var message = await BackupService.CreateBackupAsync(); if (message.Contains("fehlgeschlagen", StringComparison.OrdinalIgnoreCase)) { SetOperationMessage(message, false); } } private Task HandleRestoreAsync() { showRestoreConfirm = true; return Task.CompletedTask; } private void CancelRestoreConfirm() { showRestoreConfirm = false; } private async Task HandleConfirmRestore() { showRestoreConfirm = false; var message = await BackupService.RestoreBackupAsync(); var isSuccess = message.StartsWith("Wiederherstellung erfolgreich", StringComparison.OrdinalIgnoreCase); if (isSuccess) { NavigationManager.NavigateTo("/", forceLoad: true); } else { SetOperationMessage(message, isSuccess); } } private async Task HandleDashboardExportAsync() { var pdf = await PdfStatementService.GenerateDashboardStatementAsync(); savedPdfPath = await FileSaveService.SaveFileAsync(pdf, $"{DisplayClubName}_Übersicht.pdf"); } private async Task HandleOpenSavedPdf() { if (!string.IsNullOrWhiteSpace(savedPdfPath)) { await FileSaveService.OpenFileAsync(savedPdfPath); } savedPdfPath = null; } private void CancelOpenSavedPdf() { savedPdfPath = null; } #endregion #region Helpers private void SetOperationMessage(string message, bool success) { operationMessage = message; operationMessageClass = success ? "alert-success" : "alert-danger"; } private static string FormatCurrency(decimal amount) { return amount.ToString("N2", CultureInfo.GetCultureInfo("de-DE")) + " €"; } #endregion }