using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.Wpf; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PRSDesktop; public class JobScopeDocumentGrid : DynamicDocumentGrid, IJobScopeGrid { public JobScope? Scope { get => Item; set { Load(value ?? new JobScope(), null); } } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { if((Scope?.ID ?? Guid.Empty) == Guid.Empty) { MessageWindow.ShowMessage("Please select a scope.", "No scope selected."); return; } base.DoAdd(OpenEditorOnDirectEdit); } public override void SaveItem(JobScopeDocument item) { Client.Save(item, "Updated by user."); } public override void SaveItems(JobScopeDocument[] items) { Client.Save(items, "Updated by user."); } public override void DeleteItems(params CoreRow[] rows) { Client.Delete(rows.Select(x => new JobScopeDocument { ID = x.Get(x => x.ID) }), "Deleted by user."); } public override JobScopeDocument LoadItem(CoreRow row) { var id = row.Get(x => x.ID); return Client.Query( new Filter(x => x.ID).IsEqualTo(id), DynamicGridUtils.LoadEditorColumns(DataColumns())) .ToObjects() .FirstOrDefault() ?? throw new Exception($"No {nameof(JobScopeDocument)} with ID {id}"); } public override JobScopeDocument[] LoadItems(CoreRow[] rows) { var ids = rows.Select(x => x.Get(x => x.ID)); return Client.Query( new Filter(x => x.ID).InList(ids), DynamicGridUtils.LoadEditorColumns(DataColumns())) .ToObjects().ToArray(); } protected override void Reload(Filters criteria, Columns columns, ref SortOrder? sort, Action action) { if(Scope is null || Scope.ID == Guid.Empty) { criteria.Add(new Filter().None()); } else { criteria.Add(new Filter(x => x.EntityLink.ID).IsEqualTo(Scope?.ID ?? Guid.Empty)); } Client.Query(criteria.Combine(), columns, sort, action); } }