using System; using System.Collections.Generic; using Xamarin.Forms; using InABox.Core; using Comal.Classes; using InABox.Clients; using System.Linq; using System.Threading.Tasks; using Syncfusion.XForms.PopupLayout; using Xamarin.Essentials; using System.Windows.Input; using PRSSecurity = InABox.Core.Security; namespace comal.timesheets { class StaffStatus { public Guid ID { get; set; } public String StaffName { get; set; } public String In { get; set; } public String Out { get; set; } public string Mobile { get; set; } public string StaffLocation { get; set; } public bool InTimeVisible { get; set; } public bool OutTimeVisible { get; set; } public bool InImageVisible { get; set; } public bool OutImageVisible { get; set; } public bool HiddenRowVisible { get; set; } public int HiddenRowHeight { get; set; } public StaffStatus() { ID = Guid.Empty; StaffName = ""; In = ""; Out = ""; StaffLocation = ""; InTimeVisible = true; OutTimeVisible = true; InImageVisible = false; OutImageVisible = false; HiddenRowVisible = false; HiddenRowHeight = 0; Mobile = ""; } } public partial class StaffStatusPage : ContentPage { CoreTable employees = null; CoreTable timesheets = null; bool bManager = false; public StaffStatusPage() { InitializeComponent(); if (PRSSecurity.IsAllowed()) bManager = true; ConfigurePage(); } private void ConfigurePage() { NavigationPage.SetHasBackButton(this, false); ToolbarItems.Clear(); ToolbarItems.Add(new ToolbarItem("Back", "", () => { Navigation.PopAsync(); })); popupLayout.PopupView.AppearanceMode = AppearanceMode.OneButton; popupLayout.PopupView.HeaderTitle = "Contact Details"; Title = "In/Out Board"; } protected override void OnAppearing() { base.OnAppearing(); LoadData(); } private void StaffList_Tapped(object sender, EventArgs e) { CreatePopup((StaffList.SelectedItem as StaffStatus).Mobile); popupLayout.Show(); } private void CreatePopup(string mobile) { popupLayout.PopupView.FooterTemplate = new DataTemplate(() => { return CreateFrame(mobile); }); popupLayout.PopupView.ContentTemplate = new DataTemplate(() => { return CreatePopupContent(mobile); }); } private Label CreatePopupContent(string mobile) { return new Label { Text = "Mobile: " + mobile, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center, FontAttributes = FontAttributes.Bold, FontSize = 24 }; } private Grid CreateGrid(Image img, Label label) { var grid = new Grid { BackgroundColor = Color.FromHex("#15C7C1"), RowSpacing = 0, ColumnSpacing = 0, Padding = 0, Margin = 0 }; grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); Grid.SetColumn(img, 0); Grid.SetColumn(label, 1); grid.Children.Add(img); grid.Children.Add(label); return grid; } private Image CreateImage(string mobile) { Image img = new Image { Source = "call.png", HeightRequest = 40, WidthRequest = 40, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, Margin = 0, }; img.GestureRecognizers.Add(new TapGestureRecognizer { Command = CallPerson(mobile) }); return img; } private Label CreateCallLabel(string mobile) { Label label = new Label { Text = "Call", FontAttributes = FontAttributes.Bold, FontSize = 24, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, HorizontalTextAlignment = TextAlignment.Start, VerticalTextAlignment = TextAlignment.Center, BackgroundColor = Color.FromHex("#15C7C1"), TextColor = Color.White, Margin = 0 }; label.GestureRecognizers.Add(new TapGestureRecognizer { Command = CallPerson(mobile) }); return label; } private Frame CreateFrame(string mobile) { var img = CreateImage(mobile); var label = CreateCallLabel(mobile); var grid = CreateGrid(img, label); Frame frame = new Frame { Content = grid, Padding = 0, Margin = 0, BackgroundColor = Color.FromHex("#15C7C1") }; frame.GestureRecognizers.Add(new TapGestureRecognizer { Command = CallPerson(mobile) }); return frame; } private ICommand CallPerson(string mobile) { return new Command(() => { try { PhoneDialer.Open(mobile); } catch (Exception e2) { string s = e2.Message; } }); } private void LoadData() { Task.Run(() => { ShowLoading(); CoreTable employees = QueryEmployees(); CoreTable timesheets = QueryTimeSheets(); List staff = new List(); foreach (CoreRow row in employees.Rows) staff.Add(CreateStatus(row, timesheets)); ShowList(staff); }); } private void ShowList(List staff) { Device.BeginInvokeOnMainThread(() => { loadingColumn.Width = 0; listViewColumn.Width = new GridLength(1, GridUnitType.Star); loadingLayout.IsVisible = false; StaffList.IsVisible = true; StaffList.ItemsSource = staff; }); } private StaffStatus CreateStatus(CoreRow row, CoreTable timesheets) { Guid empid = row.Get(x => x.ID); String name = row.Get(x => x.Name); CoreRow startrow = timesheets.Rows.FirstOrDefault(r => r.Get(c => c.EmployeeLink.ID).Equals(empid)); CoreRow finishrow = timesheets.Rows.LastOrDefault(r => r.Get(c => c.EmployeeLink.ID).Equals(empid)); String sIn = ""; String sOut = ""; if (startrow != null) { TimeSpan start = startrow.Get(c => c.Start); TimeSpan finish = finishrow.Get(c => c.Finish); sIn = String.Format("{0:hh\\:mm} ", start); if (finish.Ticks > 0) sOut = String.Format("{0:hh\\:mm}", finish); } var status = new StaffStatus() { StaffName = row.Get(x => x.Name), ID = empid, In = sIn, Out = sOut }; if (!string.IsNullOrWhiteSpace(row.Get(x => x.Mobile))) status.Mobile = row.Get(x => x.Mobile); if (!bManager) status = ShowDots(status); else if (startrow != null) status = ShowManagerOptions(status, startrow); return status; } private StaffStatus ShowManagerOptions(StaffStatus status, CoreRow startrow) { status.StaffLocation = startrow.Get("Address"); if (!string.IsNullOrWhiteSpace(status.StaffLocation)) { status.HiddenRowVisible = true; status.HiddenRowHeight = 30; } return status; } private StaffStatus ShowDots(StaffStatus status) { status.InTimeVisible = false; status.OutTimeVisible = false; if (!string.IsNullOrWhiteSpace(status.In)) status.InImageVisible = true; if (!string.IsNullOrWhiteSpace(status.Out)) status.OutImageVisible = true; return status; } private void ShowLoading() { Device.BeginInvokeOnMainThread(async () => { for (int i = 0; i < 10; i++) { Random random = new Random(); uint number = (uint)random.Next(500, 3000); await loadingLbl.TranslateTo(0, 15, 500); await loadingLbl.TranslateTo(0, 0, 500); loadingLbl.RotateTo(360, number); await loadingLbl.TranslateTo(0, 15, 500); await loadingLbl.TranslateTo(0, 0, 500); await loadingLbl.TranslateTo(0, 15, 500); await loadingLbl.TranslateTo(0, 0, 500); await loadingLbl.TranslateTo(0, 15, 500); number = (uint)random.Next(500, 3000); await loadingLbl.TranslateTo(0, 0, 500); loadingLbl.RotateTo(360, number); } }); } private CoreTable QueryEmployees() { return new Client().Query( LookupFactory.DefineFilter().And (x => x.Name).IsNotEqualTo("Administrator"), new Columns(x => x.ID, x => x.Name, x => x.Mobile), new SortOrder(x => x.Name) ); } private CoreTable QueryTimeSheets() { return new Client().Query( new Filter(x => x.Date).IsEqualTo(DateTime.Today), new Columns(x => x.EmployeeLink.ID, x => x.Start, x => x.Finish, x => x.Address), new SortOrder(x => x.EmployeeLink.ID).ThenBy(x => x.Start) ); } } }