using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using NPOI.SS.Formula.Functions; using Syncfusion.Pdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PRSDesktop { public class ScanGrid : DynamicDataGrid { public string? AppliesTo { get; set; } public ScanGrid() { Options.BeginUpdate() .AddRange(DynamicGridOption.MultiSelect, DynamicGridOption.DragSource, DynamicGridOption.FilterRows) .Remove(DynamicGridOption.ImportData) .EndUpdate(); } protected override void OnRowsDragStart(CoreRow[] rows) { var table = new CoreTable(); table.Columns.Add(new CoreColumn { ColumnName = "ID", DataType = typeof(Guid) }); foreach(var row in rows) { var newRow = table.NewRow(); newRow.Set(x => x.ID, row.Get(x => x.Document.ID)); table.Rows.Add(newRow); } DragTable(typeof(Document), table); } private void UploadDocument(string filename, byte[] data) { var document = new Document { FileName = filename, CRC = CoreUtils.CalculateCRC(data), TimeStamp = DateTime.Now, Data = data }; new Client().Save(document, ""); var scan = new Scan(); scan.Document.ID = document.ID; scan.AppliesTo = AppliesTo ?? ""; new Client().Save(scan, ""); Dispatcher.Invoke(() => { Refresh(false, true); }); } private static PdfDocumentBase CombinePages(IEnumerable pages) { var document = new PdfDocument(); foreach (var page in pages) { document.ImportPage(page.Pdf, page.PageIndex); } return document; } public void ShowDocumentWindow(List pages, string filename) { var window = new DocumentManipulationWindow(pages, filename); if (window.ShowDialog() == true) { Progress.ShowModal("Uploading Files", (progress) => { foreach (var group in window.Groups) { progress.Report($"Uploading '{group.FileName}'"); var doc = CombinePages(group.Pages); byte[] data; using (var ms = new MemoryStream()) { doc.Save(ms); data = ms.ToArray(); } UploadDocument(group.FileName, data); } }); } } protected override void DoAdd() { ShowDocumentWindow(new(), ""); } protected override Scan CreateItem() { var scan = base.CreateItem(); scan.AppliesTo = AppliesTo ?? ""; return scan; } protected override void GenerateColumns(DynamicGridColumns columns) { base.GenerateColumns(columns); columns.RemoveAll(x => x.ColumnName == "Document.FileName"); columns.Add(x => x.Document.FileName, 0, "Filename", "", InABox.Core.Alignment.MiddleLeft); } protected override void Reload(Filters criteria, Columns columns, ref SortOrder? sort, Action action) { if (!string.IsNullOrWhiteSpace(AppliesTo)) { criteria.Add(new Filter(x => x.AppliesTo).IsEqualTo(AppliesTo)); } base.Reload(criteria, columns, ref sort, action); } } }