using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using Comal.Classes; using InABox.Configuration; using InABox.Core; using InABox.DynamicGrid; using NPOI.HSSF.Util; using Syncfusion.Windows.Tools.Controls; namespace PRSDesktop { public class ProductGroupSelectorData { // FullGuid => All Staff public Guid[] Groups { get; set; } public ProductGroupSelectorData(Guid[] groups) { Groups = groups; } public ProductGroupSelectorData() : this(new[] { CoreUtils.FullGuid }) { } } public class ProductGroupSelectorSettings { public ProductGroupSelectorSettings() { } } public class ProductGroupSelectorSettingsChangedArgs : EventArgs { public ProductGroupSelectorSettings Settings { get; private set; } public ProductGroupSelectorSettingsChangedArgs(ProductGroupSelectorSettings settings) { Settings = settings; } } public delegate void ProductGroupSelectorSettingsChanged(object sender, ProductGroupSelectorSettingsChangedArgs args); public class ProductGroupSelectorSelectionChangedArgs : EventArgs { public ProductGroupSelectorData Selection { get; private set; } public ProductGroupSelectorSelectionChangedArgs(ProductGroupSelectorData selection) { Selection = selection; } } public delegate void ProductGroupSelectorSelectionChanged(object sender, ProductGroupSelectorSelectionChangedArgs args); public partial class ProductGroupSelector : UserControl { private enum Suppress { This } private class ListEntry { public Guid Key { get; set; } public String Value { get; set; } } public ProductGroupSelectorSettings Settings { get; set; } public event ProductGroupSelectorSettingsChanged SettingsChanged; public event ProductGroupSelectorSelectionChanged SelectionChanged; private void UpdateSettings() { bool changed = false; if (changed && (!EventSuppressor.IsSet(Suppress.This))) SettingsChanged?.Invoke(this, new ProductGroupSelectorSettingsChangedArgs(Settings)); } private CoreTable _productgroups; public ProductGroupSelector() { Settings = new ProductGroupSelectorSettings(); using (new EventSuppressor(Suppress.This)) InitializeComponent(); } public void Setup() { using (new EventSuppressor(Suppress.This)) { MultiQuery query = new MultiQuery(); query.Add( LookupFactory.DefineFilter(), new Columns(x => x.ID) .Add(x => x.Code) .Add(x=>x.Description), new SortOrder(x => x.Code) ); query.Query(); _productgroups = query.Get(); SelectedGroups.ItemsSource = _productgroups.Rows.Select( r => new ListEntry() { Key = r.Get(c => c.ID), Value = String.Format("{0}: {1}", r.Get(c => c.Code), r.Get(c => c.Description)) } ).ToArray(); } } public T[] GetGroupData(Func transform) { List result = new List(); var groups = GetSelectedGroups(); var rows = _productgroups.Rows.Where(r => groups.Contains(CoreUtils.FullGuid) || groups.Contains(r.Get(c => c.ID))); foreach (var row in rows) result.Add(transform.Invoke(row)); return result.ToArray(); } private Guid[] GetSelectedGroups() { var jobs = (SelectedGroups.SelectedItems.Count == SelectedGroups.Items.Count) ? new Guid[] { CoreUtils.FullGuid } : SelectedGroups.SelectedItems .OfType() .Select(x => x.Key) .ToArray(); return jobs; } public ProductGroupSelectorData Selection { get => GetSelectionData(); set => SelectData(value); } private ProductGroupSelectorData GetSelectionData() { return new ProductGroupSelectorData( GetSelectedGroups() ); } private void SelectData(ProductGroupSelectorData selection) { using (new EventSuppressor(Suppress.This)) { var sels = SelectedGroups.Items .OfType() .Where(x => selection.Groups.Contains(CoreUtils.FullGuid) || selection.Groups.Contains(x.Key)); SelectedGroups.SelectedItems.Clear(); foreach (var sel in sels) SelectedGroups.SelectedItems.Add(sel); } } private void SelectedGroups_OnItemChecked(object? sender, ItemCheckedEventArgs e) { if (EventSuppressor.IsSet(Suppress.This)) return; UpdateSettings(); SelectionChanged?.Invoke(this, new ProductGroupSelectorSelectionChangedArgs(GetSelectionData())); } private void SelectAll_OnClick(object sender, RoutedEventArgs e) { using (new EventSuppressor(Suppress.This)) { var selected = SelectedGroups.SelectedItems .OfType() .Select(x=>x.Key) .ToArray(); var items = SelectedGroups.Items .OfType() .ToArray(); if (items.Length == selected.Length) { SelectedGroups.SelectedItems.Clear(); SelectAll.Content = "All"; } else { var unselected = items.Where(p => !selected.Contains(p.Key)); foreach (var item in unselected) SelectedGroups.SelectedItems.Add(item); SelectAll.Content = "None"; } UpdateSettings(); SelectionChanged?.Invoke(this, new ProductGroupSelectorSelectionChangedArgs(GetSelectionData())); } } } }