138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
// Copyright (c) 2023 schick Informatik
|
|
// Description: Administration screen for ships
|
|
//
|
|
|
|
|
|
using BreCalClient.misc.Api;
|
|
using BreCalClient.misc.Model;
|
|
using System;
|
|
using System.Windows;
|
|
|
|
namespace BreCalClient
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for ShipListDialog.xaml
|
|
/// </summary>
|
|
public partial class ShipListDialog : Window
|
|
{
|
|
public ShipListDialog()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
#region Properties
|
|
|
|
public ShipApi? ShipApi { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Event handler
|
|
|
|
private void buttonClose_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
this.dataGridShips.Initialize();
|
|
this.dataGridShips.ItemsSource = BreCalLists.AllShips;
|
|
|
|
this.dataGridShips.CreateRequested += DataGridShips_CreateRequested;
|
|
this.dataGridShips.EditRequested += DataGridShips_EditRequested;
|
|
this.dataGridShips.DeleteRequested += DataGridShips_DeleteRequested;
|
|
}
|
|
|
|
private async void DataGridShips_DeleteRequested(object obj)
|
|
{
|
|
if (obj is ShipModel shipmodel)
|
|
{
|
|
if (!shipmodel.Ship.Deleted)
|
|
{
|
|
try
|
|
{
|
|
if (this.ShipApi != null)
|
|
await this.ShipApi.ShipDeleteAsync(shipmodel.Ship.Id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
BreCalLists.Ships.Remove(shipmodel); // remove from "selectable" ships
|
|
shipmodel.Ship.Deleted = true; // set deleted marker on working instance
|
|
this.dataGridShips.ItemsSource = null;
|
|
this.dataGridShips.ItemsSource = BreCalLists.AllShips;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async void DataGridShips_EditRequested(object obj)
|
|
{
|
|
if (obj is ShipModel shipmodel)
|
|
{
|
|
EditShipDialog esd = new()
|
|
{
|
|
Ship = shipmodel.Ship
|
|
};
|
|
esd.Participants.AddRange(BreCalLists.Participants_Tug);
|
|
|
|
if (esd.ShowDialog() ?? false)
|
|
{
|
|
try
|
|
{
|
|
if (this.ShipApi != null)
|
|
{
|
|
Id tmpId = await this.ShipApi.ShipUpdateAsync(shipmodel.Ship);
|
|
}
|
|
this.dataGridShips.ItemsSource = null;
|
|
this.dataGridShips.ItemsSource = BreCalLists.AllShips;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private async void DataGridShips_CreateRequested()
|
|
{
|
|
|
|
ShipModel shipModel = new((ShipModel.LastEditShip != null) ? ShipModel.LastEditShip : new Ship());
|
|
|
|
EditShipDialog esd = new()
|
|
{
|
|
Ship = shipModel.Ship
|
|
};
|
|
esd.Participants.AddRange(BreCalLists.Participants_Tug);
|
|
|
|
if(esd.ShowDialog() ?? false)
|
|
{
|
|
try
|
|
{
|
|
if (this.ShipApi != null)
|
|
{
|
|
Id id = await this.ShipApi.ShipsCreateAsync(shipModel.Ship);
|
|
shipModel.Ship.Id = id.VarId;
|
|
this.dataGridShips.ItemsSource = null;
|
|
BreCalLists.AllShips.Add(shipModel);
|
|
BreCalLists.Ships.Add(shipModel);
|
|
if(!BreCalLists.ShipLookupDict.TryAdd(id.VarId, shipModel))
|
|
BreCalLists.ShipLookupDict[id.VarId] = shipModel;
|
|
this.dataGridShips.ItemsSource = BreCalLists.AllShips;
|
|
ShipModel.LastEditShip = null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ShipModel.LastEditShip = shipModel.Ship;
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|