using Microsoft.Win32; using System; using System.IO; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using WinForms = System.Windows.Forms; namespace dealer { public partial class EditVehicleWindow : Window { public string? SelectedPhotoPath { get; private set; } public System.Windows.Media.Color SelectedColor { get; private set; } = Colors.White; public bool WasEdited { get; private set; } = false; private DataManager dataManager; private Vehicle vehicle; private bool photoChanged = false; public EditVehicleWindow(Vehicle vehicle, DataManager dataManager) { InitializeComponent(); this.dataManager = dataManager; this.vehicle = vehicle; LoadVehicleData(); } private void LoadVehicleData() { // SPZ SpzTextBox.Text = vehicle.Spz ?? ""; // Model ModelTextBox.Text = vehicle.Model ?? ""; // Barva if (!string.IsNullOrEmpty(vehicle.Barva)) { try { SelectedColor = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(vehicle.Barva); ColorPreviewBorder.Background = new SolidColorBrush(SelectedColor); ColorNameTextBlock.Text = vehicle.Barva; ColorNameTextBlock.FontStyle = FontStyles.Normal; ColorNameTextBlock.Foreground = System.Windows.Media.Brushes.Black; } catch { ColorPreviewBorder.Background = System.Windows.Media.Brushes.White; } } // Rok vyroby YearTextBox.Text = vehicle.RokVyroby?.ToString() ?? ""; // Najete km MileageTextBox.Text = vehicle.NajeteKm?.ToString() ?? ""; // Cena PriceTextBox.Text = vehicle.Cena?.ToString() ?? ""; // Fotografie (base64) if (!string.IsNullOrEmpty(vehicle.Fotografie)) { try { byte[] imageBytes = Convert.FromBase64String(vehicle.Fotografie); using (var ms = new MemoryStream(imageBytes)) { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = ms; bitmap.EndInit(); bitmap.Freeze(); CurrentPhotoImage.Source = bitmap; } CurrentPhotoBorder.Visibility = Visibility.Visible; PhotoPathTextBlock.Text = "Aktualni fotografie zobrazena vyse"; } catch { CurrentPhotoBorder.Visibility = Visibility.Collapsed; } } } private void SelectPhotoButton_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog { Title = "Vyberte fotografii vozidla", Filter = "Obrazky|*.jpg;*.jpeg;*.png;*.bmp;*.gif|Vsechny soubory|*.*", FilterIndex = 1 }; if (openFileDialog.ShowDialog() == true) { SelectedPhotoPath = openFileDialog.FileName; PhotoPathTextBlock.Text = SelectedPhotoPath; photoChanged = true; // Show preview of new photo try { var bitmap = new BitmapImage(new Uri(SelectedPhotoPath)); CurrentPhotoImage.Source = bitmap; CurrentPhotoBorder.Visibility = Visibility.Visible; } catch { // Ignore preview errors } } } private void SelectColorButton_Click(object sender, RoutedEventArgs e) { WinForms.ColorDialog colorDialog = new WinForms.ColorDialog { AllowFullOpen = true, FullOpen = true }; if (colorDialog.ShowDialog() == WinForms.DialogResult.OK) { SelectedColor = System.Windows.Media.Color.FromArgb( colorDialog.Color.A, colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B); ColorPreviewBorder.Background = new SolidColorBrush(SelectedColor); ColorNameTextBlock.Text = $"#{SelectedColor.R:X2}{SelectedColor.G:X2}{SelectedColor.B:X2}"; ColorNameTextBlock.FontStyle = FontStyles.Normal; ColorNameTextBlock.Foreground = System.Windows.Media.Brushes.Black; } } private void NumericOnly_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !int.TryParse(e.Text, out _); } private void DecimalOnly_PreviewTextInput(object sender, TextCompositionEventArgs e) { var textBox = sender as System.Windows.Controls.TextBox; string newText = textBox?.Text.Insert(textBox.SelectionStart, e.Text) ?? e.Text; e.Handled = !decimal.TryParse(newText, out _); } private void SaveButton_Click(object sender, RoutedEventArgs e) { // Update vehicle properties vehicle.Spz = SpzTextBox.Text; vehicle.Model = ModelTextBox.Text; vehicle.Barva = $"#{SelectedColor.R:X2}{SelectedColor.G:X2}{SelectedColor.B:X2}"; vehicle.RokVyroby = int.TryParse(YearTextBox.Text, out int rok) ? rok : (int?)null; vehicle.NajeteKm = int.TryParse(MileageTextBox.Text, out int km) ? km : (int?)null; vehicle.Cena = decimal.TryParse(PriceTextBox.Text, out decimal cena) ? cena : (decimal?)null; // Update photo only if changed if (photoChanged && !string.IsNullOrEmpty(SelectedPhotoPath) && File.Exists(SelectedPhotoPath)) { byte[] imageBytes = File.ReadAllBytes(SelectedPhotoPath); vehicle.Fotografie = Convert.ToBase64String(imageBytes); } this.dataManager.Save(); WasEdited = true; this.Close(); } private void CancelButton_Click(object sender, RoutedEventArgs e) { this.Close(); } } }