using Comal.Classes; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PRSDesktop; /// /// Interaction logic for DigitalFormsLibrary.xaml /// public partial class DigitalFormsLibrary : UserControl, IPanel { private string? CurrentGroup = null; private bool AllItems = true; public List> GroupList { get; private set; } private bool _refreshing; public DigitalFormsLibrary() { InitializeComponent(); } #region Panel Stuff public bool IsReady { get; set; } public string SectionName => "Digital Forms Library"; public event DataModelUpdateEvent? OnUpdateDataModel; public Dictionary Selected() { return new Dictionary { { typeof(DigitalForm).EntityName(), Forms.SelectedRows } }; } public DataModel DataModel(Selection selection) { var ids = Forms.ExtractValues(x => x.ID, selection).ToArray(); return new AutoDataModel(new Filter(x => x.ID).InList(ids)); } public void CreateToolbarButtons(IPanelHost host) { } public void Setup() { Forms.Refresh(true, false); RolesTab.Visibility = Security.CanView() && Security.CanView() && Security.CanView() ? Visibility.Visible : Visibility.Collapsed; var visibleTabItems = Tab.Items.OfType().Where(x => x.Visibility == Visibility.Visible).ToList(); if (visibleTabItems.Count <= 1) { foreach (var tab in visibleTabItems) { tab.Visibility = Visibility.Collapsed; Tab.SelectedItem = tab; } } } private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.OriginalSource != Tab) return; RefreshCurrentTab(); } private void RefreshCurrentTab() { if (Tab.SelectedTab == FormsTab) { RefreshFormsTab(); } else if (Tab.SelectedTab == RolesTab) { RefreshRoleTab(); } } private void RefreshFormsTab() { Forms.Refresh(false, true); } private void RefreshRoleTab() { DigitalFormRoles.Refresh(true, true); } public void Refresh() { RefreshCurrentTab(); } public void Heartbeat(TimeSpan time) { } public void Shutdown(CancelEventArgs? cancel) { } #endregion private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (_refreshing || e.OriginalSource != Groups) return; string? newCurrentGroup; if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0) { AllItems = true; newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null; } else { AllItems = false; var selected = (Tuple)e.AddedItems[0]; newCurrentGroup = selected.Item2; } if (!Equals(newCurrentGroup, CurrentGroup)) { CurrentGroup = newCurrentGroup; Forms.Refresh(false, false); } } private void Forms_AfterRefresh(object sender, InABox.DynamicGrid.AfterRefreshEventArgs args) { if (Forms.MasterData is null) return; _refreshing = true; GroupList = new List> { new Tuple("All Items", null, PRSDesktop.Resources.doc_misc.AsBitmapImage()) }; foreach (var row in Forms.MasterData!.Rows) { var appliesTo = row.Get(x => x.AppliesTo); var display = appliesTo.NotWhiteSpaceOr("Unassigned"); if (!GroupList.Any(x => x.Item1.Equals(display))) GroupList.Add(new Tuple(display, appliesTo, PRSDesktop.Resources.doc_misc.AsBitmapImage())); } GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList(); Groups.ItemsSource = GroupList; Groups.Visibility = Visibility.Visible; var index = GroupList.FindIndex(x => Equals(x.Item2, CurrentGroup)); if(index == -1) { index = 0; CurrentGroup = null; Groups.SelectedIndex = 0; AllItems = true; _refreshing = false; Forms.Refresh(false, false); } else { Groups.SelectedIndex = index; AllItems = index == 0; _refreshing = false; } } private void Forms_OnCreateItem(object sender, object item) { if (item is not DigitalForm form) return; form.AppliesTo = CurrentGroup ?? ""; } private bool Forms_OnFilterRecord(CoreRow row) { return AllItems || Equals(row.Get(x => x.AppliesTo), CurrentGroup); } }