Rename ContractWindow to AreaCommAdminWindow
This commit is contained in:
273
Elwig/Windows/AreaCommAdminWindow.xaml.cs
Normal file
273
Elwig/Windows/AreaCommAdminWindow.xaml.cs
Normal file
@ -0,0 +1,273 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System;
|
||||
|
||||
namespace Elwig.Windows {
|
||||
public partial class AreaCommAdminWindow : Window {
|
||||
private readonly AppDbContext Context = new();
|
||||
private readonly Member member;
|
||||
private bool IsEditing = false;
|
||||
private bool IsCreating = false;
|
||||
private IEnumerable<TextBox> TextBoxInputs = Array.Empty<TextBox>();
|
||||
private IEnumerable<ComboBox> ComboBoxInputs = Array.Empty<ComboBox>();
|
||||
private readonly Dictionary<Control, bool> Valid = new();
|
||||
private readonly Dictionary<Control, object?> OriginalValues = new();
|
||||
|
||||
public AreaCommAdminWindow(Member member) {
|
||||
InitializeComponent();
|
||||
this.member = member;
|
||||
this.Title = $"Flächenbindungen von {member.FamilyName} {member.GivenName} - Elwig";
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
||||
KgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList();
|
||||
RdInput.ItemsSource = Context.WbRde.OrderBy(r => r.Name).ToList();
|
||||
}
|
||||
|
||||
private void RefreshAreaCommitmentList() {
|
||||
Context.AreaCommitments.Load();
|
||||
List<AreaCommitment> commitments = Context.AreaCommitments.Where(a => a.Contract.MgNr == member.MgNr).ToList();
|
||||
|
||||
AreaCommitmentList.ItemsSource = commitments;
|
||||
if (commitments.Count == 1)
|
||||
AreaCommitmentList.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
private void RefreshInputs(bool validate = false) {
|
||||
ClearInputStates();
|
||||
AreaCommitment a = (AreaCommitment)AreaCommitmentList.SelectedItem;
|
||||
if (a != null) {
|
||||
//EditMemberButton.IsEnabled = true;
|
||||
//DeleteMemberButton.IsEnabled = true;
|
||||
//AreaCommitmentButton.IsEnabled = true;
|
||||
FillInputs(a);
|
||||
} else {
|
||||
//EditMemberButton.IsEnabled = false;
|
||||
//DeleteMemberButton.IsEnabled = false;
|
||||
//AreaCommitmentButton.IsEnabled = false;
|
||||
ClearInputs();
|
||||
}
|
||||
//if (!validate) ClearInputStates();
|
||||
GC.Collect();
|
||||
}
|
||||
|
||||
private void FillInputs(AreaCommitment a) {
|
||||
OriginalValues.Clear();
|
||||
|
||||
KgInput.SelectedItem = a.Kg.Kg;
|
||||
RdInput.SelectedItem = a.Rd;
|
||||
GstNr.Text = a.GstNr;
|
||||
|
||||
FillOriginalValues();
|
||||
}
|
||||
|
||||
private void FillOriginalValues() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
OriginalValues[tb] = tb.Text;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
OriginalValues[cb] = cb.SelectedItem;
|
||||
}
|
||||
|
||||
private void ClearInputStates() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
Utils.ClearInputState(tb);
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
Utils.ClearInputState(cb);
|
||||
}
|
||||
|
||||
private void ClearInputs() {
|
||||
OriginalValues.Clear();
|
||||
foreach (var tb in TextBoxInputs) {
|
||||
tb.Text = " ";
|
||||
tb.Text = "";
|
||||
}
|
||||
foreach (var cb in ComboBoxInputs) {
|
||||
cb.SelectedItem = null;
|
||||
if (cb.ItemsSource != null)
|
||||
Utils.SetInputInvalid(cb);
|
||||
}
|
||||
}
|
||||
|
||||
private void NewAreaCommitmentButton_Click(object sender, RoutedEventArgs evt) {
|
||||
IsCreating = true;
|
||||
AreaCommitmentList.IsEnabled = false;
|
||||
AreaCommitmentList.SelectedItem = null;
|
||||
HideNewEditDeleteButtons();
|
||||
ShowSaveResetCancelButtons();
|
||||
UnlockInputs();
|
||||
InitInputs();
|
||||
}
|
||||
|
||||
private void EditAreaCommitmentButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (AreaCommitmentList.SelectedItem == null)
|
||||
return;
|
||||
|
||||
IsEditing = true;
|
||||
AreaCommitmentList.IsEnabled = false;
|
||||
|
||||
HideNewEditDeleteButtons();
|
||||
ShowSaveResetCancelButtons();
|
||||
UnlockInputs();
|
||||
}
|
||||
|
||||
private void DeleteAreaCommitmentButton_Click(object sender, RoutedEventArgs evt) {
|
||||
AreaCommitment a = (AreaCommitment)AreaCommitmentList.SelectedItem;
|
||||
if (a == null) return;
|
||||
|
||||
var r = MessageBox.Show(
|
||||
$"Soll die Flächenbindung \"{a.Kg.Kg.Name}, {a.Rd.Name}\", (MgNr. {a.GstNr}) wirklich unwiderruflich gelöscht werden?",
|
||||
"Mitglied löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
||||
if (r == MessageBoxResult.Yes) {
|
||||
Context.Remove(a);
|
||||
Context.SaveChanges();
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveButton_Click(object sender, RoutedEventArgs evt) {
|
||||
AreaCommitment? a = new();
|
||||
if (IsEditing)
|
||||
a = (AreaCommitment)AreaCommitmentList.SelectedItem;
|
||||
else if (IsCreating)
|
||||
a = new();
|
||||
|
||||
a.KgNr = ((AT_Kg)KgInput.SelectedItem).KgNr;
|
||||
a.RdNr = ((WbRd)RdInput.SelectedItem).RdNr;
|
||||
a.GstNr = GstNr.Text;
|
||||
|
||||
try {
|
||||
if (IsEditing)
|
||||
Context.Update(a);
|
||||
else if (IsCreating)
|
||||
Context.Add(a);
|
||||
Context.SaveChanges();
|
||||
} 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, "Flächenbindung aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
|
||||
IsEditing = false;
|
||||
IsCreating = false;
|
||||
AreaCommitmentList.IsEnabled = true;
|
||||
HideSaveResetCancelButtons();
|
||||
ShowNewEditDeleteButtons();
|
||||
LockInputs();
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
|
||||
private void ResetButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (IsEditing) {
|
||||
RefreshInputs();
|
||||
} else if (IsCreating) {
|
||||
InitInputs();
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, RoutedEventArgs evt) {
|
||||
IsEditing = false;
|
||||
IsCreating = false;
|
||||
AreaCommitmentList.IsEnabled = true;
|
||||
HideSaveResetCancelButtons();
|
||||
ShowNewEditDeleteButtons();
|
||||
RefreshInputs();
|
||||
ClearInputStates();
|
||||
LockInputs();
|
||||
}
|
||||
|
||||
private void LockInputs() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
tb.IsReadOnly = true;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.IsEnabled = false;
|
||||
}
|
||||
|
||||
private void UnlockInputs() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
tb.IsReadOnly = false;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.IsEnabled = true;
|
||||
}
|
||||
|
||||
private void UpdateButtons() {
|
||||
if (!IsEditing && !IsCreating) return;
|
||||
bool ch = HasChanged(), v = IsValid();
|
||||
ResetButton.IsEnabled = (ch);
|
||||
SaveButton.IsEnabled = (v && ch);
|
||||
}
|
||||
|
||||
private void InitInputs() {
|
||||
FillOriginalValues();
|
||||
}
|
||||
|
||||
private bool HasChanged() {
|
||||
return !IsValid() ||
|
||||
TextBoxInputs.Any(InputHasChanged) ||
|
||||
ComboBoxInputs.Any(InputHasChanged);
|
||||
}
|
||||
|
||||
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 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsValid() {
|
||||
return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
|
||||
}
|
||||
|
||||
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() {
|
||||
NewAreaCommitmentButton.IsEnabled = true;
|
||||
EditAreaCommitmentButton.IsEnabled = AreaCommitmentList.SelectedItem != null;
|
||||
DeleteAreaCommitmentButton.IsEnabled = AreaCommitmentList.SelectedItem != null;
|
||||
NewAreaCommitmentButton.Visibility = Visibility.Visible;
|
||||
EditAreaCommitmentButton.Visibility = Visibility.Visible;
|
||||
DeleteAreaCommitmentButton.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
private void HideNewEditDeleteButtons() {
|
||||
NewAreaCommitmentButton.IsEnabled = false;
|
||||
EditAreaCommitmentButton.IsEnabled = false;
|
||||
DeleteAreaCommitmentButton.IsEnabled = false;
|
||||
NewAreaCommitmentButton.Visibility = Visibility.Hidden;
|
||||
EditAreaCommitmentButton.Visibility = Visibility.Hidden;
|
||||
DeleteAreaCommitmentButton.Visibility = Visibility.Hidden;
|
||||
}
|
||||
|
||||
private void AreaCommitmentList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
RefreshInputs();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user