using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; namespace PRSDesktop { internal class EmployeeRoleGrid : DynamicDataGrid { public EmployeeRoleGrid() { ColumnsTag = "EmployeeRoles"; } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.Clear(); options.SelectColumns = true; options.AddRows = true; options.DeleteRows = true; options.MultiSelect = true; } public Guid EmployeeID { get; set; } protected override void Reload( Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { if (EmployeeID == Guid.Empty) criteria.Add(new Filter().None()); else criteria.Add(new Filter(x => x.EmployeeLink).LinkValid(EmployeeID)); base.Reload(criteria, columns, ref sort, token, action); } protected override bool CanCreateItems() { return EmployeeID != Guid.Empty; } protected override bool CanDeleteItems(params CoreRow[] rows) { return EmployeeID != Guid.Empty; } public override EmployeeRole CreateItem() { var result = base.CreateItem(); result.EmployeeLink.ID = EmployeeID; return result; } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { var ids = Data.ExtractValues(c => c.RoleLink.ID).Distinct().ToArray(); MultiSelectDialog dlg = new MultiSelectDialog( new Filter(x => x.ID).NotInList(ids), Columns.None().Add(x => x.ID).Add(x => x.Code).Add(x => x.Description), true); if (dlg.ShowDialog() == true) { List updates = new List(); foreach (var newid in dlg.IDs()) { EmployeeRole newrole = new EmployeeRole(); newrole.EmployeeLink.ID = EmployeeID; newrole.RoleLink.ID = newid; updates.Add(newrole); } if (updates.Any()) { new Client().Save(updates, "Updated by User"); Refresh(false, true); DoChanged(); } } } } }