using Microsoft.AspNetCore.Components; using Newtonsoft.Json; using Telerik.Blazor; using Telerik.Blazor.Components; using TireTargets.Components.CSharp; using TireTargets.Components.Layout; namespace TireTargets.Components.Pages { public partial class Selection { /* * * Code relating to the location selection * */ private List locInfo = new List(); private List<(int, bool)> locSelections = new List<(int, bool)> (); private Dictionary locationState = new Dictionary (); private bool dummy = false; protected override async Task OnInitializedAsync() { await FetchAvailableOffers(); locInfo = await DatabaseDriver.GetLocationInfoForUser(MainLayout.Session["id"], MainLayout.Session["login_method"]); locSelections = await DatabaseDriver.DoesLocationNeedSelection(); foreach (var location in locInfo) { locationState.Add(location.LocationID, false); } await base.OnInitializedAsync(); } private bool DoesLocIDNeedSelection(int id) { foreach ((int, bool) value in locSelections) { if (value.Item1 == id) return value.Item2; } return false; } private void ConfirmSelection() { //PrintLocStates(); ShowWizard = true; } private void PrintLocStates() { foreach(var current in locationState) { Console.WriteLine("" + current.Key + " | " + current.Value); } } public void SelectLocation(int locID) { Console.WriteLine("Toggled value for " + locID); locationState[locID] = !locationState[locID]; } /* * * * Wizard portion * * */ private bool ShowWizard = false; private bool WizardComplete = false; [CascadingParameter] private DialogFactory Dialog { get; set; } // Tire offer variables private List<(string, string)> availableTireOffers = new List<(string, string)>(); private List selectedTireOffers = new List(); // Service offer variables private List<(string, string)> availableServiceOffers = new List<(string, string)>(); private List selectedServiceOffers = new List(); private async Task FetchAvailableOffers() { if (availableTireOffers.Count == 0) { // TODO: switch to read from server instead of local dir string[] files = Directory.GetFiles(@"wwwroot\assets\TestTireDir"); foreach (var file in files) { availableTireOffers.Add((file.Substring(8), file.Substring(8))); selectedTireOffers.Add(false); } files = Directory.GetFiles(@"wwwroot\assets\TestServiceDir"); foreach (var file in files) { availableServiceOffers.Add((file.Substring(8), file.Substring(8))); selectedServiceOffers.Add(false); } await Task.Delay(5); } } public async Task GetSelectionJson(int locID) { // Add tire offer information to JSON SelectionObject sel = new SelectionObject(); for (int index = 0; index < selectedTireOffers.Count; index++) { if (selectedTireOffers[index]) { sel.tireOffers.Add(availableTireOffers[index].Item2, availableTireOffers[index].Item2.Split("_")[0]); } } for (int index = 0; index < selectedServiceOffers.Count; index++) { if (selectedServiceOffers[index]) { sel.serviceOffers.Add(availableServiceOffers[index].Item2, availableServiceOffers[index].Item2.Split("_")[0]); } } sel.lastUpdated = DateTime.Now; await Task.Delay(5); return JsonConvert.SerializeObject(sel, Newtonsoft.Json.Formatting.Indented); } private async Task OnFinishHandler() { // Update database for selected location(s) Dictionary locSelections = new Dictionary(); foreach (DatabaseDriver.Location item in locInfo) { if (locationState[item.LocationID]) { var json = await GetSelectionJson(item.LocationID); locSelections.Add(item.LocationID, json); } } await DatabaseDriver.UpdateSelection(locSelections); await SendEmailNotice(locSelections); navigationManager.NavigateTo("/portal", true); } private async Task SendEmailNotice(Dictionary locSelections) { using (var em = new kmCommonLibsCore.Emails()) { em.Subject = "TireTargets Offers Updated"; em.AddAddress(kmCommonLibsCore.enuAddressType.From, "support@keymotive.us", "TireTargets"); em.AddAddress(kmCommonLibsCore.enuAddressType.To, "joshdeck@keymotive.net", "TireTargets Support"); string locationInfoJson = ""; foreach (var current in locSelections) { locationInfoJson += "Location ID: " + current.Key + "
" + current.Value + "



"; } em.HtmlBody = "A customer has modified their selections:

" + locationInfoJson; try { em.Send(); } catch (Exception e) { await Console.Out.WriteLineAsync("ERROR: " + e.Message); } } } private class SelectionObject { public SelectionObject() { tireOffers = new Dictionary(); serviceOffers = new Dictionary(); } public Dictionary tireOffers; public Dictionary serviceOffers; public DateTime lastUpdated; } } }