using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using Syncfusion.Data.Extensions; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using Syncfusion.Pdf.Parsing; using Encoder = System.Drawing.Imaging.Encoder; using Image = System.Windows.Controls.Image; using System.Reflection; namespace PRSDesktop { /// /// Interaction logic for DataEntryPanel.xaml /// public partial class DataEntryPanel : UserControl, IBasePanel { BaseDynamicEditorControl? Popup; public DataEntryPanel() { InitializeComponent(); } public void Setup() { var types = CoreUtils.Entities .Where(x => x.IsAssignableTo(typeof(Entity)) && x.HasInterface(typeof(IRemotable)) && x.HasInterface(typeof(IPersistent)) && x.GetCustomAttribute() is null && Security.CanEdit(x) && x.HasInterface()) .Select(x => new Tuple(x.Name, x)).OrderBy(x => x.Item1).ToList(); types.Insert(0, new("", null)); TypeBox.ItemsSource = types; TypeBox.DisplayMemberPath = "Item1"; TypeBox.SelectedValuePath = "Item2"; ScanPanel.Setup(); } public void Refresh() { ScanPanel.Refresh(); } public bool IsReady { get; set; } public string SectionName => "Data Entry"; public DataModel DataModel(Selection selection) { return new EmptyDataModel(); } public event DataModelUpdateEvent? OnUpdateDataModel; public void CreateToolbarButtons(IPanelHost host) { if (Security.IsAllowed()) { host.CreateSetupAction(new PanelAction { Caption = "Scan Tags", OnExecute = (action) => { var list = new MasterList(typeof(ScanTag)); list.ShowDialog(); } }); } } public void Heartbeat(TimeSpan time) { } public Dictionary Selected() { return new Dictionary(); } public void Shutdown() { ScanPanel.Shutdown(); } private void ClearEditor(Type TEntity) { UpdateEditor(TEntity, null); if(Popup is not null) { Popup.SetValue(Guid.Empty); } } private bool EditorChanged = false; private IDynamicDataGrid UpdateEditor(Type TEntity, BaseObject[]? items) { DetailGrid.Children.Remove(Editor); Editor = new EmbeddedDynamicEditorForm(); Editor.SetLayoutType(); Editor.SetValue(Grid.RowProperty, 1); Editor.SetValue(Grid.ColumnProperty, 0); Editor.SetValue(Grid.ColumnSpanProperty, 4); EditorChanged = false; Editor.OnAfterEditorValueChanged += (sender, column) => { EditorChanged = true; return null; }; Editor.OnOK += () => { var cancel = new System.ComponentModel.CancelEventArgs(); Editor.SaveItem(cancel); if(!cancel.Cancel) ClearEditor(TEntity); }; Editor.OnCancel += () => { ClearEditor(TEntity); }; DetailGrid.Children.Add(Editor); var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), TEntity); grid.InitialiseEditorForm(Editor, items ?? new object[] { Activator.CreateInstance(TEntity)! }); return (grid as IDynamicDataGrid)!; } private void TypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selectedType = TypeBox.SelectedValue as Type; if (selectedType is not null) { var dataGrid = UpdateEditor(selectedType, null); var editorColumns = dataGrid.LoadEditorColumns(); if (Popup is not null) DetailGrid.Children.Remove(Popup); var code = DatabaseSchema.Properties(selectedType) .Where(x => x.Parent is null && (x.Editor is CodeEditor || x.Editor is UniqueCodeEditor) && x.Editor.Editable != Editable.Hidden) .FirstOrDefault(); BaseEditor editor; if (code is not null) { editor = new CodePopupEditor(selectedType) { CodeColumn = code.Name }; Popup = new CodePopupEditorControl { Margin = new Thickness(5), CodeColumn = code.Name }; } else { editor = new PopupEditor(selectedType); Popup = new PopupEditorControl { Margin = new Thickness(5) }; } if (Popup is IPopupEditorControl popupEditor) { Popup.ColumnName = "ID"; popupEditor.OnDefineFilter += (sender, type) => { return LookupFactory.DefineFilter(type); }; foreach (var column in LookupFactory.DefineColumns(selectedType).ColumnNames().Where(x => x != "ID")) { popupEditor.OtherColumns.Add(column, column); } if (popupEditor is CodePopupEditorControl codePopup && !popupEditor.OtherColumns.ContainsKey(codePopup.CodeColumn)) { popupEditor.OtherColumns.Add(codePopup.CodeColumn, codePopup.CodeColumn); } } Popup.SetValue(Grid.RowProperty, 0); Popup.SetValue(Grid.ColumnProperty, 3); Popup.EditorDefinition = editor; Popup.OnEditorValueChanged += (s, v) => { var entityID = (Guid)v["ID"]; BaseObject[]? objs = null; if (entityID != Guid.Empty) { var obj = Client.Create(selectedType) .Query( Filter.Create(selectedType, x => x.ID).IsEqualTo(entityID), editorColumns) .ToObjects(selectedType) .FirstOrDefault(); if (obj is not null) { objs = new BaseObject[] { obj }; } } UpdateEditor(selectedType, objs); }; Popup.Configure(); Popup.Loaded = true; DetailGrid.Children.Add(Popup); } else { DetailGrid.Children.Remove(Editor); if (Popup is not null) DetailGrid.Children.Remove(Popup); } } private void ScanPanel_OnSelectAppliesTo(string appliesTo) { if(CoreUtils.TryGetEntity(appliesTo, out var entity)) { TypeBox.SelectedValue = entity; } } } }