Files
logofclient/ResultWindow.axaml.cs
2026-08-14 14:55:11 +02:00

270 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
namespace Logof_Client;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public partial class ResultWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
private List<ResultItem> _results = new();
public List<ResultItem> Results
{
get => _results;
private set
{
_results = value;
PropertyChanged?.Invoke(
this,
new PropertyChangedEventArgs(nameof(Results))
);
}
}
public List<CheckBox> errortypecheckboxes = new();
public KasAddressList ur_addresses = new("Ergebnis_" + DateTime.Now.ToString("ddMMyy_HHmmss"));
public List<KasPerson> ur_result;
public List<CheckBox> warningtypecheckboxes = new();
public List<ResultItem>? SelectedResults { get; set; }
public ResultWindow(List<KasPerson> result, int addressSetID)
{
InitializeComponent();
DataContext = this;
ur_result = result;
Load(result);
}
//public List<ResultItem> Results { get; private set; } = new();
private void GenerateView(List<KasPerson> result) {
try
{
// Filter to only show persons with errors
var result_with_errors = result.Where(p => p.PersonError != null).ToList();
LblResultCount.Content = $"{result_with_errors.Count}/{ur_result.Count} Ergebnisse";
//LbResults.Items.Clear();
// foreach (var person in result_with_errors)
// LbResults.Items.Add(person.PersonError.ToString(person));
Results = result_with_errors
.Select(person => new ResultItem
{
Person = person,
PersonId = person.id,
Name = $"{person.vorname} {person.name}".Trim(),
Errors = string.Join(", ", person.PersonError.errors),
Warnings = string.Join(", ", person.PersonError.warnings),
Refsid = person.refsid.ToString() ?? ""
})
.ToList();
}
catch (Exception ex)
{
Logger.Log("Error while generating result view: " + ex.Message, Logger.LogType.Warning);
}
}
private void ViewSingle(int id)
{
foreach (var result in ur_addresses.KasPersons)
if (result.id == id)
{
var wind = new Window();
var stp = new StackPanel();
stp.Orientation = Orientation.Horizontal;
stp.Margin = new Thickness(10);
var tb = new TextBlock();
var tb2 = new TextBlock();
tb.Text =
"id:\nanrede:\ntitel:\nvorname:\nadel:\nname:\nnamezus:\nanredzus:\nstrasse:\nstrasse2:\nplz:\nort:\nland:\npplz:\npostfach:\nname1:\nname2:\nname3:\nname4:\nname5:\nfunktion:\nfunktion2:\nabteilung:\nfunktionad:";
tb2.Text = result.id + "\n" + result.anrede + "\n" + result.titel + "\n" + result.vorname + "\n" +
result.adel + "\n" + result.name + "\n" + result.namezus + "\n" + result.anredzus + "\n" +
result.strasse + "\n" + result.strasse2 + "\n" + result.plz + "\n" + result.ort + "\n" +
result.land + "\n" + result.pplz + "\n" + result.postfach + "\n" + result.name1 + "\n" +
result.name2 + "\n" + result.name3 + "\n" + result.name4 + "\n" + result.name5 + "\n" +
result.funktion + "\n" + result.funktion2 + "\n" + result.abteilung + "\n" +
result.funktionad;
stp.Children.Add(tb);
stp.Children.Add(tb2);
wind.Content = stp;
wind.ShowInTaskbar = false;
wind.SizeToContent = SizeToContent.WidthAndHeight;
wind.Show();
return;
}
}
private void Load(List<KasPerson> result)
{
try
{
var knownErrors = new List<AddressCheck.ErrorTypes>();
var knownWarnings = new List<AddressCheck.WarningTypes>();
foreach (var person in result)
{
if (person.PersonError == null) continue;
foreach (var errtyp in person.PersonError.errors)
if (!knownErrors.Contains(errtyp))
knownErrors.Add(errtyp);
foreach (var wartyp in person.PersonError.warnings)
if (!knownWarnings.Contains(wartyp))
knownWarnings.Add(wartyp);
}
foreach (var errtype in knownErrors)
{
var cb = new CheckBox();
cb.IsChecked = true;
cb.Content = errtype.ToString();
errortypecheckboxes.Add(cb);
StpFilterOptions.Children.Add(cb);
}
foreach (var wartype in knownWarnings)
{
var cb = new CheckBox();
cb.IsChecked = true;
cb.Content = wartype.ToString();
warningtypecheckboxes.Add(cb);
StpFilterOptions.Children.Add(cb);
}
GenerateView(result);
}
catch (Exception ex)
{
Logger.Log("Error while showing naming window: " + ex.Message, Logger.LogType.Warning);
}
}
private void UpdateFilter()
{
try
{
var temp_result = new List<KasPerson>();
var checkedErrors = new HashSet<AddressCheck.ErrorTypes>();
var checkedWarnings = new HashSet<AddressCheck.WarningTypes>();
// safer parsing: use TryParse and trim the Content string
foreach (var cb in errortypecheckboxes)
if (cb.IsChecked == true)
{
var s = cb.Content?.ToString()?.Trim();
if (!string.IsNullOrEmpty(s) &&
Enum.TryParse<AddressCheck.ErrorTypes>(s, true, out var et))
checkedErrors.Add(et);
}
foreach (var cb in warningtypecheckboxes)
if (cb.IsChecked == true)
{
var s = cb.Content?.ToString()?.Trim();
if (!string.IsNullOrEmpty(s) &&
Enum.TryParse<AddressCheck.WarningTypes>(s, true, out var wt))
checkedWarnings.Add(wt);
}
// If no checkboxes are selected, show all persons with errors (default behavior)
if (checkedErrors.Count == 0 && checkedWarnings.Count == 0)
temp_result = ur_result.Where(p => p.PersonError != null).ToList();
else
foreach (var person in ur_result)
{
if (person.PersonError == null) continue;
var personErrors = person.PersonError.errors ?? Enumerable.Empty<AddressCheck.ErrorTypes>();
var personWarnings = person.PersonError.warnings ?? Enumerable.Empty<AddressCheck.WarningTypes>();
var matchesError = false;
var matchesWarning = false;
// only test errors if the user selected error-types
if (checkedErrors.Count > 0)
matchesError = personErrors.Any(err => checkedErrors.Contains(err));
// only test warnings if the user selected warning-types
if (checkedWarnings.Count > 0)
matchesWarning = personWarnings.Any(war => checkedWarnings.Contains(war));
// If at least one category matches (OR across categories), include person
if ((matchesError || matchesWarning) && !temp_result.Contains(person))
temp_result.Add(person);
}
LblResultCount.Content = $"{temp_result.Count}/{ur_result.Count} Ergebnisse";
//LbResults.Items.Clear();
GenerateView(temp_result);
//foreach (var person in temp_result) LbResults.Items.Add(person.PersonError.ToString(person));
} catch (Exception ex)
{
Logger.Log("Error while updating filter: " + ex.Message, Logger.LogType.Warning);
}
}
private void BtnShowSelected_OnClick(object? sender, RoutedEventArgs e)
{
if (LbResults.SelectedItems.Count > 0)
{
var selectedPersons = LbResults.SelectedItems
.Cast<ResultItem>()
.Select(x => x.Person)
.ToList();
if (selectedPersons.Count == 0)
return;
var preview = new PreviewWindow(selectedPersons);
preview.Show();
}
}
private void BtnExecuteFilter_OnClick(object? sender, RoutedEventArgs e)
{
Console.WriteLine("Updating filter...");
UpdateFilter();
}
//public ObservableCollection<ResultItem> Results { get; } = new();
}
public class ResultItem
{
public int PersonId { get; init; }
public string Name { get; init; } = "";
public string Errors { get; init; } = "";
public string Warnings { get; init; } = "";
public string Refsid { get; init; } = "";
public KasPerson Person { get; init; } = null!;
}