CustomerPanel.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Threading;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.Wpf;
  14. namespace PRSDesktop
  15. {
  16. public class CustomerPanelSettings : IUserConfigurationSettings
  17. {
  18. public CustomerPanelSettings()
  19. {
  20. AnchorWidth = 500F;
  21. View = DynamicSplitPanelView.Combined;
  22. }
  23. public DynamicSplitPanelView View { get; set; }
  24. public double AnchorWidth { get; set; }
  25. }
  26. public partial class CustomerPanel : UserControl, IPanel<Customer>
  27. {
  28. private CustomerPanelSettings settings;
  29. private Customer _customer;
  30. private enum PageIndex
  31. {
  32. Contacts,
  33. Spreadsheets
  34. }
  35. public CustomerPanel()
  36. {
  37. InitializeComponent();
  38. }
  39. public bool IsReady { get; set; }
  40. public event DataModelUpdateEvent? OnUpdateDataModel;
  41. public Dictionary<string, object[]> Selected()
  42. {
  43. return new Dictionary<string, object[]> { { typeof(Customer).EntityName(), Customers.SelectedRows } };
  44. }
  45. public void Setup()
  46. {
  47. settings = new UserConfiguration<CustomerPanelSettings>().Load();
  48. SplitPanel.View = settings.View;
  49. SplitPanel.AnchorWidth = settings.AnchorWidth;
  50. Customers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  51. Customers.Refresh(true, false);
  52. CustomerContacts.Refresh(true, false);
  53. CustomerSpreadsheets.Refresh(true,false);
  54. CustomerActivities.Refresh(true,false);
  55. CustomerProducts.Refresh(true,false);
  56. }
  57. public void Shutdown(CancelEventArgs? cancel)
  58. {
  59. }
  60. public void CreateToolbarButtons(IPanelHost host)
  61. {
  62. AccountsSetupActions.Standard(host);
  63. host.CreateSetupSeparator();
  64. AccountsSetupActions.CustomerSpreadsheetTemplates(host);
  65. //host.CreateToolbarButton(new PanelAction() { Caption = "Select Columns", OnExecute = DoSelectColumns, Image = PRSDesktop.Resources.shared });
  66. }
  67. public void Refresh()
  68. {
  69. Customers.Refresh(true, true);
  70. }
  71. public string SectionName => "Customers";
  72. public DataModel DataModel(Selection selection)
  73. {
  74. var ids = Customers.ExtractValues(x => x.ID, selection).ToArray();
  75. return new BaseDataModel<Customer>(new Filter<Customer>(x => x.ID).InList(ids));
  76. }
  77. public void Heartbeat(TimeSpan time)
  78. {
  79. }
  80. public Type DataType()
  81. {
  82. return typeof(Customer);
  83. }
  84. private void RefreshSubPage(ICustomerGrid grid)
  85. {
  86. if (grid == null)
  87. return;
  88. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("CustomerPanel: RefreshSubPage({0})", grid.GetType().EntityName()));
  89. grid.Customer = _customer;
  90. var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  91. timer.Tick += (o, e) =>
  92. {
  93. timer.Stop();
  94. grid.Refresh(!grid.IsReady, true);
  95. };
  96. timer.Start();
  97. }
  98. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  99. {
  100. settings.View = SplitPanel.View;
  101. settings.AnchorWidth = SplitPanel.AnchorWidth;
  102. new UserConfiguration<CustomerPanelSettings>().Save(settings);
  103. var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  104. if (Customers.ColumnsTag != newTag)
  105. {
  106. Customers.ColumnsTag = newTag;
  107. Customers.Refresh(true, true);
  108. }
  109. }
  110. private void CustomerDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. if (e.AddedItems.Count > 0)
  113. {
  114. var t = e.AddedItems[0] as TabItem;
  115. if (t != null && t.Visibility == Visibility.Visible)
  116. RefreshSubPage(t.Content as ICustomerGrid);
  117. }
  118. }
  119. private void Customers_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  120. {
  121. var row = e.Rows?.FirstOrDefault();
  122. _customer = row?.ToObject<Customer>() ?? new Customer();
  123. Dispatcher.Invoke(() => { RefreshSubPage(CustomerDetails.SelectedContent as ICustomerGrid); });
  124. }
  125. }
  126. }