using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Metrics; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using WGneu.Models; namespace WGneu.Windows { public partial class MemberListWindow : Window { private bool IsEditing = false; private bool IsCreating = false; private List TextFilter = new(); private readonly Control[] ExemptInputs; private readonly Dictionary Valid = new(); private readonly Dictionary OriginalValues = new(); private readonly RoutedCommand CtrlF = new(); private readonly WgContext Context = new(); public MemberListWindow() { InitializeComponent(); CtrlF.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control)); CommandBindings.Add(new CommandBinding(CtrlF, FocusSearchInput)); ExemptInputs = new Control[] { SearchInput, ActiveMemberInput, MemberList, NewMemberButton, EditMemberButton, DeleteMemberButton, ResetButton, SaveButton, CancelButton }; foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) Valid[tb] = true; } private void Window_Loaded(object sender, RoutedEventArgs e) { ActiveMemberInput.IsChecked = true; BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList(); DefaultKgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList(); } protected override void OnClosing(CancelEventArgs e) { Context.Dispose(); base.OnClosing(e); } private int CountMatchesInMember(Member m) { if (TextFilter.Count == 0) return 0; string[] check = new string[] { m.MgNr.ToString(), m.FamilyName.ToLower(), m.GivenName.ToLower(), m.DefaultKg.Name.ToLower() }; int i = 0; foreach (string c in check) { if (TextFilter.Any(f => c == f)) i += 10; else if (TextFilter.Any(f => c.Contains(f))) i += 1; } return i; } private void RefreshMemberList() { Context.Members.Load(); IQueryable memberQuery = Context.Members; if (ActiveMemberInput.IsChecked == true) memberQuery = memberQuery.Where(m => m.Active); List members = memberQuery.ToList(); members = members.OrderBy(m => m.FamilyName + " " + m.GivenName).ToList(); if (TextFilter.Count > 0) { members = members .ToDictionary(m => m, m => CountMatchesInMember(m)) .OrderByDescending(a => a.Value) .Where(a => a.Value > 0) .Select(a => a.Key) .ToList(); } MemberList.ItemsSource = members; if (members.Count == 1) MemberList.SelectedIndex = 0; RefreshInputs(); } private void ClearInputStates() { foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) Utils.ClearInputState(tb); foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) Utils.ClearInputState(cb); foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) Utils.ClearInputState(cb); foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) Utils.ClearInputState(rb); } private void RefreshInputs() { Member m = (Member)MemberList.SelectedItem; if (m != null) { EditMemberButton.IsEnabled = true; DeleteMemberButton.IsEnabled = true; FillInputs(m); } else { EditMemberButton.IsEnabled = false; DeleteMemberButton.IsEnabled = false; ClearInputs(); } ClearInputStates(); } private void InitInputs() { ClearInputs(); MgNrInput.Text = NextMgNr().ToString(); } private void MemberList_SelectionChanged(object sender, SelectionChangedEventArgs e) { RefreshInputs(); } private void ActiveMemberInput_Changed(object sender, RoutedEventArgs e) { RefreshMemberList(); } private void NewMemberButton_Click(object sender, RoutedEventArgs e) { IsCreating = true; MemberList.IsEnabled = false; InitInputs(); HideNewEditDeleteButtons(); ShowSaveResetCancelButtons(); UnlockInputs(); LockSearchInputs(); } private void EditMemberButton_Click(object sender, RoutedEventArgs e) { if (MemberList.SelectedItem == null) return; IsEditing = true; MemberList.IsEnabled = false; HideNewEditDeleteButtons(); ShowSaveResetCancelButtons(); UnlockInputs(); LockSearchInputs(); } private void DeleteMemberButton_Click(object sender, RoutedEventArgs e) { Member m = (Member)MemberList.SelectedItem; if (m == null) return; var r = MessageBox.Show( $"Soll das Mitglied \"{m.FamilyName} {m.GivenName}\" (MgNr. {m.MgNr}) wirklich unwiderruflich gelöscht werden?", "Mitglied löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); if (r == MessageBoxResult.Yes) { Context.Remove(m); Context.SaveChanges(); RefreshMemberList(); } } private void SaveButton_Click(object sender, RoutedEventArgs e) { Member? m = new(); if (IsEditing) m = (Member)MemberList.SelectedItem; else if (IsCreating) m = new(); int newMgNr = int.Parse(MgNrInput.Text); m.Prefix = (PrefixInput.Text == "") ? null : PrefixInput.Text; m.GivenName = GivenNameInput.Text; m.FamilyName = FamilyNameInput.Text; m.Suffix = (SuffixInput.Text == "") ? null : SuffixInput.Text; m.Birthday = (BirthdayInput.Text == "") ? null : string.Join("-", BirthdayInput.Text.Split(".").Reverse()); m.CountryCode = "AT"; m.PostalDestId = ((AT_Plz)OrtInput.SelectedItem).Id; m.PostalDest = Context.PostalDestinations.Find(m.CountryCode, m.PostalDestId); m.Address = AddressInput.Text; m.Email = (EmailInput.Text == "") ? null : EmailInput.Text; m.PhoneLandline = (PhoneLandlineInput.Text == "") ? null : PhoneLandlineInput.Text.Replace(" ", ""); m.PhoneMobile1 = (PhoneMobile1Input.Text == "") ? null : PhoneMobile1Input.Text.Replace(" ", ""); m.PhoneMobile2 = (PhoneMobile2Input.Text == "") ? null : PhoneMobile2Input.Text.Replace(" ", ""); m.Iban = (IbanInput.Text == "") ? null : IbanInput.Text.Replace(" ", ""); m.Bic = (BicInput.Text == "") ? null : BicInput.Text; m.UstId = (UstIdInput.Text == "") ? null : UstIdInput.Text; m.LfbisNr = (LfbisNrInput.Text == "") ? null : LfbisNrInput.Text; m.Buchführend = BuchführendInput.IsChecked ?? false; // TODO Rechnungsadresse m.EntryDate = (EntryDateInput.Text == "") ? null : string.Join("-", EntryDateInput.Text.Split(".").Reverse()); m.ExitDate = (ExitDateInput.Text == "") ? null : string.Join("-", ExitDateInput.Text.Split(".").Reverse()); m.BusinessShares = (BusinessSharesInput.Text == "") ? 0 : int.Parse(BusinessSharesInput.Text); m.AccountingNr = (AccountingNrInput.Text == "") ? null : AccountingNrInput.Text; m.Active = ActiveInput.IsChecked ?? false; m.VollLieferant = VollLieferantInput.IsChecked ?? false; m.Funktionär = FunkionärInput.IsChecked ?? false; m.ZwstId = ((Branch)BranchInput.SelectedItem).ZwstId; m.DefaultKgNr = ((AT_Kg)DefaultKgInput.SelectedItem).KgNr; m.Comment = (CommentInput.Text == "") ? null : CommentInput.Text; if (ContactPostInput.IsChecked ?? false) m.DefaultContact = "post"; if (ContactEmailInput.IsChecked ?? false) m.DefaultContact = "email"; try { if (IsEditing) Context.Update(m); else if (IsCreating) Context.Add(m); Context.SaveChanges(); if (newMgNr != m.MgNr) Context.Database.ExecuteSql($"UPDATE member SET mgnr = {newMgNr} WHERE mgnr = {m.MgNr}"); } catch (Exception exc) { var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; MessageBox.Show(str, "Mitglied aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error); } IsEditing = false; IsCreating = false; MemberList.IsEnabled = true; HideSaveResetCancelButtons(); ShowNewEditDeleteButtons(); LockInputs(); UnlockSearchInputs(); RefreshMemberList(); } private void ResetButton_Click(object sender, RoutedEventArgs e) { if (IsEditing) { RefreshInputs(); } else if (IsCreating) { InitInputs(); } UpdateButtons(); } private void CancelButton_Click(object sender, RoutedEventArgs e) { IsEditing = false; IsCreating = false; MemberList.IsEnabled = true; HideSaveResetCancelButtons(); ShowNewEditDeleteButtons(); RefreshInputs(); LockInputs(); UnlockSearchInputs(); } private void AreaCommitmentButton_Click(object sender, RoutedEventArgs e) { var w = new AreaCommitmentListWindow((Member) MemberList.SelectedItem); w.Show(); } private void SearchInput_TextChanged(object sender, RoutedEventArgs e) { TextFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(s => s != ""); RefreshMemberList(); } private void FocusSearchInput(object sender, RoutedEventArgs e) { if (!IsEditing && !IsCreating) { SearchInput.Focus(); SearchInput.SelectAll(); } } private int NextMgNr() { int c = Context.Members.Select(m => m.MgNr).Min(); Context.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; }); return c + 1; } private void ShowSaveResetCancelButtons() { SaveButton.IsEnabled = false; ResetButton.IsEnabled = false; CancelButton.IsEnabled = true; SaveButton.Visibility = Visibility.Visible; ResetButton.Visibility = Visibility.Visible; CancelButton.Visibility = Visibility.Visible; } private void HideSaveResetCancelButtons() { SaveButton.IsEnabled = false; ResetButton.IsEnabled = false; CancelButton.IsEnabled = false; SaveButton.Visibility = Visibility.Hidden; ResetButton.Visibility = Visibility.Hidden; CancelButton.Visibility = Visibility.Hidden; } private void ShowNewEditDeleteButtons() { NewMemberButton.IsEnabled = true; EditMemberButton.IsEnabled = MemberList.SelectedItem != null; DeleteMemberButton.IsEnabled = MemberList.SelectedItem != null; NewMemberButton.Visibility = Visibility.Visible; EditMemberButton.Visibility = Visibility.Visible; DeleteMemberButton.Visibility = Visibility.Visible; } private void HideNewEditDeleteButtons() { NewMemberButton.IsEnabled = false; EditMemberButton.IsEnabled = false; DeleteMemberButton.IsEnabled = false; NewMemberButton.Visibility = Visibility.Hidden; EditMemberButton.Visibility = Visibility.Hidden; DeleteMemberButton.Visibility = Visibility.Hidden; } private void LockInputs() { foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) tb.IsReadOnly = true; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.IsEnabled = false; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.IsEnabled = false; foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) rb.IsEnabled = false; } private void UnlockInputs() { foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) tb.IsReadOnly = false; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.IsEnabled = true; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.IsEnabled = true; foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) rb.IsEnabled = true; } private void LockSearchInputs() { SearchInput.IsEnabled = false; ActiveMemberInput.IsEnabled = false; } private void UnlockSearchInputs() { SearchInput.IsEnabled = true; ActiveMemberInput.IsEnabled = true; } private void FillInputs(Member m) { OriginalValues.Clear(); MgNrInput.Text = m.MgNr.ToString(); PredecessorMgNrInput.Text = m.PredecessorMgNr.ToString(); PrefixInput.Text = m.Prefix; GivenNameInput.Text = m.GivenName; FamilyNameInput.Text = m.FamilyName; SuffixInput.Text = m.Suffix; BirthdayInput.Text = (m.Birthday != null) ? string.Join(".", m.Birthday.Split("-").Reverse()) : null; AddressInput.Text = m.Address; AT_Plz? p = m.PostalDest.Plz(Context); if (p != null) { PlzInput.Text = p.Plz.ToString(); OrtInput.ItemsSource = p.Orte(Context); OrtInput.SelectedItem = p; } else { PlzInput.Text = null; OrtInput.ItemsSource = null; OrtInput.SelectedItem = null; } EmailInput.Text = m.Email; PhoneLandlineInput.Text = m.PhoneLandline; PhoneMobile1Input.Text = m.PhoneMobile1; PhoneMobile2Input.Text = m.PhoneMobile2; IbanInput.Text = m.Iban; BicInput.Text = m.Bic; UstIdInput.Text = m.UstId; LfbisNrInput.Text = m.LfbisNr; BuchführendInput.IsChecked = m.Buchführend; // TODO Rechnungsadresse EntryDateInput.Text = (m.EntryDate != null) ? string.Join(".", m.EntryDate.Split("-").Reverse()) : null; ExitDateInput.Text = (m.ExitDate != null) ? string.Join(".", m.ExitDate.Split("-").Reverse()) : null; BusinessSharesInput.Text = m.BusinessShares.ToString(); AccountingNrInput.Text = m.AccountingNr; BranchInput.SelectedItem = m.Branch; DefaultKgInput.SelectedItem = m.DefaultKg; CommentInput.Text = m.Comment; ActiveInput.IsChecked = m.Active; VollLieferantInput.IsChecked = m.VollLieferant; FunkionärInput.IsChecked = m.Funktionär; switch (m.DefaultContact) { case "post": ContactPostInput.IsChecked = true; break; case "email": ContactEmailInput.IsChecked = true; break; } foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) OriginalValues[tb] = tb.Text; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) OriginalValues[cb] = cb.SelectedItem; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null; foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) OriginalValues[rb] = (rb.IsChecked ?? false) ? bool.TrueString : null; } private void ClearInputs() { OriginalValues.Clear(); foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) tb.Text = ""; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.SelectedItem = null; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.IsChecked = false; foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) rb.IsChecked = false; } private bool IsValid() { return Valid.All(kv => kv.Value) && Utils.FindVisualChilds(this, ExemptInputs).All(cb => cb.ItemsSource == null || cb.SelectedItem != null); } private void UpdateButtons() { if (!IsEditing && !IsCreating) return; bool ch = HasChanged(), v = IsValid(); ResetButton.IsEnabled = (ch); SaveButton.IsEnabled = (v && ch); } private bool InputHasChanged(Control input) { if (!OriginalValues.ContainsKey(input)) { return false; } else if (input is TextBox tb) { return OriginalValues[tb]?.ToString() != tb.Text; } else if (input is ComboBox sb) { return OriginalValues[sb] != sb.SelectedItem; } else if (input is CheckBox cb) { return (OriginalValues[cb] != null) != (cb.IsChecked ?? false); } else if (input is RadioButton rb) { return (OriginalValues[rb] != null) != (rb.IsChecked ?? false); } else { return false; } } private bool HasChanged() { return !IsValid() || Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged) || Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged) || Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged) || Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged); } private void UpdatePlz(TextBox plzInput, ComboBox ortInput) { if (plzInput.Text.Length == 4) { int plz = int.Parse(plzInput.Text); ortInput.ItemsSource = Context.Postleitzahlen.Where(p => p.Plz == plz).ToHashSet(); } else { ortInput.ItemsSource = null; } ortInput.SelectedItem = null; Valid[plzInput] = (ortInput.ItemsSource != null); UpdateButtons(); } private void InputTextChanged(TextBox input, bool optional, Func checker) { var res = checker(input, optional); Valid[input] = res.IsValid; if (res.IsValid) { if (InputHasChanged(input)) { Utils.SetInputChanged(input); } else { Utils.ClearInputState(input); } } else { Utils.SetInputInvalid(input); } UpdateButtons(); } private void InputLostFocus(TextBox input, bool optional, Func checker, string? msg) { var res = checker(input, optional); if (!res.IsValid) MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); } private void CheckBox_Changed(object sender, RoutedEventArgs e) { var input = (CheckBox)sender; if (InputHasChanged(input)) { Utils.SetInputChanged(input); } else { Utils.ClearInputState(input); } UpdateButtons(); } private void RadioButton_Changed(object sender, RoutedEventArgs e) { var input = (RadioButton)sender; if (InputHasChanged(input)) { Utils.SetInputChanged(input); } else { Utils.ClearInputState(input); } UpdateButtons(); } private void TextBox_TextChanged(object sender, RoutedEventArgs e) { var input = (TextBox)sender; if (InputHasChanged(input)) { Utils.SetInputChanged(input); } else { Utils.ClearInputState(input); } UpdateButtons(); } private void ComboBox_SelectionChanged(object sender, RoutedEventArgs e) { var input = (ComboBox)sender; if (InputHasChanged(input)) { // TODO not working Utils.SetInputChanged(input); } else { Utils.ClearInputState(input); } UpdateButtons(); } private void PlzInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, false, Validator.CheckPlz); UpdatePlz((TextBox)sender, OrtInput); } private void PlzInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckPlz, null); UpdatePlz((TextBox)sender, OrtInput); } private void PhoneNrInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, true, Validator.CheckPhoneNumber); } private void PhoneNrInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckPhoneNumber, null); } private void EmailInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, true, Validator.CheckEmailAddress); } private void EmailInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckEmailAddress, null); } private void IbanInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, true, Validator.CheckIban); } private void IbanInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckIban, null); } private void BicInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, true, Validator.CheckBic); } private void BicInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckBic, null); } private void LfbisNrInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, true, Validator.CheckLfbisNr); } private void LfbisNrInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckLfbisNr, "Betriebsnummer ungültig"); } private void BillingPlzInput_TextChanged(object sender, RoutedEventArgs e) { InputTextChanged((TextBox)sender, true, Validator.CheckPlz); UpdatePlz((TextBox)sender, BillingOrtInput); } private void BillingPlzInput_LostFocus(object sender, RoutedEventArgs e) { InputLostFocus((TextBox)sender, true, Validator.CheckPlz, null); UpdatePlz((TextBox)sender, BillingOrtInput); } } }