208 lines
6.6 KiB
C#
208 lines
6.6 KiB
C#
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<DatabaseDriver.Location> locInfo = new List<DatabaseDriver.Location>();
|
|
private List<(int, bool)> locSelections = new List<(int, bool)> ();
|
|
private Dictionary<int, bool> locationState = new Dictionary<int, bool> ();
|
|
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<bool> selectedTireOffers = new List<bool>();
|
|
|
|
// Service offer variables
|
|
private List<(string, string)> availableServiceOffers = new List<(string, string)>();
|
|
private List<bool> selectedServiceOffers = new List<bool>();
|
|
|
|
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<string> 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<int, string> locSelections = new Dictionary<int, string>();
|
|
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<int, string> 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 + "<br /><pre><code>" + current.Value + "</pre></code><br /><br /><br />";
|
|
}
|
|
em.HtmlBody = "<b>A customer has modified their selections:</b><br /><br />" + locationInfoJson;
|
|
|
|
try
|
|
{
|
|
em.Send();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
await Console.Out.WriteLineAsync("ERROR: " + e.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private class SelectionObject
|
|
{
|
|
public SelectionObject()
|
|
{
|
|
tireOffers = new Dictionary<string, string>();
|
|
serviceOffers = new Dictionary<string, string>();
|
|
}
|
|
public Dictionary<string, string> tireOffers;
|
|
public Dictionary<string, string> serviceOffers;
|
|
public DateTime lastUpdated;
|
|
}
|
|
}
|
|
}
|