using InABox.Clients; using InABox.Core; using InABox.WPF; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media.Imaging; namespace InABox.DynamicGrid { public class DynamicEntityFormGrid : DynamicDataGrid where TForm : EntityForm, new() where TEntity : Entity where TEntityLink : IEntityLink, new() { private Guid EntityID { get; set; } public DynamicEntityFormGrid(Guid entityID) { EntityID = entityID; OnBeforeSave += BeforeSave; ActionColumns.Add(new DynamicActionColumn(EditImage, EditClick)); if (DynamicGridUtils.PreviewReport != null) ActionColumns.Add(new DynamicActionColumn(PrintImage, PrintClick)); HiddenColumns.Add(x => x.FormData); HiddenColumns.Add(x => x.FormCompleted); HiddenColumns.Add(x => x.FormCompletedBy.ID); HiddenColumns.Add(x => x.Form.ID); HiddenColumns.Add(x => x.Parent.ID); } protected override TForm CreateItem() { var result = base.CreateItem(); result.Parent.ID = EntityID; return result; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { criteria.Add(new Filter("Parent.ID").IsEqualTo(EntityID)); base.Reload(criteria, columns, ref sort, action); } private void BeforeSave(DynamicEditorForm editor, TForm[] items) { foreach(var item in items) { item.Parent.ID = EntityID; } } private BitmapImage PrintImage(CoreRow arg) { return Properties.Resources.print.AsBitmapImage(); } private bool PrintClick(CoreRow arg) { var formid = arg.Get(x => x.Form.ID); var model = new DigitalFormReportDataModel(new Filter("Parent.ID").IsEqualTo(EntityID), formid); var section = formid.ToString(); // TODO: This is a hack DynamicGridUtils.PrintMenu?.Invoke(null, section, model, true); return false; } private BitmapImage EditImage(CoreRow arg) { if (arg == null) return Properties.Resources.pencil.AsBitmapImage(); var completed = arg.Get(x => x.FormCompleted); return completed.IsEmpty() ? Properties.Resources.pencil.AsBitmapImage() : Properties.Resources.view.AsBitmapImage(); } private bool EditClick(CoreRow arg) { var item = LoadItem(arg); if (DynamicFormEditWindow.EditDigitalForm(item, out var dataModel)) { dataModel.Update(null); return true; } return false; } } }