using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { internal class JobScopeGrid : DynamicDataGrid { private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage(); public JobScopeGrid() { HiddenColumns.Add(x=>x.Job.DefaultScope.ID); HiddenColumns.Add(x=>x.Job.ID); ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.Start }); } protected override void DoReconfigure(FluentList options) { base.DoReconfigure(options); options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows); } private BitmapImage? IsDefaultImage(CoreRow? row) { return row == null ? tick : row.Get(x => x.Job.DefaultScope.ID) == row.Get(x => x.ID) ? tick : null; } private bool SelectDefaultAction(CoreRow? row) { if ((row == null) ||row.Get(x => x.Job.DefaultScope.ID) == row.Get(x => x.ID)) return false; using (new WaitCursor()) { var job = new Client().Query( new Filter(x => x.ID).IsEqualTo(row.Get(x => x.Job.ID)), new Columns(x => x.ID).Add(x => x.DefaultScope.ID) ).Rows.FirstOrDefault()?.ToObject(); if (job != null) { job.DefaultScope.ID = row.Get(x => x.ID); new Client().Save(job, "Updated Default Scope"); return true; } } return false; } public Job Job { get; set; } protected override bool CanCreateItems() { return Job.ID != Guid.Empty; } protected override JobScope CreateItem() { var result = base.CreateItem(); result.Job.ID = Job.ID; result.Job.Synchronise(Job); result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract; return result; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder? sort, Action action) { criteria.Add(new Filter(x => x.Job.ID).IsEqualTo(Job.ID)); base.Reload(criteria, columns, ref sort, action); } } }