using Comal.Classes; using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using InABox.Mobile; using Xamarin.Forms; using Xamarin.Forms.Xaml; using LogType = InABox.Core.LogType; using SelectionChangedEventArgs = Syncfusion.XForms.Buttons.SelectionChangedEventArgs; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SiteManufacturing : SitePage { #region Constructor / Fields List packets = new List(); public SiteManufacturing() { InitializeComponent(); } protected override void JobLoaded() { RefreshOnJobChange(Job); LoadPackets(); } #endregion #region Loading private void LoadPackets() { Task.Run(() => { QueryAndAddPackets(); Device.BeginInvokeOnMainThread(() => { ShowList(); RefreshOnListChange(packets); }); }); } private void RefreshOnJobChange(JobDetailModel job) { packets.Clear(); RefreshOnListChange(packets); locationSearchEnt.Text = ""; serialSearchEnt.Text = ""; setoutSearchEnt.Text = ""; ShowLoading(); } private void ShowLoading() { loadingLbl.Text = "Loading..."; loadingCol.Width = new GridLength(1, GridUnitType.Star); loadingLbl.IsVisible = true; listCol.Width = 0; listView.IsVisible = false; } private void ShowList() { listCol.Width = new GridLength(1, GridUnitType.Star); listView.IsVisible = true; loadingCol.Width = 0; loadingLbl.IsVisible = false; } private void QueryAndAddPackets() { CoreTable packetstable = QueryPackets(); while (packetstable == null) packetstable = QueryPackets(); foreach (CoreRow row in packetstable.Rows) CreateAndAddPacket(row); } private CoreTable QueryPackets() { try { return new Client().Query( new Filter( x => x.SetoutLink.JobLink.ID).IsEqualTo(Job.Item.ID) .And(x => x.Serial).IsNotEqualTo(null) .And(x => x.Location).IsNotEqualTo(null) , new Columns( x => x.ID, x => x.OrderItem.ID, x => x.Serial, x => x.Location, x => x.SetoutLink.ID, x => x.SetoutLink.Number )); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } private void CreateAndAddPacket(CoreRow row) { packets.Add(new SiteManufacturingPacketShell { ID = row.Get(x => x.ID), OrderID = row.Get(x => x.OrderItem.ID), Serial = "Serial: " + row.Get(x => x.Serial), Location = "Location: " + row.Get(x => x.Location), SetoutID = row.Get(x => x.SetoutLink.ID), Setout = "Setout: " + row.Get(x => x.SetoutLink.Number) }); } #endregion #region Taps private void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } private void Packet_Tapped(object sender, EventArgs e) { try { SiteManufacturingPacketShell packet = listView.SelectedItem as SiteManufacturingPacketShell; ManufacturingPacketPopup popup = new ManufacturingPacketPopup(packet.ID, packet.OrderID); Navigation.PushAsync(popup); } catch (Exception ex) { DisplayAlert("Error", ex.Message, "OK"); } } private void PDFs_Tapped(object sender, EventArgs e) { try { var item = ((TappedEventArgs)e).Parameter as SiteManufacturingPacketShell; if (item == null) return; Dictionary fileNameIDS = new Dictionary(); Filter filter = new Filter(x => x.EntityLink.ID).IsEqualTo(item.SetoutID) .And(x => x.Superceded).IsEqualTo(DateTime.MinValue); CoreTable table = new Client().Query ( filter, new Columns(x => x.DocumentLink.ID, x => x.DocumentLink.FileName) ); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { if (!fileNameIDS.ContainsKey(row.Get("DocumentLink.FileName"))) fileNameIDS.Add(row.Get("DocumentLink.FileName"), row.Get("DocumentLink.ID")); } PDFList list = new PDFList(); //fileNameIDS); Navigation.PushAsync(list); } else DisplayAlert("Alert", "No Drawings found", "OK"); } catch (Exception ex) { DisplayAlert("Error", ex.Message, "OK"); } } #endregion #region Searching List serialSearchList = new List(); List locationSearchList = new List(); List setoutSearchList = new List(); bool bSerialSearch = false; bool bLocationSearch = false; bool bSetoutSearch = false; private void SerialSearchEnt_Changed(object sender, EventArgs e) { serialSearchList.Clear(); if (!string.IsNullOrWhiteSpace(serialSearchEnt.Text)) { bSerialSearch = true; serialSearchList = CreateSerialSearchList(packets); } else bSerialSearch = false; RunSearch(); } private void LocationSearchEnt_Changed(object sender, EventArgs e) { locationSearchList.Clear(); if (!string.IsNullOrWhiteSpace(locationSearchEnt.Text)) { bLocationSearch = true; locationSearchList = CreateLocationSearchList(packets); } else bLocationSearch = false; RunSearch(); } private void SetoutSearchEnt_Changed(object sender, EventArgs e) { setoutSearchList.Clear(); if (!string.IsNullOrWhiteSpace(setoutSearchEnt.Text)) { bSetoutSearch = true; setoutSearchList = CreateSetoutSearchList(packets); } else bSetoutSearch = false; RunSearch(); } private void RunSearch() { List searchList = new List(); if (bSerialSearch && bLocationSearch && bSetoutSearch) { searchList = serialSearchList; searchList = CreateLocationSearchList(searchList); searchList = CreateSetoutSearchList(searchList); } else if (bSerialSearch && bLocationSearch && !bSetoutSearch) { searchList = serialSearchList; searchList = CreateLocationSearchList(searchList); } else if (bSerialSearch && !bLocationSearch && bSetoutSearch) { searchList = serialSearchList; searchList = CreateSetoutSearchList(searchList); } else if (!bSerialSearch && bLocationSearch && bSetoutSearch) { searchList = locationSearchList; searchList = CreateSetoutSearchList(searchList); } else if (bSerialSearch && !bLocationSearch && !bSetoutSearch) searchList = serialSearchList; else if (!bSerialSearch && bLocationSearch && !bSetoutSearch) searchList = locationSearchList; else if (!bSerialSearch && !bLocationSearch && bSetoutSearch) searchList = setoutSearchList; if (bSerialSearch || bLocationSearch || bSetoutSearch) RefreshOnListChange(searchList); else RefreshOnListChange(packets); } private List CreateLocationSearchList(List sublist) { return sublist .Where(x => x.Location.ToUpper().Contains(locationSearchEnt.Text.ToUpper())) .ToList(); } private List CreateSerialSearchList(List sublist) { return sublist .Where(x => x.Serial.ToUpper().Contains(serialSearchEnt.Text.ToUpper())) .ToList(); } private List CreateSetoutSearchList(List sublist) { return sublist .Where(x=>x.Setout.ToUpper().Contains(setoutSearchEnt.Text.ToUpper())) .ToList(); } private void RefreshOnListChange(IEnumerable list) { listView.ItemsSource = null; listView.ItemsSource = list; Title = "Packets (" + list.Count() + ")"; } #endregion private void Display_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { throw new NotImplementedException(); } } #region Classes /// /// ViewModel class for SiteManufacturing Module /// public class SiteManufacturingPacketShell { public Guid ID { get; set; } public Guid OrderID { get; set; } public Guid SetoutID { get; set; } public string Serial { get; set; } public string Location { get; set; } public string Setout { get; set; } public SiteManufacturingPacketShell() { ID = Guid.Empty; OrderID = Guid.Empty; Serial = ""; Location = ""; SetoutID = Guid.Empty; Setout = ""; } } #endregion }