using Comal.Classes; using InABox.DynamicGrid; using Syncfusion.Windows.Shared; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Syncfusion.Windows.Tools.Controls; namespace PRS.Shared { public class ProductPriceEditorControl : DynamicEnclosedEditorControl { private DockPanel _dockpanel; private ComboBox _chargeable; private ComboBox _pricetype; private Border _priceborder; private Label _fixedpricelabel; private DoubleTextBox _pricevalue; private Label _markuplabel; private ProductCharge _productCharge = new(); public override void Configure() { } private readonly Dictionary> _pricetypelabel = new Dictionary>() { { ProductPriceType.FixedPrice, new Tuple("$", "") }, { ProductPriceType.CostPlus, new Tuple("", "%") }, }; protected override FrameworkElement CreateEditor() { _chargeable = new ComboBox() { ItemsSource = new String[] { "Unchargeable", "Chargeable" }, VerticalContentAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, SelectedIndex = _productCharge.Chargeable ? 1 : 0, Width = 150.0F }; _chargeable.SelectionChanged += _chargeable_Changed; _pricetype = new ComboBox { ItemsSource = Enum.GetValues(), VerticalContentAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, SelectedValue = _productCharge.PriceType, Margin = new Thickness(5,0,0,0), Width = 100.0F, Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed }; _pricetype.SelectionChanged += _pricetype_SelectionChanged; _fixedpricelabel = new Label() { Content = _pricetypelabel[_productCharge.PriceType].Item1, Margin = new Thickness(5,0,0,0), Padding = new Thickness(0), HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; _pricevalue = new DoubleTextBox { Padding = new Thickness(0), Margin= new Thickness(0), VerticalContentAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, BorderThickness = new Thickness(0.0F), }; _pricevalue.ValueChanged += _pricevalue_Changed; _markuplabel = new Label() { Content = _pricetypelabel[_productCharge.PriceType].Item2, Margin= new Thickness(0,0,5,0), Padding = new Thickness(0), HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; Grid grid = new Grid() { ColumnDefinitions = { new ColumnDefinition() { Width = new GridLength(20.0F, GridUnitType.Pixel) }, new ColumnDefinition() { Width = new GridLength(60.0F, GridUnitType.Pixel) }, new ColumnDefinition() { Width = new GridLength(20.0F, GridUnitType.Pixel) }, } }; _fixedpricelabel.SetValue(Grid.ColumnProperty, 0); grid.Children.Add(_fixedpricelabel); _pricevalue.SetValue(Grid.ColumnProperty, 1); grid.Children.Add(_pricevalue); _markuplabel.SetValue(Grid.ColumnProperty, 2); grid.Children.Add(_markuplabel); _priceborder = new Border() { BorderBrush = new SolidColorBrush(Colors.Gray), BorderThickness = new Thickness(0.75), Margin = new Thickness(5,0,0,0), Padding = new Thickness(0), Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed, Child = grid }; _dockpanel = new DockPanel() { LastChildFill = false }; _chargeable.SetValue(DockPanel.DockProperty, Dock.Left); _dockpanel.Children.Add(_chargeable); _pricetype.SetValue(DockPanel.DockProperty, Dock.Left); _dockpanel.Children.Add(_pricetype); _priceborder.SetValue(DockPanel.DockProperty, Dock.Left); _dockpanel.Children.Add(_priceborder); return _dockpanel; } public override void SetValue(string property, object? value) { if (!property.StartsWith($"{ColumnName}.")) return; property = property[(ColumnName.Length + 1)..]; if (property == nameof(_productCharge.Chargeable) && value is bool b) { _productCharge.Chargeable = b; _chargeable.SelectedIndex = b ? 1 : 0; } else if (property == nameof(_productCharge.Markup) && value is double markup) { _productCharge.Markup = markup; if (_productCharge.PriceType == ProductPriceType.CostPlus) { _pricevalue.Value = markup; } } else if (property == nameof(_productCharge.Price) && value is double price) { _productCharge.Price = price; if(_productCharge.PriceType == ProductPriceType.FixedPrice) { _pricevalue.Value = price; } } else if (property == nameof(_productCharge.PriceType) && value is ProductPriceType priceType) { _productCharge.PriceType = priceType; _pricetype.SelectedValue = priceType; } } public override object? GetValue(string property) { if (!property.StartsWith($"{ColumnName}.")) return null; property = property[(ColumnName.Length + 1)..]; if (property == nameof(_productCharge.Chargeable)) return _productCharge.Chargeable; if (property == nameof(_productCharge.Markup)) return _productCharge.Markup; if (property == nameof(_productCharge.Price)) return _productCharge.Price; if (property == nameof(_productCharge.PriceType)) return _productCharge.PriceType; return null; } public override Dictionary GetValues() { return new Dictionary { { $"{ColumnName}.{nameof(_productCharge.Chargeable)}", _productCharge.Chargeable }, { $"{ColumnName}.{nameof(_productCharge.Markup)}", _productCharge.Markup }, { $"{ColumnName}.{nameof(_productCharge.Price)}", _productCharge.Price }, { $"{ColumnName}.{nameof(_productCharge.PriceType)}", _productCharge.PriceType } }; } private void _chargeable_Changed(object sender, SelectionChangedEventArgs e) { _productCharge.Chargeable = _chargeable.SelectedIndex == 1; _pricetype.Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed; _priceborder.Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed; } private void _pricetype_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(_pricetype.SelectedValue is ProductPriceType priceType) { _productCharge.PriceType = priceType; _pricevalue.Value = priceType switch { ProductPriceType.CostPlus => _productCharge.Markup, _ => _productCharge.Price, }; _fixedpricelabel.Content = _pricetypelabel[priceType].Item1; _markuplabel.Content = _pricetypelabel[priceType].Item2; } CheckChanged(); } private void _pricevalue_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var price = _pricevalue.Value ?? 0.0; switch (_productCharge.PriceType) { case ProductPriceType.FixedPrice: _productCharge.Price = price; break; case ProductPriceType.CostPlus: _productCharge.Markup = price; break; } CheckChanged(); } public override int DesiredHeight() { return 25; } public override int DesiredWidth() { return int.MaxValue; } public override void SetColor(System.Windows.Media.Color color) { var brush = new SolidColorBrush(color); // _chargeable.Background = brush; // _pricetype.Background = brush; _pricevalue.Background = brush; _fixedpricelabel.Background = brush; _markuplabel.Background = brush; _priceborder.Background = brush; } public override void SetFocus() { _pricevalue.Focus(); } } }