// Copyright (c) 2023 schick Informatik // Description: Terminals have all different fields so a different dialog // using BreCalClient.misc.Model; using System; using System.Windows; namespace BreCalClient { /// /// Interaction logic for EditTimesTerminalControl.xaml /// public partial class EditTimesTerminalControl : Window, IEditTimesControl { public EditTimesTerminalControl() { InitializeComponent(); } #region Properties public Times Times { get; set; } = new(); public ShipcallControlModel ShipcallModel { get; set; } = new(); #endregion #region event handler private void Window_Loaded(object sender, RoutedEventArgs e) { this.comboBoxBerth.ItemsSource = BreCalLists.Berths; this.CopyToControls(); this.EnableControls(); } private void contextMenuItemClearOperationStart_Click(object sender, RoutedEventArgs e) { this.datePickerOperationStart.Value = null; } private void contextMenuItemClearOperationEnd_Click(object sender, RoutedEventArgs e) { this.datePickerOperationEnd.Value = null; } private void contextMenuItemClearOperationStart_End_Click(object sender, RoutedEventArgs e) { this.datePickerOperationStart_End.Value = null; } private void contextMenuItemClearOperationEnd_End_Click(object sender, RoutedEventArgs e) { this.datePickerOperationEnd_End.Value = null; } private void contextMenuItemBerth_Click(object sender, RoutedEventArgs e) { this.comboBoxBerth.SelectedIndex -= 1; } private void buttonOK_Click(object sender, RoutedEventArgs e) { if (!CheckValues(out string message)) { System.Windows.MessageBox.Show(message, BreCalClient.Resources.Resources.textWarning, MessageBoxButton.OK, MessageBoxImage.Warning); } else { this.CopyToModel(); this.DialogResult = true; this.Close(); } } private void buttonCancel_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; this.Close(); } private void contextMenuItemClearPierside_Click(object sender, RoutedEventArgs e) { this.comboBoxPierside.SelectedIndex = -1; } private void buttonClearAll_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show(BreCalClient.Resources.Resources.textClearAll, BreCalClient.Resources.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { this.datePickerOperationStart.Value = null; this.datePickerOperationStart_End.Value = null; this.datePickerOperationEnd.Value = null; this.datePickerOperationEnd_End.Value = null; this.comboBoxBerth.SelectedIndex = -1; this.comboBoxPierside.SelectedIndex = -1; this.textBoxRemarks.Text = null; this.textBoxBerthRemarks.Text = null; } } #endregion #region private methods private bool CheckValues(out string message) { message = ""; if (this.datePickerOperationStart.Value.HasValue && (this.datePickerOperationStart.Value.Value < DateTime.Now) && (this.datePickerOperationStart_End.Value == null)) { message = BreCalClient.Resources.Resources.textOperationStartInThePast; return false; } if (this.datePickerOperationStart_End.Value.HasValue && this.datePickerOperationStart_End.Value < DateTime.Now) { message = BreCalClient.Resources.Resources.textOperationStartInThePast; return false; } if (this.datePickerOperationStart.Value.HasValue && this.datePickerOperationStart_End.Value.HasValue && this.datePickerOperationStart.Value > this.datePickerOperationStart_End.Value) { message = BreCalClient.Resources.Resources.textEndValueBeforeStartValue; return false; } if (this.datePickerOperationEnd.Value.HasValue && (this.datePickerOperationEnd.Value.Value < DateTime.Now) && (this.datePickerOperationEnd_End.Value == null)) { message = BreCalClient.Resources.Resources.textOperationEndInThePast; return false; } if (this.datePickerOperationEnd_End.Value.HasValue && this.datePickerOperationEnd_End.Value < DateTime.Now) { message = BreCalClient.Resources.Resources.textOperationEndInThePast; return false; } if (this.datePickerOperationEnd.Value.HasValue && this.datePickerOperationEnd_End.Value.HasValue && this.datePickerOperationEnd.Value > this.datePickerOperationEnd_End.Value) { message = BreCalClient.Resources.Resources.textEndValueBeforeStartValue; return false; } if(this.datePickerOperationEnd.Value.IsTooFar() || this.datePickerOperationEnd_End.Value.IsTooFar() || this.datePickerOperationStart.Value.IsTooFar() || this.datePickerOperationStart_End.Value.IsTooFar()) { message = BreCalClient.Resources.Resources.textTooFarInTheFuture; return false; } if((this.datePickerOperationEnd_End.Value.HasValue && !this.datePickerOperationEnd.Value.HasValue) || (this.datePickerOperationStart_End.Value.HasValue && !this.datePickerOperationStart.Value.HasValue)) { message = BreCalClient.Resources.Resources.textStartTimeMissing; return false; } return true; } private void CopyToModel() { this.Times.PierSide = this.comboBoxPierside.SelectedIndex switch { 0 => true, 1 => false, _ => null, }; this.Times.OperationsStart = this.datePickerOperationStart.Value; this.Times.OperationsEnd = this.datePickerOperationEnd.Value; this.Times.EtaIntervalEnd = this.datePickerOperationStart_End.Value; this.Times.EtdIntervalEnd = this.datePickerOperationEnd_End.Value; this.Times.BerthId = (this.comboBoxBerth.SelectedItem != null) ? ((Berth)this.comboBoxBerth.SelectedItem).Id : null; this.Times.Remarks = this.textBoxRemarks.Text.Trim(); this.Times.BerthInfo = this.textBoxBerthRemarks.Text.Trim(); } private void CopyToControls() { this.datePickerOperationStart.Value = this.Times.OperationsStart; this.datePickerOperationEnd.Value = this.Times.OperationsEnd; this.datePickerOperationStart_End.Value = this.Times.EtaIntervalEnd; this.datePickerOperationEnd_End.Value = this.Times.EtdIntervalEnd; if(this.Times.PierSide == null) { this.comboBoxPierside.SelectedIndex = -1; } else this.comboBoxPierside.SelectedIndex = (this.Times.PierSide ?? false) ? 0 : 1; this.comboBoxBerth.SelectedValue = this.Times.BerthId; this.textBoxRemarks.Text = this.Times.Remarks; this.textBoxBerthRemarks.Text = this.Times.BerthInfo; switch (ShipcallModel.Shipcall?.Type) { case ShipcallType.Arrival: this.labelEnd.Visibility = Visibility.Hidden; this.datePickerOperationEnd.Visibility = Visibility.Hidden; this.datePickerOperationEnd_End.Visibility = Visibility.Hidden; this.rowEnd.Height = new(0); break; case ShipcallType.Departure: this.rowStart.Height = new(0); this.labelBerth.Visibility = Visibility.Hidden; this.comboBoxBerth.Visibility= Visibility.Hidden; this.labelPierside.Visibility = Visibility.Hidden; this.comboBoxPierside.Visibility = Visibility.Hidden; this.labelBerthRemarks.Visibility = Visibility.Hidden; this.textBoxBerthRemarks.Visibility = Visibility.Hidden; break; case ShipcallType.Shifting: this.rowStart.Height = new(0); this.labelBerth.Visibility = Visibility.Hidden; this.comboBoxBerth.Visibility = Visibility.Hidden; this.labelPierside.Visibility = Visibility.Hidden; this.comboBoxPierside.Visibility = Visibility.Hidden; this.labelBerthRemarks.Visibility = Visibility.Hidden; this.textBoxBerthRemarks.Visibility = Visibility.Hidden; break; } } private void EnableControls() { if (this.Times.ParticipantId != App.Participant.Id) { this.buttonOK.IsEnabled = false; return; } this.datePickerOperationStart.IsEnabled = ShipcallModel.Shipcall?.Type == ShipcallType.Arrival; this.datePickerOperationStart_End.IsEnabled = ShipcallModel.Shipcall?.Type == ShipcallType.Arrival; this.datePickerOperationEnd.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Departure) || (ShipcallModel.Shipcall?.Type == ShipcallType.Shifting); this.datePickerOperationEnd_End.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Departure) || (ShipcallModel.Shipcall?.Type == ShipcallType.Shifting); this.comboBoxBerth.IsEnabled = ShipcallModel.Shipcall?.Type == ShipcallType.Arrival; this.comboBoxPierside.IsEnabled = ShipcallModel.Shipcall?.Type == ShipcallType.Arrival; this.textBoxBerthRemarks.IsReadOnly = ShipcallModel.Shipcall?.Type != ShipcallType.Arrival; this.textBoxRemarks.IsReadOnly = false; this.buttonClearAll.IsEnabled = true; } #endregion } }