using FastReport; using InABox.Clients; using InABox.Core; using InABox.Core.Reports; using InABox.Wpf.Reports; using InABox.WPF; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Controls; using System.Windows.Media.Imaging; using FastReport.Table; using FastReport.Utils; using Border = System.Windows.Controls.Border; using Size = System.Windows.Size; using netDxf.Objects; namespace InABox.DynamicGrid { public class DigitalFormReportGrid : DynamicGrid, IDynamicEditorPage { public DynamicEditorGrid EditorGrid { get; set; } public PageType PageType => PageType.Other; public bool Ready { get; set; } private DigitalForm Form { get; set; } private bool _readOnly; public bool ReadOnly { get => _readOnly; set { if (_readOnly != value) { _readOnly = value; Reconfigure(); } } } protected new DynamicGridMemoryEntityDataComponent DataComponent; protected override void Init() { if (Security.CanEdit()) { ActionColumns.Add(new DynamicImageColumn(ScriptImage, ScriptClick)); ActionColumns.Add(new DynamicImageColumn(Wpf.Resources.pencil.AsBitmapImage(), DesignClick)); } DataComponent = new DynamicGridMemoryEntityDataComponent(this); base.DataComponent = DataComponent; } protected override void DoReconfigure(DynamicGridOptions options) { options.ShowHelp = true; if (Security.CanEdit() && !ReadOnly) { options.AddRows = true; options.EditRows = true; } if (Security.CanDelete() && !ReadOnly) options.DeleteRows = true; } public void Load(object item, Func? PageDataHandler) { Form = (DigitalForm)item; Refresh(true, false); var data = PageDataHandler?.Invoke(typeof(ReportTemplate)); if(data is null && Form.ID == Guid.Empty) { data = new CoreTable(); data.LoadColumns(typeof(ReportTemplate)); } if(data is not null) { DataComponent.LoadData(data); } else { DataComponent.LoadData( new Filter(x => x.Section).IsEqualTo(Form.ID.ToString()), Columns.All(), null); } Refresh(false, true); Ready = true; } private DynamicVariableGrid? GetVariableGrid() => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid) as DynamicVariableGrid; private List GetVariables() => GetVariableGrid()?.Items.ToList() ?? new List(); private DynamicFormLayoutGrid? GetLayoutGrid() => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid) as DynamicFormLayoutGrid; private List GetLayouts() => GetLayoutGrid()?.Items.ToList() ?? new List(); private BitmapImage? ScriptImage(CoreRow? arg) { return arg == null ? Wpf.Resources.edit.AsBitmapImage() : arg.Get(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage(); } private bool ScriptClick(CoreRow? arg) { if (arg != null && !ReadOnly) { if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model) { Logger.Send(LogType.Error, "", "Invalid entity type for data model."); return false; } var template = DataComponent.LoadItem(arg); var script = template.Script; if (string.IsNullOrWhiteSpace(script)) script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last()); var editor = new ScriptEditorWindow(script); if (editor.ShowDialog() == true) { template.Script = editor.Script; DataComponent.SaveItem(template); return true; } } return false; } private bool DesignClick(CoreRow? arg) { if (arg is null && !ReadOnly) return false; if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model) { Logger.Send(LogType.Error, "", "Invalid entity type for data model."); return false; } var template = DataComponent.LoadItem(arg); ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => DataComponent.SaveItem(template)); return false; } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { var layouts = GetLayouts(); if(layouts.Count == 0) { base.DoAdd(OpenEditorOnDirectEdit); } else { var menu = new ContextMenu(); foreach (var layout in layouts) { menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout); } menu.AddSeparatorIfNeeded(); menu.AddItem("Create blank report", null, () => base.DoAdd()); menu.IsOpen = true; } } private void AddLayout(DigitalFormLayout layout) { var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables()); var item = CreateItem(); item.DataModel = model.Name; item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description; item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString(); if (EditItems(new[] { item })) { DataComponent.SaveItem(item); Refresh(false, true); } } public void BeforeSave(object item) { } public void AfterSave(object item) { foreach (var template in DataComponent.Items) { template.Section = Form.ID.ToString(); } DataComponent.SaveItems(); } public override ReportTemplate CreateItem() { var item = base.CreateItem(); var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables()); item.DataModel = model.Name; if (Form.ID != Guid.Empty) { item.Section = Form.ID.ToString(); } return item; } public string Caption() => "Reports"; public Size MinimumSize() { return new Size(400, 400); } public int Order() { return int.MinValue; } } }