using BudgetApp.Data.Entities; namespace BudgetApp.Components.Pages; public partial class Salary { private List? _salaries; private List? _accountsList; private readonly Dictionary _nextPaydates = []; private string? _userId; private SalaryForm _formModel = new(); protected override async Task OnInitializedAsync() { var authState = await AuthStateProvider.GetAuthenticationStateAsync(); _userId = authState.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value; await LoadData(); } private async Task LoadData() { if (_userId is null) return; var windowEnd = DateTime.Today.AddDays(90); await SalaryService.MaterializeSalaryTransactionsAsync(_userId, windowEnd); _salaries = await SalaryService.GetSalariesAsync(_userId); _accountsList = await AccountService.GetAccountsAsync(_userId); _nextPaydates.Clear(); foreach (var sal in _salaries) { _nextPaydates[sal.Id] = await SalaryService.GetNextPaydateAsync(sal.Id); } if (_accountsList.Count > 0 && _formModel.AccountId == 0) { _formModel.AccountId = _accountsList[0].Id; } } private async Task AddSalary() { if (_userId is null) return; var entry = new Data.Entities.Salary { Name = _formModel.Name, Amount = _formModel.Amount, AccountId = _formModel.AccountId, RecurrenceType = _formModel.RecurrenceType, StartDate = _formModel.StartDate, UserId = _userId }; await SalaryService.CreateSalaryAsync(entry, DateTime.Today.AddDays(90)); _formModel = new SalaryForm { AccountId = _accountsList?.FirstOrDefault()?.Id ?? 0 }; await LoadData(); } private bool _showDeleteModal; private Data.Entities.Salary? _pendingDeleteSalary; private void ConfirmDelete(Data.Entities.Salary s) { _pendingDeleteSalary = s; _showDeleteModal = true; } private void CancelDelete() { _showDeleteModal = false; _pendingDeleteSalary = null; } private async Task ExecuteDelete() { if (_userId is null || _pendingDeleteSalary is null) return; _showDeleteModal = false; await SalaryService.DeleteSalaryAsync(_pendingDeleteSalary.Id, _userId); _pendingDeleteSalary = null; await LoadData(); } private sealed class SalaryForm : System.ComponentModel.DataAnnotations.IValidatableObject { [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Name is required")] [System.ComponentModel.DataAnnotations.MaxLength(200)] public string Name { get; set; } = string.Empty; [System.ComponentModel.DataAnnotations.Range(0.01, double.MaxValue, ErrorMessage = "Amount must be greater than zero")] public decimal Amount { get; set; } public int AccountId { get; set; } public RecurrenceType RecurrenceType { get; set; } = RecurrenceType.Monthly; public DateTime StartDate { get; set; } = DateTime.Today; public IEnumerable Validate( System.ComponentModel.DataAnnotations.ValidationContext validationContext) { yield break; } } }