using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Threading; using System.Threading.Tasks; using comal.timesheets.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets.QAForms { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class EmployeeFormPicker : ContentPage { List employeeFormShells = new List(); Guid empID = Guid.Empty; List roles = new List(); bool firstLoad = true; Employee employee = new Employee(); public EmployeeFormPicker() { InitializeComponent(); empID = GlobalVariables.EmpID; filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged; roles.Add("All"); LoadEmployee(); NavigationPage.SetHasBackButton(this, false); } private void LoadEmployee() { Task.Run(() => { employee = new Client().Query(new Filter(x => x.ID).IsEqualTo(empID)).Rows.FirstOrDefault().ToObject(); }); } private void FilterOptionsControl_OnFilterOptionChanged(string filterOption) { if (filterOption == filterOptionsControl.CurrentOption) return; filterOptionsControl.CurrentOption = filterOption; if (filterOption == "All") { employeeFormDisplayList.ItemsSource = employeeFormShells; } else { employeeFormDisplayList.ItemsSource = employeeFormShells.Where(x => x.Role.Equals(filterOption)); } } void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } protected async override void OnAppearing() { LoadData(); base.OnAppearing(); } async void LoadData() { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Refreshing")) { Title = "Loading"; await Task.Run(() => { employeeFormShells.Clear(); string emptyString = ""; CoreTable table = new Client().Query( new Filter(x => x.Parent.ID).IsEqualTo(empID), new Columns( x => x.ID, //0 x => x.Form.Description, //1 x => x.FormCompleted, //2 x => x.FormData, //3 x => x.Created, //4 x => x.Form.ID //5 ), new SortOrder(x => x.Created, SortDirection.Descending) ); foreach (CoreRow row in table.Rows) { List list = row.Values; if (list[0] == null) { list[0] = Guid.Empty; } //0 if (list[1] == null) { list[1] = emptyString; } //1 if (list[2] == null) { list[2] = DateTime.MinValue; } //2 if (list[3] == null) { list[3] = emptyString; } //3 if (list[4] == null) { list[4] = DateTime.MinValue; } //4 if (list[5] == null) { list[5] = Guid.Empty; } //5 EmployeeFormShell shell = new EmployeeFormShell(); shell.ID = Guid.Parse(list[0].ToString()); shell.Description = list[1].ToString(); shell.Completed = DateTime.Parse(list[2].ToString()); shell.Created = "Created: " + DateTime.Parse(list[4].ToString()).ToString("dd MMM yy"); if (shell.Completed != DateTime.MinValue) { shell.Color = Color.FromHex("#15C7C1"); //light blue shell.Status = "Status: Completed " + shell.Completed.ToString("dd-MMMM-yy"); } else if (!string.IsNullOrWhiteSpace(list[3].ToString())) { shell.Color = Color.Orange; shell.Status = "Status: In Progress"; } else { shell.Color = Color.FromHex("#f08080"); //light coral / red shell.Status = "Status: Not started"; } CoreTable table2 = new Client().Query(new Filter(x => x.Form.ID).IsEqualTo(Guid.Parse(list[5].ToString())), new Columns(x => x.Role.Name)); if (table2.Rows.Any()) { List list2 = table2.Rows.FirstOrDefault().Values; if (list2[0] == null) { list2[0] = ""; } //0 string role = list2[0].ToString(); shell.Role = role; if (!roles.Contains(role)) { roles.Add(role); } } employeeFormShells.Add(shell); } Device.BeginInvokeOnMainThread(() => { if (firstLoad) { filterOptionsControl.Options = roles; filterOptionsControl.CreateRadioButtonsAndSetDefault(roles.First()); firstLoad = false; } employeeFormDisplayList.ItemsSource = null; employeeFormDisplayList.ItemsSource = employeeFormShells; Title = "My Employee Forms"; }); }); } } async void LayoutsList_Tapped(object sender, EventArgs e) { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { bool readOnly = false; EmployeeForm empForm = new EmployeeForm(); EmployeeFormShell shell = employeeFormDisplayList.SelectedItem as EmployeeFormShell; CoreTable table0 = new Client().Query( new Filter(x => x.ID).IsEqualTo(shell.ID), new Columns( x => x.ID, //0 x => x.Form.Description, //1 x => x.Form.AppliesTo, //2 x => x.FormCompleted, //3 x => x.FormCompletedBy.ID, //4 x => x.FormData, //5 x => x.Form.ID, //6 x => x.FormStarted, //7 x => x.FormOpen, //8 x => x.Parent.ID, x => x.BlobData ) ); if (table0.Rows.Any()) { empForm = table0.Rows.First().ToObject(); if (empForm.FormCompleted != DateTime.MinValue) { readOnly = true; } DigitalFormLayout layout = new DigitalFormLayout(); CoreTable table = new Client().Query( new Filter(x => x.Form.ID).IsEqualTo(empForm.Form.ID) ); if (table.Rows.Any()) { layout = table.Rows.FirstOrDefault().ToObject(); } var model = new DigitalFormHostModel(); model.LoadItems(employee, empForm, layout); DigitalFormHost host = new DigitalFormHost(model); Navigation.PushAsync(host); } } } catch { } } } public class EmployeeFormShell { public Guid ID { get; set; } public string Description { get; set; } public Color Color { get; set; } public DateTime Completed { get; set; } public string Status { get; set; } public string Created { get; set; } public string Role { get; set; } public EmployeeFormShell() { ID = Guid.Empty; Description = ""; Color = Color.Default; Completed = DateTime.MinValue; Status = ""; Created = ""; Role = ""; } } }