using System; using System.Linq; using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using InABox.Wpf; using System.Threading; namespace PRSDesktop { public class JobFormGrid : DynamicEntityFormGrid, IMasterDetailControl, IDataModelSource { protected override Job Entity { get => Master; set => Master = value; } public Job? Master { get; set; } public Filter MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty ? new Filter(x => x.Parent.ID).IsEqualTo(Master.ID) : new Filter().None(); public event DataModelUpdateEvent? OnUpdateDataModel; public string SectionName => "Job Forms"; public DataModel DataModel(Selection selection) { var ids = ExtractValues(x => x.ID, selection).ToArray(); return new AutoDataModel(new Filter(x => x.ID).InList(ids)); } public JobFormGrid() { } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.SelectColumns = true; options.FilterRows = true; EditOnAdd = true; } protected override void Reload( Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { criteria.Add(MasterDetailFilter); base.Reload(criteria, columns, ref sort, token, action); } protected override bool CanCreateItems() { return base.CanCreateItems() && Master != null; } public override JobForm CreateItem() { var result = base.CreateItem(); result.Parent.ID = Master?.ID ?? Guid.Empty; result.Parent.Synchronise(Master ?? new Job()); return result; } } }