107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
using Microsoft.Win32;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using WinForms = System.Windows.Forms;
|
|
|
|
namespace dealer
|
|
{
|
|
public partial class AddVehicleWindow : Window
|
|
{
|
|
public string? SelectedPhotoPath { get; private set; }
|
|
public System.Windows.Media.Color SelectedColor { get; private set; } = Colors.White;
|
|
|
|
private DataManager dataManager;
|
|
|
|
public AddVehicleWindow(DataManager dataManager)
|
|
{
|
|
InitializeComponent();
|
|
|
|
this.dataManager = dataManager;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
string? base64Image = null;
|
|
if (!string.IsNullOrEmpty(SelectedPhotoPath) && File.Exists(SelectedPhotoPath))
|
|
{
|
|
byte[] imageBytes = File.ReadAllBytes(SelectedPhotoPath);
|
|
base64Image = Convert.ToBase64String(imageBytes);
|
|
}
|
|
|
|
var vehicle = new Vehicle
|
|
{
|
|
Spz = SpzTextBox.Text,
|
|
Model = ModelTextBox.Text,
|
|
Barva = $"#{SelectedColor.R:X2}{SelectedColor.G:X2}{SelectedColor.B:X2}",
|
|
RokVyroby = int.TryParse(YearTextBox.Text, out int rok) ? rok : (int?)null,
|
|
NajeteKm = int.TryParse(MileageTextBox.Text, out int km) ? km : (int?)null,
|
|
Cena = decimal.TryParse(PriceTextBox.Text, out decimal cena) ? cena : (decimal?)null,
|
|
Fotografie = base64Image
|
|
};
|
|
|
|
this.dataManager.AddVehicle(vehicle);
|
|
this.dataManager.Save();
|
|
|
|
this.Close();
|
|
}
|
|
|
|
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
}
|
|
}
|