// Copyright (c) 2025 - schick Informatik
// Description: Display control of formsheet Tab 7. Crew data departure
//
using bsmd.database;
using ClosedXML.Excel;
using ENI2.EditControls;
using ENI2.Util;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace ENI2.SheetDisplayControls
{
///
/// Interaction logic for CrewDepartureControl.xaml
///
public partial class CrewDepartureControl : DetailBaseControl
{
#region Fields
private Message _crewaMessage;
private Message _crewdMessage;
#endregion
#region Construction
public CrewDepartureControl()
{
InitializeComponent();
}
#endregion
#region public overrides
public override void Initialize()
{
base.Initialize();
foreach (Message aMessage in this.Messages)
{
if (aMessage.MessageNotificationClass == Message.NotificationClass.CREWA) { this._crewaMessage = aMessage; this.ControlMessages.Add(aMessage); }
if (aMessage.MessageNotificationClass == Message.NotificationClass.CREWD) { this._crewdMessage = aMessage; this.ControlMessages.Add(aMessage); }
}
#region init CREWD
if (this._crewdMessage == null)
{
this._crewdMessage = this.Core.CreateMessage(Message.NotificationClass.CREWD);
this.Messages.Add(this._crewdMessage);
}
this.dataGridCrewListDeparture.Initialize();
this.dataGridCrewListDeparture.ItemsSource = this._crewdMessage.Elements;
this.dataGridCrewListDeparture.AddingNewItem += DataGridCrewListDeparture_AddingNewItem;
this.dataGridCrewListDeparture.EditRequested += DataGridCrewListDeparture_EditRequested;
this.dataGridCrewListDeparture.DeleteRequested += DataGridCrewListDeparture_DeleteRequested;
this.dataGridCrewListDeparture.CreateRequested += DataGridCrewListDeparture_CreateRequested;
this.dataGridCrewListDeparture.RefreshGrid += DataGridCrewListDeparture_RefreshGrid;
this.dataGridCrewListDeparture.MultiEditRequested += DataGridCrewListDeparture_MultiEditRequested;
if (this._crewdMessage.Elements.Count > 0)
{
this.checkBoxCrewNotificationSchengenDeparture.IsChecked = ((CREWD)this._crewdMessage.Elements[0]).NotificationSchengen;
this.checkBoxCrewNotificationPAXDeparture.IsChecked = ((CREWD)this._crewdMessage.Elements[0]).NotificationPAX;
}
this.textBlockNumCrewEntriesDeparture.DataContext = this._crewdMessage;
// extra menu copy to CREWA
{
this.dataGridCrewListDeparture.ContextMenu.Items.Add(new Separator());
MenuItem copyCREWDItem = new MenuItem();
copyCREWDItem.Header = Properties.Resources.textCopyToCREWA;
copyCREWDItem.Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/documents.png")) };
copyCREWDItem.Click += CopyCREWDItem_Click; ;
this.dataGridCrewListDeparture.ContextMenu.Items.Add(copyCREWDItem);
}
#endregion
}
public override void SetEnabled(bool enabled)
{
base.SetEnabled(enabled);
this.crewDepartureGroupBox.IsEnabled = enabled;
}
#endregion
#region event handler
private void checkBoxCrewNotificationSchengenDeparture_Click(object sender, RoutedEventArgs e)
{
foreach (CREWD crewd in _crewdMessage.Elements.Cast())
{
crewd.NotificationSchengen = checkBoxCrewNotificationSchengenDeparture.IsChecked;
}
this.SublistElementChanged(Message.NotificationClass.CREWD);
}
private void checkBoxCrewNotificationPAXDeparture_Click(object sender, RoutedEventArgs e)
{
foreach (CREWD crewd in _crewdMessage.Elements.Cast())
{
crewd.NotificationSchengen = checkBoxCrewNotificationSchengenDeparture.IsChecked;
}
this.SublistElementChanged(Message.NotificationClass.CREWD);
}
private void buttonImportExcelCrewDeparture_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx"
};
if (ofd.ShowDialog() ?? false)
{
try
{
using (var workbook = new XLWorkbook(ofd.FileName))
{
var worksheet = workbook.Worksheet(1); // Get first worksheet
var rows = worksheet.RangeUsed().RowsUsed().Skip(1); // Skip header row if present
List importCrew = new List();
foreach (var row in rows)
{
if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break;
// Check if we have at least 13 columns
if (worksheet.RangeUsed().ColumnCount() < 13)
{
throw new InvalidDataException("Sheet must have 13 columns of data");
}
CREWD crew = new CREWD();
crew.IsDeparture = true;
// Check if first two cells are empty
if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue;
if (!row.Cell(1).IsEmpty()) crew.CrewMemberLastName = row.Cell(1).GetString().Clean();
if (crew.CrewMemberLastName?.Equals("Family Name") == true || string.IsNullOrWhiteSpace(crew.CrewMemberLastName)) continue;
if (!row.Cell(2).IsEmpty()) crew.CrewMemberFirstName = row.Cell(2).GetString().Clean();
if (!row.Cell(3).IsEmpty()) crew.CrewMemberGender = GlobalStructures.ParseGender(row.Cell(3).GetString());
if (!row.Cell(4).IsEmpty()) crew.CrewMemberDuty = row.Cell(4).GetString().Clean();
if (!row.Cell(5).IsEmpty()) crew.CrewMemberNationality = row.Cell(5).GetString().Substring(0, 2).ToUpper();
if (!row.Cell(6).IsEmpty()) crew.CrewMemberPlaceOfBirth = row.Cell(6).GetString().Clean();
if (!row.Cell(7).IsEmpty()) crew.CrewMemberCountryOfBirth = row.Cell(7).GetString().Substring(0, 2).ToUpper();
if (!row.Cell(8).IsEmpty()) crew.CrewMemberDateOfBirth = row.Cell(8).GetDateTime();
if (!row.Cell(9).IsEmpty()) crew.CrewMemberIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(row.Cell(9).GetString());
if (!row.Cell(10).IsEmpty()) crew.CrewMemberIdentityDocumentId = row.Cell(10).GetString().Clean();
if (!row.Cell(11).IsEmpty()) crew.CrewMemberIdentityDocumentIssuingState = row.Cell(11).GetString().Substring(0, 2).ToUpper();
if (!row.Cell(12).IsEmpty()) crew.CrewMemberIdentityDocumentExpiryDate = row.Cell(12).GetDateTime();
if (!row.Cell(13).IsEmpty()) crew.CrewMemberVisaNumber = row.Cell(13).GetString().Clean();
crew.MessageHeader = this._crewdMessage;
crew.IsDirty = true;
crew.Identifier = CREWD.GetNewIdentifier(this._crewdMessage.Elements);
this._crewdMessage.Elements.Add(crew);
importCrew.Add(crew);
}
if (importCrew.Count > 0)
{
this.dataGridCrewListDeparture.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.CREWD);
MessageBox.Show(String.Format(Properties.Resources.textCrewImported, importCrew.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private async void buttonDeleteAllCrewD_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show(Properties.Resources.textConfimDeleteAllEntries, Properties.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
foreach (CREWD crewd in this._crewdMessage.Elements.Cast())
{
await DBManagerAsync.DeleteAsync(crewd);
}
this._crewdMessage.Elements.Clear();
this.dataGridCrewListDeparture.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.CREWD);
}
}
private void CopyCREWDItem_Click(object sender, RoutedEventArgs e)
{
if (this.dataGridCrewListDeparture.SelectedItems != null)
{
foreach (CREWD crewd in this.dataGridCrewListDeparture.SelectedItems)
{
CREW crewa = new CREW();
crewa.MessageHeader = this._crewaMessage;
crewa.CopyFromCREW(crewd);
crewa.IsDeparture = false;
crewa.Identifier = DatabaseEntity.GetNewIdentifier(this._crewaMessage.Elements);
this._crewaMessage.Elements.Add(crewa);
this.SublistElementChanged(Message.NotificationClass.CREWA);
}
}
}
#endregion
#region crew grid departure
private void DataGridCrewListDeparture_CreateRequested()
{
EditCREWDialog ecd = new EditCREWDialog
{
CREW = new CREWD()
};
ecd.CREW.IsDeparture = true;
ecd.CREW.Identifier = CREWD.GetNewIdentifier(_crewdMessage.Elements);
ecd.CREW.MessageHeader = this._crewdMessage;
ecd.AddClicked += () =>
{
ecd.CopyValuesToEntity();
if (!this._crewdMessage.Elements.Contains(ecd.CREW))
{
this._crewdMessage.Elements.Add(ecd.CREW);
this.CheckCREWD();
}
this.dataGridCrewListDeparture.Items.Refresh();
ecd.CREW = new CREWD
{
IsDeparture = true,
MessageHeader = this._crewdMessage,
Identifier = CREWD.GetNewIdentifier(_crewdMessage.Elements)
};
this.SublistElementChanged(Message.NotificationClass.CREWD);
};
if (ecd.ShowDialog() ?? false)
{
if (!this._crewdMessage.Elements.Contains(ecd.CREW))
{
_crewdMessage.Elements.Add(ecd.CREW);
this.CheckCREWD();
}
this.dataGridCrewListDeparture.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.CREWD);
}
}
private void DataGridCrewListDeparture_DeleteRequested(DatabaseEntity obj)
{
if (obj is CREWD crewd)
{
// are you sure dialog is in base class
_crewdMessage.Elements.Remove(crewd);
DBManager.Instance.Delete(crewd);
}
}
private void DataGridCrewListDeparture_RefreshGrid()
{
DatabaseEntity.ResetIdentifiers(_crewdMessage.Elements);
this.SublistElementChanged(Message.NotificationClass.CREWD);
this.dataGridCrewListDeparture.Items.Refresh();
}
private void DataGridCrewListDeparture_EditRequested(DatabaseEntity obj)
{
EditCREWDialog ecd = new EditCREWDialog
{
CREW = obj as CREWD
};
ecd.AddClicked += () =>
{
ecd.CopyValuesToEntity();
if (!_crewdMessage.Elements.Contains(ecd.CREW))
{
_crewdMessage.Elements.Add(ecd.CREW);
this.CheckCREWD();
}
this.dataGridCrewListDeparture.Items.Refresh();
ecd.CREW = new CREWD
{
IsDeparture = true,
Identifier = CREWD.GetNewIdentifier(_crewdMessage.Elements),
MessageHeader = _crewdMessage
};
this.SublistElementChanged(Message.NotificationClass.CREWD);
};
if (ecd.ShowDialog() ?? false)
{
if (!_crewdMessage.Elements.Contains(ecd.CREW))
{
_crewdMessage.Elements.Add(ecd.CREW);
this.CheckCREWD();
}
ecd.CREW.IsDirty = true;
this.dataGridCrewListDeparture.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.CREWD);
}
}
private void DataGridCrewListDeparture_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
this.DataGridCrewListDeparture_CreateRequested();
}
private void DataGridCrewListDeparture_MultiEditRequested(List databaseEntities)
{
List crewList = new List();
foreach (CREW acrew in databaseEntities.Cast())
crewList.Add(acrew);
// write common values of all CREW entities to template entity
CREW crew = CREW.CreateCommon(crewList);
EditCREWDialog dialog = new EditCREWDialog();
dialog.CREW = crew;
dialog.AddVisible = false;
if (dialog.ShowDialog() ?? false)
{
// write back changed values from crew to all entities and mark them as changed
CREW.WriteTemplateToList(crew, crewList);
this.SublistElementChanged(Message.NotificationClass.CREWD);
this.dataGridCrewListDeparture.Items.Refresh();
}
}
#endregion
#region private methods
private void CheckCREWD()
{
if (this._crewdMessage.Elements.Count == 0) return;
CREWD firstCREW = this._crewdMessage.Elements[0] as CREWD;
if (this._crewdMessage.Elements.Count == 1)
{
firstCREW.NotificationSchengen = true;
this.checkBoxCrewNotificationSchengenDeparture.IsChecked = true;
}
else
{
for (int i = 1; i < this._crewdMessage.Elements.Count; i++)
{
((CREWD)this._crewdMessage.Elements[i]).NotificationSchengen = firstCREW.NotificationSchengen;
}
}
}
#endregion
}
}