|
|
|
@ -1,11 +1,10 @@ |
|
|
|
|
using Justice.Services; |
|
|
|
|
using Justice.Helpers; |
|
|
|
|
using Justice.Models; |
|
|
|
|
using Microsoft.Maui.Storage; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
using MailKit; |
|
|
|
|
|
|
|
|
|
namespace Justice.Views |
|
|
|
|
{ |
|
|
|
@ -15,6 +14,16 @@ namespace Justice.Views |
|
|
|
|
private readonly EmailService _emailService; |
|
|
|
|
private string _selectedAttachmentPath; // Store the file path of the selected attachment |
|
|
|
|
|
|
|
|
|
// Mapping Incident Types to Authority Emails |
|
|
|
|
private readonly Dictionary<string, string> _incidentTypeToEmail = new() |
|
|
|
|
{ |
|
|
|
|
{ "Accident", "amreitsyanf@gmail.com" }, |
|
|
|
|
{ "Fire", "policed282@gmail.com" }, |
|
|
|
|
{ "Crime", "policed282@gmail.com" }, |
|
|
|
|
{ "Medical", "amreitsyanf@gmail.com" }, |
|
|
|
|
{ "Other", "general_authority@example.com" } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
public IncidentReportPage() |
|
|
|
|
{ |
|
|
|
|
InitializeComponent(); |
|
|
|
@ -40,8 +49,6 @@ namespace Justice.Views |
|
|
|
|
{ |
|
|
|
|
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); |
|
|
|
|
LocationEntry.Text = address; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
@ -59,11 +66,11 @@ namespace Justice.Views |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>> |
|
|
|
|
{ |
|
|
|
|
{ DevicePlatform.Android, new[] { "*/*" } }, // Allow all files on Android |
|
|
|
|
{ DevicePlatform.iOS, new[] { "public.data" } }, // iOS equivalent for all files |
|
|
|
|
{ DevicePlatform.WinUI, new[] { "*" } } // Allow all files on Windows |
|
|
|
|
}); |
|
|
|
|
{ |
|
|
|
|
{ DevicePlatform.Android, new[] { "*/*" } }, // Allow all files on Android |
|
|
|
|
{ DevicePlatform.iOS, new[] { "public.data" } }, // iOS equivalent for all files |
|
|
|
|
{ DevicePlatform.WinUI, new[] { "*" } } // Allow all files on Windows |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
var pickOptions = new PickOptions |
|
|
|
|
{ |
|
|
|
@ -91,7 +98,6 @@ namespace Justice.Views |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnSubmitReportClicked(object sender, EventArgs e) |
|
|
|
|
{ |
|
|
|
|
if (string.IsNullOrWhiteSpace(ReporterNameEntry.Text) || |
|
|
|
@ -101,7 +107,7 @@ namespace Justice.Views |
|
|
|
|
await DisplayAlert("Error", "Please fill in all required fields.", "OK"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
await SetLoadingStateAsync(true); //show loader |
|
|
|
|
await SetLoadingStateAsync(true); // Show loader |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); |
|
|
|
@ -116,86 +122,39 @@ namespace Justice.Views |
|
|
|
|
DateTime = DateTime.Now, |
|
|
|
|
AttachmentPath = _selectedAttachmentPath |
|
|
|
|
}; |
|
|
|
|
//save report to database |
|
|
|
|
|
|
|
|
|
// Save report to database |
|
|
|
|
await _databaseHelper.InsertAsync(report); |
|
|
|
|
// get nearby authorities based on incident location |
|
|
|
|
var authorityService = new AuthorityService(); |
|
|
|
|
var nearbyAuthorities = authorityService.GetNearByAuthorities(report.Latitude, report.Longitude); |
|
|
|
|
// Prepare the email content |
|
|
|
|
string emailBody = $"Incident Report:\n\n" + |
|
|
|
|
$"Name: {report.ReporterName}\n" + |
|
|
|
|
$"Type: {report.IncidentType}\n" + |
|
|
|
|
$"Description: {report.Description}\n" + |
|
|
|
|
$"Latitude: {report.Latitude}\n" + |
|
|
|
|
$"Longitude: {report.Longitude}\n" + |
|
|
|
|
$"Location: {report.Address}\n" + |
|
|
|
|
$"Date/Time: {report.DateTime}"; |
|
|
|
|
|
|
|
|
|
// Send email to each nearby authority |
|
|
|
|
foreach (var authority in nearbyAuthorities) |
|
|
|
|
// Get the email address for the selected incident type |
|
|
|
|
string incidentType = report.IncidentType; |
|
|
|
|
if (_incidentTypeToEmail.TryGetValue(incidentType, out var recipientEmail)) |
|
|
|
|
{ |
|
|
|
|
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(authority.Email, "Incident Report", emailBody, _selectedAttachmentPath); |
|
|
|
|
// Prepare the email content |
|
|
|
|
string emailBody = $"Incident Report:\n\n" + |
|
|
|
|
$"Name: {report.ReporterName}\n" + |
|
|
|
|
$"Type: {report.IncidentType}\n" + |
|
|
|
|
$"Description: {report.Description}\n" + |
|
|
|
|
$"Latitude: {report.Latitude}\n" + |
|
|
|
|
$"Longitude: {report.Longitude}\n" + |
|
|
|
|
$"Location: {report.Address}\n" + |
|
|
|
|
$"Date/Time: {report.DateTime}"; |
|
|
|
|
|
|
|
|
|
// Send email |
|
|
|
|
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(recipientEmail, "Incident Report", emailBody, _selectedAttachmentPath); |
|
|
|
|
if (emailSent) |
|
|
|
|
{ |
|
|
|
|
Console.WriteLine($"Email sent to: {authority.Name}"); |
|
|
|
|
Console.WriteLine($"Email sent to: {recipientEmail}"); |
|
|
|
|
await DisplayAlert("Success", "Incident report submitted and email sent.", "OK"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("Error", "Failed to send email.", "OK"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await DisplayAlert("Success", "Incident report submitted and email alert sent to nearby authorities.", "OK"); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("Error", $"Failed to submit report:{ex.Message}", "OK"); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
await SetLoadingStateAsync(false); //hide loader |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); |
|
|
|
|
|
|
|
|
|
/* var report = new IncidentReport |
|
|
|
|
{ |
|
|
|
|
ReporterName = ReporterNameEntry.Text.Trim(), |
|
|
|
|
IncidentType = IncidentTypePicker.SelectedItem.ToString(), |
|
|
|
|
Description = DescriptionEditor.Text.Trim(), |
|
|
|
|
Latitude = latitude, |
|
|
|
|
Longitude = longitude, |
|
|
|
|
Address = address, |
|
|
|
|
DateTime = DateTime.Now, |
|
|
|
|
AttachmentPath = _selectedAttachmentPath // Save the attachment path |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// Save the report to the database |
|
|
|
|
|
|
|
|
|
await _databaseHelper.InsertAsync(report); |
|
|
|
|
|
|
|
|
|
// Get the recipient email based on the incident type |
|
|
|
|
string recipientEmail = _emailService.GetRecipientEmail(report.IncidentType); |
|
|
|
|
|
|
|
|
|
// Prepare the email content |
|
|
|
|
string emailBody = $"Incident Report:\n\n" + |
|
|
|
|
$"Name: {report.ReporterName}\n" + |
|
|
|
|
$"Type: {report.IncidentType}\n" + |
|
|
|
|
$"Description: {report.Description}\n" + |
|
|
|
|
$"Latitude: {report.Latitude}\n" + |
|
|
|
|
$"Longitude: {report.Longitude}\n" + |
|
|
|
|
$"Location: {report.Address}\n" + |
|
|
|
|
$"Date/Time: {report.DateTime}"; |
|
|
|
|
|
|
|
|
|
// Send email |
|
|
|
|
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(recipientEmail, "Incident Report", emailBody, _selectedAttachmentPath); |
|
|
|
|
|
|
|
|
|
if (emailSent) |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("Success", "Incident report submitted and email alert sent.", "OK"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("Error", "Failed to send email alert.", "OK"); |
|
|
|
|
await DisplayAlert("Error", "No authority found for the selected incident type.", "OK"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
@ -204,10 +163,8 @@ namespace Justice.Views |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
await SetLoadingStateAsync(false); |
|
|
|
|
await SetLoadingStateAsync(false); // Hide loader |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|