using com.sun.tools.doclets.formats.html;
using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.Reports;
using InABox.Reports.Common;
using InABox.Scripting;
using InABox.WPF;
using PRSDesktop.Configuration;
using PRSDesktop.Forms;
using PRSDesktop.WidgetGroups;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Converter;
using Syncfusion.Windows.Shared;
using Syncfusion.XlsIO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
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;
using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
namespace PRSDesktop
{
public enum DateFilterType
{
Today,
Yesterday,
Week,
SevenDays,
Month,
ThirtyDays,
Year,
TwelveMonths,
Custom
}
public class DFFilter : BaseObject
{
[EditorSequence(1)]
[TextBoxEditor]
public string Name { get; set; }
[EditorSequence(2)]
[FilterEditor]
public string Filter { get; set; }
protected override void Init()
{
base.Init();
Name = "";
Filter = "";
}
}
public class DigitalFormsDashboardProperties : IDashboardProperties
{
public bool ShowJobFilter { get; set; } = false;
public bool ShowDateFilter { get; set; } = true;
public Guid JobID { get; set; }
public DateFilterType DateFilterType { get; set; } = DateFilterType.Today;
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public Dictionary> Filters { get; set; } = new();
}
public class DigitalFormsDashboardElement : DashboardElement { }
///
/// Interaction logic for DigitalFormsDashboard.xaml
///
public partial class DigitalFormsDashboard : UserControl,
IDashboardWidget,
IRequiresSecurity,
IHeaderDashboard, IActionsDashboard
{
public DigitalFormsDashboardProperties Properties { get; set; }
private List DigitalForms;
private List Jobs;
private Dictionary Categories;
public DashboardHeader Header { get; set; } = new();
private bool IsQAForm = false;
private List Questions = new();
public DigitalFormsDashboard()
{
InitializeComponent();
}
public void Setup()
{
var results = Client.QueryMultiple(
new KeyedQueryDef(new Filter(x => x.Active).IsEqualTo(true)),
new KeyedQueryDef(
LookupFactory.DefineFilter(),
new Columns(x => x.ID)
.Add(x => x.JobNumber)
.Add(x => x.Name)));
DigitalForms = results.Get().ToList();
var categories = new DigitalFormCategoryLookups(null);
categories.OnAfterGenerateLookups += (sender, entries) => { entries.Insert(0, new LookupEntry("", "Select Category")); };
Categories = categories.AsTable("AppliesTo")
.ToDictionary("AppliesTo", "Display")
.Cast>()
.ToDictionary(x => (x.Key as string)!, x => x.Value);
Jobs = results.Get().ToList();
Jobs.Insert(0, new Job { ID = Guid.Empty, JobNumber = "ALL", Name = "All Jobs" });
SetupHeader();
SetupFilters();
}
#region Header
private ComboBox CategoryBox;
private ComboBox FormBox;
private ComboBox JobBox;
private ComboBox DateTypeBox;
private Label FromLabel;
private DatePicker FromPicker;
private Label ToLabel;
private DatePicker ToPicker;
private Button Print;
private static Dictionary FilterTypes = new()
{
{ DateFilterType.Today, "Today" },
{ DateFilterType.Yesterday, "Yesterday" },
{ DateFilterType.Week, "Week to Date" },
{ DateFilterType.SevenDays, "Last 7 Days" },
{ DateFilterType.Month, "Month to Date" },
{ DateFilterType.ThirtyDays, "Last 30 Days" },
{ DateFilterType.Year, "Year to Date" },
{ DateFilterType.TwelveMonths, "Last 12 Months" },
{ DateFilterType.Custom, "Custom" }
};
public void SetupHeader()
{
CategoryBox = new ComboBox {
Width = 150,
VerticalContentAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 5, 0)
};
CategoryBox.ItemsSource = Categories;
CategoryBox.SelectedValuePath = "Key";
CategoryBox.DisplayMemberPath = "Value";
CategoryBox.SelectionChanged += Category_SelectionChanged;
FormBox = new ComboBox
{
Width = 250,
VerticalContentAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 5, 0),
IsEnabled = false
};
FormBox.SelectionChanged += FormBox_SelectionChanged;
FormBox.ItemsSource = new Dictionary { };
JobBox = new ComboBox
{
Width = 250,
Margin = new Thickness(0, 0, 5, 0),
VerticalContentAlignment = VerticalAlignment.Center
};
JobBox.ItemsSource = Jobs.ToDictionary(x => x.ID, x => $"{x.JobNumber} : {x.Name}");
JobBox.SelectedIndex = 0;
JobBox.SelectedValuePath = "Key";
JobBox.DisplayMemberPath = "Value";
JobBox.SelectionChanged += JobBox_SelectionChanged;
DateTypeBox = new ComboBox
{
Width = 120,
VerticalContentAlignment = VerticalAlignment.Center
};
DateTypeBox.ItemsSource = FilterTypes;
DateTypeBox.SelectedValuePath = "Key";
DateTypeBox.DisplayMemberPath = "Value";
DateTypeBox.SelectedValue = Properties.DateFilterType;
DateTypeBox.SelectionChanged += DateTypeBox_SelectionChanged;
FromLabel = new Label { Content = "From", VerticalContentAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 5, 0) };
FromPicker = new DatePicker {
Width = 100,
Background = new SolidColorBrush(Colors.LightYellow),
VerticalContentAlignment = VerticalAlignment.Center,
FirstDayOfWeek = DayOfWeek.Monday,
Margin = new Thickness(0, 0, 5, 0)
};
FromPicker.SelectedDateChanged += FromPicker_SelectedDateChanged;
ToLabel = new Label { Content = "To", VerticalContentAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 5, 0) };
ToPicker = new DatePicker
{
Width = 100,
Background = new SolidColorBrush(Colors.LightYellow),
VerticalContentAlignment = VerticalAlignment.Center,
FirstDayOfWeek = DayOfWeek.Monday,
Margin = new Thickness(0, 0, 5, 0)
};
ToPicker.SelectedDateChanged += ToPicker_SelectedDateChanged;
Print = new Button
{
Width = 25,
Height = 25,
Content = new Image { Source = PRSDesktop.Resources.printer.AsBitmapImage() }
};
Print.Click += Print_Click;
Header.BeginUpdate()
.Clear()
.Add(CategoryBox)
.Add(FormBox)
.Add(JobBox)
.Add(DateTypeBox)
.Add(FromLabel)
.Add(FromPicker)
.Add(ToLabel)
.Add(ToPicker)
.AddRight(Print);
Header.EndUpdate();
}
private void Print_Click(object sender, RoutedEventArgs e)
{
var menu = new ContextMenu();
foreach (var report in ReportUtils.LoadReports(SectionName, DataModel(Selection.None)))
{
menu.AddItem(report.Name, PRSDesktop.Resources.printer, report, PrintReport_Click);
}
if (Security.IsAllowed())
{
menu.AddSeparatorIfNeeded();
menu.AddItem("Manage Reports", PRSDesktop.Resources.printer, ManageReports_Click);
}
menu.IsOpen = true;
}
private void PrintReport_Click(ReportTemplate obj)
{
Selection selection;
if (obj.SelectedRecords && obj.AllRecords)
selection = RecordSelectionDialog.Execute();
else if (obj.SelectedRecords)
selection = Selection.Selected;
else if (obj.AllRecords)
selection = Selection.All;
else
selection = Selection.None;
ReportUtils.PreviewReport(obj, DataModel(selection), false, Security.IsAllowed());
}
private void ManageReports_Click()
{
var manager = new ReportManager()
{
DataModel = DataModel(Selection.None),
Section = SectionName,
Populate = true
};
manager.ShowDialog();
}
private void Search_KeyUp(object sender, KeyEventArgs e)
{
Refresh();
}
private void JobBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Properties.JobID = (Guid)JobBox.SelectedValue;
Refresh();
}
private void SetDateFilterVisibility(bool visible)
{
var visibility = visible ? Visibility.Visible : Visibility.Collapsed;
FromLabel.Visibility = visibility;
FromPicker.Visibility = visibility;
ToLabel.Visibility = visibility;
ToPicker.Visibility = visibility;
DateTypeBox.Visibility = visibility;
}
private void SetJobFilterVisibility(bool visible)
{
var visibility = visible ? Visibility.Visible : Visibility.Collapsed;
JobBox.Visibility = visibility;
}
private void DateTypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var filterType = (DateFilterType)DateTypeBox.SelectedValue;
Properties.DateFilterType = filterType;
if(filterType == DateFilterType.Custom)
{
if (FromPicker.SelectedDate == null || FromPicker.SelectedDate == DateTime.MinValue)
{
Properties.FromDate = DateTime.Today;
}
else
{
Properties.FromDate = FromPicker.SelectedDate.Value;
}
if (ToPicker.SelectedDate == null || ToPicker.SelectedDate == DateTime.MinValue)
{
Properties.ToDate = DateTime.Today;
}
else
{
Properties.ToDate = ToPicker.SelectedDate.Value;
}
}
SetupDateFilters();
Refresh();
}
private void FromPicker_SelectedDateChanged(object? sender, SelectionChangedEventArgs e)
{
Properties.FromDate = FromPicker.SelectedDate ?? DateTime.Today;
}
private void ToPicker_SelectedDateChanged(object? sender, SelectionChangedEventArgs e)
{
Properties.ToDate = ToPicker.SelectedDate ?? DateTime.Today;
}
private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SetCategory((CategoryBox.SelectedValue as string)!);
var jobLink = FormType is not null ? GetJobLink("", FormType) : "";
if (string.IsNullOrWhiteSpace(jobLink))
{
var jobID = Properties.JobID;
JobBox.SelectedValue = Guid.Empty;
JobBox.IsEnabled = false;
Properties.JobID = jobID;
}
else
{
JobBox.SelectedValue = Properties.JobID;
JobBox.IsEnabled = true;
}
if (ParentType is null)
{
FormBox.IsEnabled = false;
FormBox.ItemsSource = new Dictionary { };
}
else
{
var forms = DigitalForms.Where(x => x.AppliesTo == ParentType.Name).ToList();
forms.Insert(0, new DigitalForm { ID = Guid.Empty, Description = "Select Form" });
FormBox.ItemsSource = forms;
FormBox.DisplayMemberPath = "Description";
FormBox.SelectedIndex = 0;
FormBox.IsEnabled = true;
}
Refresh();
}
private void FormBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Form = (FormBox.SelectedValue as DigitalForm)!;
Refresh();
}
#endregion
private string SectionName
{
get
{
if (Form is null || Form.ID == Guid.Empty)
return "Digital Forms";
return Form.ID.ToString() ?? "Digital Forms";
}
}
private DataModel DataModel(Selection selection)
{
if(FormType is null || Form is null || Form.ID == Guid.Empty)
{
return new AutoDataModel(new Filter().None());
}
IFilter filter;
switch (selection)
{
case Selection.Selected:
var formids = DataGrid.SelectedItems.Select(x => (x as DataRowView)!.Row["ID"]).ToArray();
filter = Filter.Create(FormType, x => x.ID).InList(formids);
break;
case Selection.All:
filter = Filter.Create(FormType).All();
break;
case Selection.None:
default:
filter = Filter.Create(FormType).None();
break;
}
return (Activator.CreateInstance(typeof(DigitalFormReportDataModel<>)!
.MakeGenericType(FormType), new object?[] { filter, Form.ID }) as DataModel)!;
}
public void BuildActionsMenu(ContextMenu menu)
{
menu.AddCheckItem