using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Comal.Classes;
using InABox.Clients;
using InABox.Configuration;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
namespace PRSDesktop
{
public class EquipmentPanelSettings : BaseObject, IGlobalConfigurationSettings
{
public bool CustomerFilterEnabled { get; set; }
public EquipmentPanelSettings()
{
CustomerFilterEnabled = false;
}
}
///
/// Interaction logic for EquipmentPanel.xaml
///
public partial class EquipmentPanel : UserControl, IPanel
{
private EquipmentPanelSettings _settings = null;
private String _searchfilter = "";
public ObservableCollection> GroupList { get; private set; }
public bool IsReady { get; set; }
private String _customercode = "";
private List> _customers;
public EquipmentPanel()
{
GroupList = new ObservableCollection>();
InitializeComponent();
}
public Type DataType()
{
return typeof(Equipment);
}
public event DataModelUpdateEvent OnUpdateDataModel;
public Dictionary Selected()
{
return new Dictionary { { typeof(Equipment).EntityName(), _Equipment.SelectedRows } };
}
public void Setup()
{
_Equipment.CustomerID = Guid.Empty;
_Equipment.GroupID = CoreUtils.FullGuid;
_settings = new GlobalConfiguration().Load();
ReconfigurePanel();
_Equipment.Refresh(true, false);
GroupList.Add(new Tuple("All Items", CoreUtils.FullGuid, PRSDesktop.Resources.edit.AsBitmapImage()));
var groups = new Client().Query(
null,
new Columns(x=>x.ID)
.Add(x=>x.Description)
.Add(x=>x.Thumbnail.ID),
new SortOrder(x => x.Code)
);
var ids = groups.ExtractValues(c => c.Thumbnail.ID).Distinct().Where(x => x != Guid.Empty).ToArray();
var Images = ids.Any()
? new Client().Load(new Filter(x=>x.ID).InList(ids))
: new Document[] { };
foreach (var row in groups.Rows)
{
var image = Images.FirstOrDefault(x => x.ID.Equals(row.Get(c=>c.Thumbnail.ID)));
BitmapImage img = (image != null && image.Data != null && image.Data.Length > 0)
? ImageUtils.LoadImage(image.Data)
: PRSDesktop.Resources.specifications.AsBitmapImage();
GroupList.Add(
new Tuple(
row.Get(c=>c.Description),
row.Get(c=>c.ID),
img
)
);
}
Groups.Visibility = groups.Rows.Any() ? Visibility.Visible : Visibility.Collapsed;
Groups.ItemsSource = GroupList;
Groups.SelectedIndex = 0;
}
public void Shutdown()
{
}
public void CreateToolbarButtons(IPanelHost host)
{
host.CreateSetupAction(new PanelAction() { Caption = "Equipment Settings", Image = PRSDesktop.Resources.specifications, OnExecute = EquipmentSettingsClick });
}
private void EquipmentSettingsClick(PanelAction obj)
{
var pages = new DynamicEditorPages();
var buttons = new DynamicEditorButtons();
buttons.Add(
"",
PRSDesktop.Resources.help.AsBitmapImage(),
_settings,
(f, i) =>
{
Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + typeof(Equipment).Name.SplitCamelCase().Replace(" ", "_"))
{ UseShellExecute = true });
}
);
var propertyEditor = new DynamicEditorForm(typeof(EquipmentPanelSettings), pages, buttons);
propertyEditor.Items = new BaseObject[] { _settings };
if (propertyEditor.ShowDialog() == true)
{
new GlobalConfiguration().Save(_settings);
ReconfigurePanel();
}
}
private void ReconfigurePanel()
{
CustomerLabel.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
Customer.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
CustomerSearch.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
CustomerName.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
if (!_settings.CustomerFilterEnabled)
{
_Equipment.CustomerID = CoreUtils.FullGuid;
_customercode = "";
Customer.Text = "";
CustomerName.Text = "";
}
else if (_Equipment.CustomerID == CoreUtils.FullGuid)
_Equipment.CustomerID = Guid.Empty;
if (IsReady)
_Equipment.Refresh(false,true);
}
public void Refresh()
{
_Equipment.Refresh(false, true);
}
public string SectionName => "Equipment";
public DataModel DataModel(Selection selection)
{
var ids = _Equipment?.ExtractValues(x => x.ID, selection)?.ToArray() ?? new Guid[] { };
return new BaseDataModel(new Filter(x => x.ID).InList(ids));
}
public void Heartbeat(TimeSpan time)
{
}
private bool _Equipment_OnFilterRecord(CoreRow row)
{
if (!String.IsNullOrWhiteSpace(_searchfilter))
{
for (int i=0; i)e.AddedItems[0];
_Equipment.GroupID = selected.Item2;
}
if (sCur != _Equipment.GroupID)
_Equipment.Refresh(false, true);
}
private void Search_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (String.IsNullOrEmpty(Search.Text) && !String.Equals(Search.Text, _searchfilter))
{
_searchfilter = "";
_Equipment.Refresh(false, false);
}
}
private void Search_OnKeyUp(object sender, KeyEventArgs e)
{
if (new Key[] { Key.Enter, Key.Return }.Contains(e.Key) && !String.Equals(Search.Text, _searchfilter))
{
_searchfilter = Search.Text;
_Equipment.Refresh(false, false);
}
}
private void CheckCustomerCache()
{
if (_customers == null)
{
_customers = new Client().Query(
null,
new Columns(x => x.ID).Add(x => x.Code).Add(x => x.Name),
new SortOrder(x => x.Name)
).ToTuples(x =>x.ID,x=>x.Code,x=>x.Name).ToList();
}
}
private void DoSearchCustomers()
{
var dlg = new MultiSelectDialog(null, new Columns(x => x.ID).Add(x => x.Code).Add(x => x.Name), false);
if (dlg.ShowDialog("Code",Customer.Text))
{
var customer = dlg.Items().First();
_customercode = customer.Code;
Customer.Text = customer.Code;
CustomerName.Text = customer.Name;
_Equipment.CustomerID = customer.ID;
}
else
{
_customercode = "";
Customer.Text = "";
CustomerName.Text = "";
_Equipment.CustomerID = Guid.Empty;
}
_Equipment.Refresh(false, true);
}
private void CustomerSearch_OnClick(object sender, RoutedEventArgs e)
{
DoSearchCustomers();
}
private void Customer_OnKeyUp(object sender, KeyEventArgs e)
{
if (new Key[] { Key.Enter, Key.Return }.Contains(e.Key))
{
if (!String.Equals(Customer.Text, _customercode))
{
CheckCustomerCache();
var lookup = _customers.FirstOrDefault(x => String.Equals(x.Item2, Customer.Text));
if (lookup != null)
{
_customercode = lookup.Item2;
CustomerName.Text = lookup.Item3;
_Equipment.CustomerID = lookup.Item1;
_Equipment.Refresh(false, true);
}
else
DoSearchCustomers();
}
}
}
private void Customer_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (String.IsNullOrEmpty(Customer.Text) && !String.Equals(Customer.Text, _customercode))
{
_Equipment.CustomerID = Guid.Empty;
_customercode = "";
CustomerName.Text = "";
_Equipment.Refresh(false, true);
}
}
}
}