using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.DynamicGrid; namespace PRSDesktop { public class CustomerPanelSettings : IUserConfigurationSettings { public CustomerPanelSettings() { AnchorWidth = 500F; View = DynamicSplitPanelView.Combined; } public DynamicSplitPanelView View { get; set; } public double AnchorWidth { get; set; } } public partial class CustomerPanel : UserControl, IPanel { private CustomerPanelSettings settings; private Customer _customer; private enum PageIndex { Contacts, Spreadsheets } public CustomerPanel() { InitializeComponent(); } public bool IsReady { get; set; } public event DataModelUpdateEvent? OnUpdateDataModel; public Dictionary Selected() { return new Dictionary { { typeof(Customer).EntityName(), Customers.SelectedRows } }; } public void Setup() { settings = new UserConfiguration().Load(); SplitPanel.View = settings.View; SplitPanel.AnchorWidth = settings.AnchorWidth; Customers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : ""; Customers.Refresh(true, false); CustomerContacts.Refresh(true, false); CustomerSpreadsheets.Refresh(true,false); CustomerActivities.Refresh(true,false); CustomerProducts.Refresh(true,false); } public void Shutdown(CancelEventArgs? cancel) { } public void CreateToolbarButtons(IPanelHost host) { AccountsSetupActions.Standard(host); host.CreateSetupSeparator(); AccountsSetupActions.CustomerSpreadsheetTemplates(host); //host.CreateToolbarButton(new PanelAction() { Caption = "Select Columns", OnExecute = DoSelectColumns, Image = PRSDesktop.Resources.shared }); } public void Refresh() { Customers.Refresh(true, true); } public string SectionName => "Customers"; public DataModel DataModel(Selection selection) { var ids = Customers.ExtractValues(x => x.ID, selection).ToArray(); return new BaseDataModel(new Filter(x => x.ID).InList(ids)); } public void Heartbeat(TimeSpan time) { } public Type DataType() { return typeof(Customer); } private void RefreshSubPage(ICustomerGrid grid) { if (grid == null) return; Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("CustomerPanel: RefreshSubPage({0})", grid.GetType().EntityName())); grid.Customer = _customer; var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) }; timer.Tick += (o, e) => { timer.Stop(); grid.Refresh(!grid.IsReady, true); }; timer.Start(); } private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e) { settings.View = SplitPanel.View; settings.AnchorWidth = SplitPanel.AnchorWidth; new UserConfiguration().Save(settings); var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : ""; if (Customers.ColumnsTag != newTag) { Customers.ColumnsTag = newTag; Customers.Refresh(true, true); } } private void CustomerDetails_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) { var t = e.AddedItems[0] as TabItem; if (t != null && t.Visibility == Visibility.Visible) RefreshSubPage(t.Content as ICustomerGrid); } } private void Customers_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e) { var row = e.Rows?.FirstOrDefault(); _customer = row?.ToObject() ?? new Customer(); Dispatcher.Invoke(() => { RefreshSubPage(CustomerDetails.SelectedContent as ICustomerGrid); }); } } }