using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { internal class FactorySectionsGrid : DynamicGrid { private string _CurrentSection = ""; private readonly List CurrentSections = new(); public BitmapImage QAColor = PRSDesktop.Resources.quality.AsBitmapImage(Color.White); public BitmapImage QAGray = PRSDesktop.Resources.quality.AsGrayScale().AsBitmapImage(Color.White); public FactorySectionsGrid() { Options.AddRange(DynamicGridOption.RecordCount); ActionColumns.Add(new DynamicActionColumn(QualityImage, QualityAction)); HiddenColumns.Add(x => x.QualityChecks); ActionColumns.Add(new DynamicRowMovementColumn(DynamicRowMovement.Up, SwapRows)); ActionColumns.Add(new DynamicRowMovementColumn(DynamicRowMovement.Down, SwapRows)); } public string CurrentGroup { get => _CurrentSection; set { if (!string.IsNullOrEmpty(_CurrentSection)) { Sections.RemoveAll(x => x.Group.Equals(_CurrentSection)); Sections.AddRange(CurrentSections); } _CurrentSection = value; CurrentSections.Clear(); if (!string.IsNullOrEmpty(_CurrentSection)) CurrentSections.AddRange(Sections.Where(x => x.Group.Equals(_CurrentSection))); } } public List Sections { get; set; } private bool QualityAction(CoreRow arg) { if (arg != null) { var form = new NotesForm { Caption = "Please enter the Quality Checks to be performed before starting work on this stage", Text = CurrentSections[arg.Index].QualityChecks }; if (form.ShowDialog() == true) { CurrentSections[arg.Index].QualityChecks = form.Text; return true; } } return false; } private BitmapImage QualityImage(CoreRow arg) { if (arg == null) return QAColor; if (string.IsNullOrWhiteSpace(arg.Get(x => x.QualityChecks))) return QAGray; return QAColor; } private bool SwapRows(int arg1, int arg2) { var item = CurrentSections[arg1]; CurrentSections.Remove(item); CurrentSections.Insert(arg2, item); return true; } //public DynamicGridColumns DefineColumns() //{ // return LoadColumns(); //} protected override DynamicGridColumns LoadColumns() { var columns = new DynamicGridColumns { new() { ColumnName = "Name", /* Type = typeof(String), */ Width = 0 }, new() { ColumnName = "Stations", Caption = "Stns", Width = 40, Alignment = Alignment.MiddleCenter }, new() { ColumnName = "Hidden", Caption = "Hide", Width = 30 } //new DynamicGridColumn() { ColumnName = "Shared", Width = 30}, }; return columns; } #region Save / Load //protected override DataTable Reload(Dictionary criteria, List columnnames, String sort) //{ // DataTable result = new DataTable(); // result.LoadColumns(typeof(FactorySection)); // result.LoadRows(CurrentSections); // return result; //} protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { var result = new CoreTable(); result.LoadColumns(typeof(FactorySection)); result.LoadRows(CurrentSections); action.Invoke(result, null); } protected override FactorySection LoadItem(CoreRow row) { var index = Data.Rows.IndexOf(row); return CurrentSections[index]; } protected override void SaveItem(FactorySection item) { if (!CurrentSections.Contains(item)) CurrentSections.Add(item); } protected override void DeleteItems(params CoreRow[] rows) { foreach (var row in rows.OrderByDescending(x => x.Index)) CurrentSections.RemoveAt(row.Index); } protected override FactorySection CreateItem() { var section = base.CreateItem(); section.Group = CurrentGroup; section.ID = Guid.NewGuid(); return section; } #endregion } }