Files
HC_APTBS/ViewModels/Dialogs/ReportViewModel.cs
2026-04-11 12:45:18 +02:00

159 lines
6.7 KiB
C#

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels.Dialogs
{
/// <summary>
/// ViewModel for the WReport dialog.
///
/// <para>
/// Lets the operator fill in client and company information before generating
/// the PDF report. The client list is backed by the
/// <see cref="IConfigurationService"/> and persisted when the dialog is accepted.
/// </para>
/// </summary>
public sealed partial class ReportViewModel : ObservableObject
{
// ── Services ──────────────────────────────────────────────────────────────
private readonly IConfigurationService _config;
// ── Constructor ───────────────────────────────────────────────────────────
/// <summary>Initialises and populates the dialog from configuration.</summary>
public ReportViewModel(IConfigurationService configService)
{
_config = configService;
CompanyName = _config.Settings.CompanyName;
CompanyInfo = _config.Settings.CompanyInfo;
foreach (var name in _config.Clients.Keys)
ClientNames.Add(name);
}
// ── Company ───────────────────────────────────────────────────────────────
/// <summary>Company name shown in the report header.</summary>
[ObservableProperty] private string _companyName = string.Empty;
/// <summary>Company address / contact info shown in the report header.</summary>
[ObservableProperty] private string _companyInfo = string.Empty;
// ── Client ────────────────────────────────────────────────────────────────
/// <summary>List of known client names for the autocomplete combo box.</summary>
public ObservableCollection<string> ClientNames { get; } = new();
/// <summary>Currently selected or typed client name.</summary>
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveClientCommand))]
[NotifyCanExecuteChangedFor(nameof(DeleteClientCommand))]
private string _selectedClientName = string.Empty;
/// <summary>Free-text notes for the selected client.</summary>
[ObservableProperty] private string _clientInfo = string.Empty;
/// <summary>Operator name.</summary>
[ObservableProperty] private string _operatorName = string.Empty;
/// <summary>Free-text observations appended to the report.</summary>
[ObservableProperty] private string _observations = string.Empty;
// ── Dialog result ─────────────────────────────────────────────────────────
/// <summary>True after the user clicks Accept; false if they cancel.</summary>
public bool Accepted { get; private set; }
// ── Commands ──────────────────────────────────────────────────────────────
/// <summary>Saves the current client entry, closes the dialog with Accepted=true.</summary>
[RelayCommand]
private void Accept()
{
SaveCurrentClient();
_config.Settings.CompanyName = CompanyName;
_config.Settings.CompanyInfo = CompanyInfo;
_config.SaveClients();
_config.SaveSettings();
Accepted = true;
RequestClose?.Invoke();
}
/// <summary>Closes the dialog without saving.</summary>
[RelayCommand]
private void Cancel()
{
Accepted = false;
RequestClose?.Invoke();
}
/// <summary>Persists the current client entry and refreshes the client list.</summary>
[RelayCommand(CanExecute = nameof(HasClientName))]
private void SaveClient()
{
SaveCurrentClient();
RefreshClientList();
}
/// <summary>Deletes the currently selected client from the list.</summary>
[RelayCommand(CanExecute = nameof(HasClientName))]
private void DeleteClient()
{
string name = SelectedClientName.Trim();
if (string.IsNullOrEmpty(name)) return;
if (_config.Clients.ContainsKey(name))
_config.Clients.Remove(name);
SelectedClientName = string.Empty;
ClientInfo = string.Empty;
RefreshClientList();
}
/// <summary>
/// Called when the user picks a client name from the combo box.
/// Loads the associated info text.
/// </summary>
public void SelectClient(string name)
{
SelectedClientName = name ?? string.Empty;
if (!string.IsNullOrEmpty(name) && _config.Clients.TryGetValue(name, out var info))
ClientInfo = info;
}
// ── Events ────────────────────────────────────────────────────────────────
/// <summary>Raised when the dialog should close itself.</summary>
public event System.Action? RequestClose;
// ── Helpers ───────────────────────────────────────────────────────────────
private bool HasClientName() => !string.IsNullOrWhiteSpace(SelectedClientName);
private void SaveCurrentClient()
{
string name = SelectedClientName.Trim();
if (string.IsNullOrEmpty(name)) return;
if (_config.Clients.ContainsKey(name))
_config.Clients[name] = ClientInfo;
else
_config.Clients.Add(name, ClientInfo);
}
private void RefreshClientList()
{
ClientNames.Clear();
foreach (var name in _config.Clients.Keys)
ClientNames.Add(name);
}
}
}