Files
CineBook/Views/SeatSelectionDialog.xaml.cs

142 lines
5.0 KiB
C#

using CineBook.Models;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace CineBook.Views;
public partial class SeatSelectionDialog : Window
{
private readonly Screening _screening;
private readonly List<Seat> _seats;
private Seat? _selectedSeat;
public int SelectedSeatId => _selectedSeat?.SeatID ?? 0;
public int SelectedScreeningId => _screening.ScreeningID;
public decimal TotalPrice => _screening.PricePerSeat;
public string SeatDisplay => _selectedSeat?.Display ?? "";
public SeatSelectionDialog(Screening screening, List<Seat> seats, User user)
{
InitializeComponent();
_screening = screening;
_seats = seats;
MovieInfoText.Text = screening.MovieTitle;
ScreeningInfoText.Text = $"{screening.DateTimeDisplay} | {screening.Hall} | {screening.PriceDisplay} pro Sitz";
BuildSeatGrid();
UpdateSelection();
}
private void BuildSeatGrid()
{
var rows = _seats.GroupBy(s => s.Row).OrderBy(g => g.Key);
var panel = new StackPanel();
foreach (var row in rows)
{
var rowPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
Margin = new Thickness(0, 2, 0, 2)
};
rowPanel.Children.Add(new TextBlock
{
Text = row.Key,
Width = 24,
Foreground = Brushes.Gray,
FontFamily = new FontFamily("Courier New"),
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
});
foreach (var seat in row.OrderBy(s => s.SeatNumber))
{
var btn = new Button
{
Content = seat.SeatNumber.ToString(),
Width = 34,
Height = 30,
Margin = new Thickness(2),
FontFamily = new FontFamily("Courier New"),
FontSize = 11,
Tag = seat,
IsEnabled = !seat.IsBooked,
Background = seat.IsBooked
? new SolidColorBrush(Color.FromRgb(0x44, 0x22, 0x22))
: new SolidColorBrush(Color.FromRgb(0x1B, 0x4D, 0x2A)),
Foreground = seat.IsBooked ? Brushes.DarkRed : Brushes.LightGreen,
BorderBrush = seat.IsBooked
? new SolidColorBrush(Color.FromRgb(0x80, 0x20, 0x20))
: new SolidColorBrush(Color.FromRgb(0x27, 0xAE, 0x60)),
BorderThickness = new Thickness(1),
Cursor = seat.IsBooked
? System.Windows.Input.Cursors.No
: System.Windows.Input.Cursors.Hand
};
btn.ToolTip = $"Reihe {seat.Row}, Sitz {seat.SeatNumber} ({seat.Category})";
btn.Click += SeatClick;
rowPanel.Children.Add(btn);
}
panel.Children.Add(rowPanel);
}
SeatsPanel.ItemsSource = new[] { new ContentControl { Content = panel } };
}
private void SeatClick(object sender, RoutedEventArgs e)
{
if (sender is not Button btn || btn.Tag is not Seat seat) return;
_selectedSeat = seat;
UpdateSelection();
foreach (var child in FindVisualChildren<Button>(SeatsPanel))
{
if (child.Tag is Seat s && !s.IsBooked)
{
bool isSel = s.SeatID == seat.SeatID;
child.Background = isSel
? new SolidColorBrush(Color.FromRgb(0x7A, 0x62, 0x00))
: new SolidColorBrush(Color.FromRgb(0x1B, 0x4D, 0x2A));
child.Foreground = isSel ? Brushes.Gold : Brushes.LightGreen;
}
}
}
private void UpdateSelection()
{
SelectedSeatText.Text = _selectedSeat != null
? $"Reihe {_selectedSeat.Row}, Sitz {_selectedSeat.SeatNumber} ({_selectedSeat.Category})"
: "— kein Sitz gewählt —";
PriceText.Text = _selectedSeat != null ? $"{_screening.PricePerSeat:F2} €" : "";
BtnConfirm.IsEnabled = _selectedSeat != null;
}
private void ConfirmClick(object sender, RoutedEventArgs e)
{
if (_selectedSeat == null) return;
DialogResult = true;
Close();
}
private void CancelClick(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T t) yield return t;
foreach (var sub in FindVisualChildren<T>(child))
yield return sub;
}
}
}