using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using PRSClasses; using Xamarin.Forms; using Xamarin.Forms.Xaml; using comal.timesheets.QAForms; using InABox.Clients; using Comal.Classes; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets { public class DigitalFormsHostClosingArgs : EventArgs { public bool Changed { get; } public DigitalFormsHostClosingArgs(bool changed) { Changed = changed; } } public delegate void DigitalFormsHostClosingDelegate(object sender, DigitalFormsHostClosingArgs args); [XamlCompilation(XamlCompilationOptions.Compile)] public partial class DigitalFormHost : ContentPage { public event DigitalFormsHostClosingDelegate OnClosing; public IDigitalFormHostModel Model { get; private set; } QAFormViewer viewer; bool readOnly = false; Dictionary fileNameDocIDs = new Dictionary(); public DigitalFormHost(IDigitalFormHostModel model, Guid jobid = default(Guid)) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); Model = model; //titleLbl.Text = Model.DigitalFormDataModel.Instance.Form.Description; Model.OnDigitalFormHostModelBeforeSave += () => { Model.SetPropertyValues(viewer); }; Model.OnDigitalFormHostModelSaved += () => { DisplayAlert("Success", "Form completed: " + Model.DigitalFormDataModel.Instance.Form.Description, "OK"); }; saveBtn.IsEnabled = !Model.ReadOnly; //saveProgressBtn.IsEnabled = !model.ReadOnly; viewer = new QAFormViewer(Model.DigitalFormDataModel, Model.DFLayout, Model.NewForm, Model.ReadOnly, jobid); LoadDocs(); formViewerScroller.Content = viewer; } private void ExitBtn_Clicked(object sender, EventArgs e) { RetainedResults.IsFormRetained = false; OnClosing?.Invoke(this,new DigitalFormsHostClosingArgs(false)); Navigation.PopAsync(); } private async void SaveProgressBtn_Clicked(object sender, EventArgs e) { if (!readOnly) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Progress")) { viewer.SaveData(true); } } Navigation.PopAsync(); } private void SaveBtn_Clicked(object sender, EventArgs e) { SaveOptions(); } private async void DocumentBtn_Clicked(object sender, EventArgs e) { if (fileNameDocIDs.Count > 1) { PDFList pdfList = new PDFList(fileNameDocIDs, true); Navigation.PushAsync(pdfList); } else if (fileNameDocIDs.Count == 1) { PDFViewer pDFViewer = new PDFViewer(fileNameDocIDs.Values.First(), true); Device.BeginInvokeOnMainThread(() => { Navigation.PushAsync(pDFViewer); }); } } private void LoadDocs() { try { Task.Run(() => { CoreTable table = new Client().Query ( new Filter(x => x.EntityLink.ID).IsEqualTo(Model.DigitalFormLayout.Form.ID), new Columns(x => x.DocumentLink.ID, x => x.DocumentLink.FileName) ); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { List list = row.Values; if (list[0] == null) list[0] = Guid.Empty; if (list[1] == null) list[1] = ""; fileNameDocIDs.Add(list[1].ToString(), Guid.Parse(list[0].ToString())); } Device.BeginInvokeOnMainThread(() => { documentBtn.IsVisible = true; documentBtn.Text = "View Attached Document(s) (" + table.Rows.Count + ")"; }); } }); } catch { } } private async void SaveOptions() { try { string chosenOption = ""; if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban")) chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate"); else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban") || Model.DigitalFormLayout.Form.AppliesTo.Equals("Job")) chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate"); else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Product")) chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form"); else chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form"); if (!string.IsNullOrEmpty(chosenOption)) { if (!chosenOption.Equals("Cancel")) { bool saveForLater = false; if (chosenOption.Equals("Save Progress")) { saveForLater = true; ClearRetainedStatusAndResults(); } else if (chosenOption.Equals("Complete Form")) { ClearRetainedStatusAndResults(); } else if (chosenOption.Equals("Complete and Duplicate")) { RetainedResults.IsFormRetained = true; RetainedResults.RetainedFormName = Model.DigitalFormLayout.Description; RetainedResults.Results = new Dictionary(); } using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving")) { viewer.SaveData(saveForLater); if (viewer.isRequiredEmpty) { Device.BeginInvokeOnMainThread(async () => { await DisplayAlert("Alert", "Please fill in compulsory field \"" + viewer.isRequiredMessage + "\" in order to submit form. (Compulsory fields are highlighted in orange)." , "OK"); }); return; } if (viewer.errors.Count > 0) { string message = ""; int count = 1; foreach (string s in viewer.errors) { if (s.Contains("same key")) { string[] arrary = s.Split("Key"); message = message + count + ". " + "Please check with the person that designs your forms - a duplicate variable property is present (" + (arrary[arrary.Length - 1]).Remove(0, 1) + ")" + System.Environment.NewLine; } else { message = message + count + ". " + s + System.Environment.NewLine; count++; } } Device.BeginInvokeOnMainThread(() => { DisplayAlert("Form saved but errors may be present", message, "OK"); }); } } OnClosing?.Invoke(this,new DigitalFormsHostClosingArgs(true)); Navigation.PopAsync(); } } } catch (Exception ex) { DisplayAlert("Alert", "Unable to save. Issues: " + Environment.NewLine + ex.Message, "OK"); } } private void ClearRetainedStatusAndResults() { try { RetainedResults.IsFormRetained = false; RetainedResults.Results.Clear(); } catch { return; } } } }