using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Windows.Controls; using System.Windows.Media.Imaging; using com.healthmarketscience.jackcess.query; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.Wpf; using InABox.WPF; namespace PRSDesktop; public class ProblemsDockGrid : DynamicDataGrid { protected override void Init() { base.Init(); HiddenColumns.Add(x=>x.Problem.Notes); HiddenColumns.Add(x=>x.Problem.AssignedTo.Code); ActionColumns.Add(new DynamicImageColumn(TypeImage) { Position = DynamicActionColumnPosition.Start, Filters = IMAGES.Keys.Select(x=>x.Name.Split('.').Last()).ToArray(), GetFilterExpression = (col) => TypeExpression(col), }); ActionColumns.Add(new DynamicTextColumn(GetLastNote) { Position = DynamicActionColumnPosition.End, Width = 0, HeaderText = "Issues" }); ActionColumns.Add(new DynamicTextColumn(GetAssignedTo) { Position = DynamicActionColumnPosition.End, Width = 100, HeaderText="Assigned To", Alignment = Alignment.MiddleCenter}); ActionColumns.Add(new DynamicMenuColumn(ProblemMenu) { Position = DynamicActionColumnPosition.End }); AddButton("Add Note", PRSDesktop.Resources.notification.AsBitmapImage(), AddNote); AddButton("Assign To", PRSDesktop.Resources.employee.AsBitmapImage(), AssignTo); AddButton("Mark Resolved", PRSDesktop.Resources.delete.AsBitmapImage(), MarkResolved, position: DynamicGridButtonPosition.Right); } private string TypeExpression(DynamicActionColumn column) { return column.CreateFilterExpression("Type") ?? ""; } private bool TypeFilter(CoreRow row, string[] filter) { string typename = row.Get(x => x.Type); return filter.Contains(typename); } private object? GetAssignedTo(CoreRow? row) => row?.Get(x => x.Problem.AssignedTo.Code); private object? GetLastNote(CoreRow? row) => row?.Get(x => x.Problem.Notes).LastOrDefault(); protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.FilterRows = true; options.MultiSelect = true; } protected override DynamicGridColumns LoadColumns() { var result = new DynamicGridColumns(); result.Add(x => x.Code, 150, "Code", "", Alignment.MiddleLeft); result.Add(x => x.Description, 0, "Description", "", Alignment.MiddleLeft); return result; } private static readonly Dictionary IMAGES = new() { { typeof(Bill), PRSDesktop.Resources.bill.AsBitmapImage() }, { typeof(Activity), PRSDesktop.Resources.assignments.AsBitmapImage() }, { typeof(ProductStyle), PRSDesktop.Resources.palette.AsBitmapImage() }, { typeof(Product), PRSDesktop.Resources.product.AsBitmapImage() }, { typeof(CostSheet), PRSDesktop.Resources.costsheet.AsBitmapImage() }, { typeof(Kit), PRSDesktop.Resources.kit.AsBitmapImage() }, { typeof(JobBillOfMaterialsItem), PRSDesktop.Resources.box_sml.AsBitmapImage() }, { typeof(JobRequisitionItem), PRSDesktop.Resources.requisition.AsBitmapImage() }, { typeof(ManufacturingPacket), PRSDesktop.Resources.factory.AsBitmapImage() }, }; private BitmapImage? TypeImage(CoreRow? row) { var _type = row?.Get(x => x.Type) ?? ""; var _key = IMAGES.Keys.FirstOrDefault(x => string.Equals(x.Name.Split('.').Last(), _type)); return _key != null ? IMAGES[_key] : null; } private string? TypeText(CoreRow? row) { return row?.Get(x => x.Type) ?? ""; } private void ProblemMenu(DynamicMenuColumn menu, CoreRow? row) { menu.AddItem("Add Note", null, AddNote); menu.AddItem("Assign To...", null, AssignTo); menu.AddSeparator(); menu.AddItem("Edit Item", null, EditItem); menu.AddSeparator(); menu.AddItem("Mark as Resolved", null, MarkResolved); } private Type? GetType(CoreRow? row) { var _name = row?.Get(x => x.Type) ?? ""; var _type = IMAGES.Keys.FirstOrDefault(x => string.Equals(x.Name.Split('.').Last(), _name)); return _type; } private IProblems? GetItem(CoreRow? row) { var _type = GetType(row); if (_type == null) return null; var _id = row.Get(x => x.ID); var item = ClientFactory.CreateClient(_type).Query( Filter.Create(_type, "ID", Operator.IsEqualTo, _id), Columns.Local(_type), null ).Rows.FirstOrDefault()?.ToObject(_type) as IProblems; return item; } private void AddNote(CoreRow[] rows) { var _items = rows.Select(GetItem).Where(x => x != null).ToArray(); if (!_items.Any()) return; var _note = ""; if (TextBoxDialog.Execute("Add Problem Note", ref _note)) { Dictionary>> _updates = new(); foreach (var _item in _items) { if (_item == null) continue; var _notes = _item.Problem.Notes?.ToList() ?? new List(); _notes.Add(_note); _item.Problem.Notes = _notes.ToArray(); var _type = _item.GetType(); if (!_updates.ContainsKey(_type)) _updates[_type] = new List>(); _updates[_type].Add(_item); } Progress.ShowModal("Adding Notes", progress => { foreach (var _update in _updates) { progress.Report($"Adding note to {_update.Value.Count} {new Inflector.Inflector(new CultureInfo("en")).Pluralize(_update.Key.Name.Split('.').Last()).SplitCamelCase()}"); ClientFactory.CreateClient(_update.Key).Save(_update.Value, "Added Isse note"); } }); Refresh(false, true); } } private bool AddNote(Button button, CoreRow[] rows) { AddNote(rows); return false; } private void AddNote(CoreRow? row) { if (row == null) return; AddNote([row]); } private void MarkResolved(CoreRow?[] rows) { var _items = rows.Select(GetItem).Where(x => x != null).ToArray(); if (!_items.Any()) return; Dictionary>> _updates = new(); foreach (var _item in _items) { if (_item == null) continue; _item.Problem.Resolved = DateTime.Now; var _type = _item.GetType(); if (!_updates.ContainsKey(_type)) _updates[_type] = new List>(); _updates[_type].Add(_item); } Progress.ShowModal("Resolving items", progress => { foreach (var _update in _updates) { progress.Report($"Resolving {_update.Value.Count} {new Inflector.Inflector(new CultureInfo("en")).Pluralize(_update.Key.Name.Split('.').Last()).SplitCamelCase()}"); ClientFactory.CreateClient(_update.Key).Save(_update.Value, "Issue marked as resolved"); } }); Refresh(false, true); } private void MarkResolved(CoreRow? row) { if (row == null) return; MarkResolved([row]); } private bool MarkResolved(Button button, CoreRow[] rows) { if (MessageWindow.ShowYesNo("Mark Selected Issues as resolved?","Confirm")) MarkResolved(rows); return false; } private void AssignTo(CoreRow[] rows) { var _items = rows.Select(GetItem).ToArray(); if (!_items.Any()) return; var _dlg = new MultiSelectDialog(LookupFactory.DefineFilter(), null, false); if (_dlg.ShowDialog()) { Dictionary>> _updates = new(); foreach (var _item in _items) { if (_item == null) continue; _item.Problem.AssignedTo.ID = _dlg.IDs().FirstOrDefault(); var _type = _item.GetType(); if (!_updates.ContainsKey(_type)) _updates[_type] = new List>(); _updates[_type].Add(_item); } Progress.ShowModal("Reassigning items", progress => { foreach (var _update in _updates) { progress.Report($"Reassigning {_update.Value.Count} {new Inflector.Inflector(new CultureInfo("en")).Pluralize(_update.Key.Name.Split('.').Last()).SplitCamelCase()}"); ClientFactory.CreateClient(_update.Key).Save(_update.Value, "Reassigning Problem"); } }); Refresh(false, true); } } private bool AssignTo(Button button, CoreRow[] rows) { AssignTo(rows); return false; } private void AssignTo(CoreRow? row) { if (row != null) return; AssignTo([ row ]); } protected override void DoDoubleClick(object sender, DynamicGridCellClickEventArgs args) { base.DoDoubleClick(sender, args); if (args.Row == null) return; EditItem(args.Row); } private void EditItem(CoreRow? row) { var _type = GetType(row); if (_type == null) return; var _item = GetItem(row); if (_item == null) return; var _grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _type); if (_grid.EditItems([ _item ], null, false, this)) Refresh(false,true); } }