From cc0843f183673749724c2de2861febb6be9e096d Mon Sep 17 00:00:00 2001
From: Lorenz Stechauner <lorenz.stechauner@necronda.net>
Date: Mon, 10 Jun 2024 16:22:28 +0200
Subject: [PATCH] [#45] MemberAdminWindow: Add dialog for area commitment
 cancellation/transfer

---
 Elwig/Dialogs/AreaComDialog.xaml        | 74 +++++++++++++++++++++++++
 Elwig/Dialogs/AreaComDialog.xaml.cs     | 52 +++++++++++++++++
 Elwig/Windows/MemberAdminWindow.xaml.cs | 27 +++++----
 3 files changed, 139 insertions(+), 14 deletions(-)
 create mode 100644 Elwig/Dialogs/AreaComDialog.xaml
 create mode 100644 Elwig/Dialogs/AreaComDialog.xaml.cs

diff --git a/Elwig/Dialogs/AreaComDialog.xaml b/Elwig/Dialogs/AreaComDialog.xaml
new file mode 100644
index 0000000..7f18f3d
--- /dev/null
+++ b/Elwig/Dialogs/AreaComDialog.xaml
@@ -0,0 +1,74 @@
+<Window x:Class="Elwig.Dialogs.AreaComDialog"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:Elwig.Dialogs"
+        xmlns:ctrl="clr-namespace:Elwig.Controls"
+        ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True"
+        WindowStartupLocation="CenterOwner"
+        DataContext="{Binding RelativeSource={RelativeSource Self}}"
+        Title="Flächenbindungen übertragen" Height="230" Width="450">
+    <Window.Resources>
+        <Style TargetType="Label">
+            <Setter Property="HorizontalAlignment" Value="Left"/>
+            <Setter Property="VerticalAlignment" Value="Top"/>
+            <Setter Property="Padding" Value="2,4,2,4"/>
+            <Setter Property="Height" Value="25"/>
+        </Style>
+        <Style TargetType="TextBox">
+            <Setter Property="HorizontalAlignment" Value="Stretch"/>
+            <Setter Property="VerticalAlignment" Value="Top"/>
+            <Setter Property="FontSize" Value="14"/>
+            <Setter Property="Padding" Value="2"/>
+            <Setter Property="Height" Value="25"/>
+            <Setter Property="TextWrapping" Value="NoWrap"/>
+        </Style>
+        <Style TargetType="Button">
+            <Setter Property="HorizontalAlignment" Value="Right"/>
+            <Setter Property="VerticalAlignment" Value="Bottom"/>
+            <Setter Property="Width" Value="100"/>
+            <Setter Property="Height" Value="25"/>
+        </Style>
+    </Window.Resources>
+    <Grid>
+        <TextBlock x:Name="QuestionBlock1" TextAlignment="Center" Margin="0,10,0,0"
+                   HorizontalAlignment="Center" VerticalAlignment="Top">
+            Sollen die aktiven Flächenbindungen des angegebenen Vorgängers<LineBreak/>
+            übernommen werden? (<Run Text="{Binding AreaComNum}"/> FB, <Run Text="{Binding Area}"/> m²)
+        </TextBlock>
+        <TextBlock x:Name="QuestionBlock2" TextAlignment="Center" Margin="0,10,0,0" Visibility="Hidden"
+                   HorizontalAlignment="Center" VerticalAlignment="Top">
+            Sollen die aktiven Flächenbindungen gekündigt werden? (<Run Text="{Binding AreaComNum}"/> FB, <Run Text="{Binding Area}"/> m²)
+        </TextBlock>
+
+        <Label x:Name="SeasonLabel" Content="Saison:" Margin="0,50,100,0"
+               HorizontalAlignment="Center" VerticalAlignment="Top"/>
+        <ctrl:IntegerUpDown x:Name="SeasonInput" Width="56" Height="25" Margin="0,50,0,0" FontSize="14"
+                            Minimum="1900" Maximum="9999"
+                            HorizontalAlignment="Center" VerticalAlignment="Top"
+                            TextChanged="SeasonInput_TextChanged"/>
+
+        <TextBlock x:Name="DescBlock1" Margin="0,85,0,0" TextAlignment="Center"
+                   HorizontalAlignment="Center" VerticalAlignment="Top">
+            Die Flächenbindungen beim <Bold>Vorgänger</Bold> sind bis inkl. Saison <Bold><Run x:Name="CancelSeason1"/></Bold> gültig,<LineBreak/>
+            und werden beim <Bold>Nachfolger</Bold> ab inkl. Saison <Bold><Run x:Name="TransferSeason"/></Bold> übernommen.
+        </TextBlock>
+        <TextBlock x:Name="DescBlock2" Margin="0,70,0,0" TextAlignment="Center" Visibility="Hidden"
+                   HorizontalAlignment="Center" VerticalAlignment="Top">
+            Die Flächenbindungen sind bis inklusive Saison <Bold><Run x:Name="CancelSeason2"/></Bold> gültig.
+        </TextBlock>
+
+        <TextBlock x:Name="InfoBlock" Margin="0,0,0,75" TextAlignment="Center"
+                   HorizontalAlignment="Center" VerticalAlignment="Bottom">
+            Falls die Flächenbindungen später an ein neues Mitglied<LineBreak/>
+            übertragen werden sollen bitte <Italic>Nein</Italic> auswählen!
+        </TextBlock>
+        <TextBlock Text="Die Änderungen werden erst beim Speichern übernommen!"
+                   HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,50"/>
+
+        <Button x:Name="ConfirmButton" Content="Ja" Margin="10,10,115,10" Grid.Column="1"
+                Click="ConfirmButton_Click"/>
+        <Button x:Name="CancelButton" Content="Nein" Margin="10,10,10,10" Grid.Column="1" IsCancel="True"/>
+    </Grid>
+</Window>
diff --git a/Elwig/Dialogs/AreaComDialog.xaml.cs b/Elwig/Dialogs/AreaComDialog.xaml.cs
new file mode 100644
index 0000000..a725db6
--- /dev/null
+++ b/Elwig/Dialogs/AreaComDialog.xaml.cs
@@ -0,0 +1,52 @@
+using Elwig.Helpers;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace Elwig.Dialogs {
+    public partial class AreaComDialog : Window {
+
+        public int CancelSeason { get; set; }
+        public int SuccessorSeason => CancelSeason + 1;
+
+        public string AreaComNum { get; set; }
+        public string Area { get; set; }
+
+        public AreaComDialog(string name, int areaComNum, int area) {
+            CancelSeason = Utils.FollowingSeason - 1;
+            AreaComNum = $"{areaComNum:N0}";
+            Area = $"{area:N0}";
+            InitializeComponent();
+            SeasonInput.Text = $"{CancelSeason}";
+            Title = $"Aktive Flächenbindungen kündigen - {name}";
+            QuestionBlock1.Visibility = Visibility.Hidden;
+            QuestionBlock2.Visibility = Visibility.Visible;
+            DescBlock1.Visibility = Visibility.Hidden;
+            DescBlock2.Visibility = Visibility.Visible;
+            Height = 240;
+            SeasonInput.Margin = new(0, 40, 0, 0);
+            SeasonLabel.Margin = new(0, 40, 100, 0);
+        }
+
+        public AreaComDialog(string name, string successorName, int areaComNum, int area) {
+            CancelSeason = Utils.FollowingSeason - 1;
+            AreaComNum = $"{areaComNum:N0}";
+            Area = $"{area:N0}";
+            InitializeComponent();
+            SeasonInput.Text = $"{CancelSeason}";
+            Title = $"Aktive Flächenbindungen übertragen - {name} - {successorName}";
+            InfoBlock.Visibility = Visibility.Hidden;
+        }
+
+        private void SeasonInput_TextChanged(object sender, TextChangedEventArgs evt) {
+            CancelSeason = (int)SeasonInput.Value!;
+            CancelSeason1.Text = $"{CancelSeason}";
+            CancelSeason2.Text = $"{CancelSeason}";
+            TransferSeason.Text = $"{SuccessorSeason}";
+        }
+
+        private void ConfirmButton_Click(object sender, RoutedEventArgs evt) {
+            DialogResult = true;
+            Close();
+        }
+    }
+}
diff --git a/Elwig/Windows/MemberAdminWindow.xaml.cs b/Elwig/Windows/MemberAdminWindow.xaml.cs
index e1485e4..082c65f 100644
--- a/Elwig/Windows/MemberAdminWindow.xaml.cs
+++ b/Elwig/Windows/MemberAdminWindow.xaml.cs
@@ -1134,7 +1134,7 @@ namespace Elwig.Windows {
             var valid = InputLostFocus((TextBox)sender, Validator.CheckPredecessorMgNr);
             if (valid && PredecessorMgNrInput.Text != "" && (IsEditing || IsCreating)) {
                 var mgnr = int.Parse(PredecessorMgNrInput.Text);
-                if (MemberList.SelectedItem is Member m && m.MgNr == mgnr)
+                if (MemberList.SelectedItem is not Member m || m.MgNr == mgnr)
                     return;
                 using var ctx = new AppDbContext();
                 var areaComs = await ctx.AreaCommitments
@@ -1142,13 +1142,12 @@ namespace Elwig.Windows {
                     .ToListAsync();
                 if (areaComs.Count == 0)
                     return;
-                var res = MessageBox.Show("Sollen die aktiven Flächenbindungen des angegebenen\n" +
-                    $"Vorgängers übernommen werden? ({areaComs.Sum(c => c.Area)} m²)\n\n" +
-                    "Die Änderungen werden erst beim Speichern übernommen!",
-                    "Aktive Flächenbindungen übernehmen", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
-                if (res != MessageBoxResult.Yes)
+
+                var oldMember = (await ctx.Members.FindAsync(mgnr))!;
+                var d = new AreaComDialog(oldMember.AdministrativeName, m.AdministrativeName, areaComs.Count, areaComs.Sum(c => c.Area));
+                if (d.ShowDialog() != true)
                     return;
-                TransferPredecessorAreaComs = Utils.FollowingSeason;
+                TransferPredecessorAreaComs = d.SuccessorSeason;
                 SetOriginalValue(PredecessorMgNrInput, -1);  // hack to allow user to save
                 UpdateButtons();
             }
@@ -1190,18 +1189,18 @@ namespace Elwig.Windows {
         }
 
         private async void ActiveInput_Changed(object sender, RoutedEventArgs evt) {
+            if (MemberList.SelectedItem is not Member m)
+                return;
             if ((IsEditing || IsCreating) && ActiveInput.IsChecked == false && int.TryParse(MgNrInput.Text, out var mgnr)) {
                 using var ctx = new AppDbContext();
                 var areaComs = await ctx.AreaCommitments
                     .Where(c => c.MgNr == mgnr && (c.YearTo == null || c.YearTo >= Utils.FollowingSeason))
                     .ToListAsync();
-                if (areaComs.Count >= 0) {
-                    var res = MessageBox.Show($"Sollen die aktiven Flächenbindungen gekündigt werden? ({areaComs.Sum(c => c.Area)} m²)\n" +
-                        "Falls die Flächenbindungen später an ein neues Mitglied\n" +
-                        "übertragen werden sollen bitte \"Nein\" auswählen!\n\n" +
-                        "Die Änderungen werden erst beim Speichern übernommen!",
-                        "Flächenbindungen kündigen", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
-                    CancelAreaComs = res == MessageBoxResult.Yes ? Utils.FollowingSeason - 1 : null;
+                if (areaComs.Count > 0) {
+                    var d = new AreaComDialog(m.AdministrativeName, areaComs.Count, areaComs.Sum(c => c.Area));
+                    if (d.ShowDialog() == true) {
+                        CancelAreaComs = d.CancelSeason;
+                    }
                 }
             }
             CheckBox_Changed(sender, evt);