|
|
|
@ -1,7 +1,11 @@ |
|
|
|
|
using ChildGuard.Services; |
|
|
|
|
using Justice.Services; |
|
|
|
|
using Justice.Helpers; |
|
|
|
|
using Justice.Models; |
|
|
|
|
using Microsoft.Maui.Storage; |
|
|
|
|
using System; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
using MailKit; |
|
|
|
|
|
|
|
|
|
namespace Justice.Views |
|
|
|
|
{ |
|
|
|
@ -9,6 +13,7 @@ namespace Justice.Views |
|
|
|
|
{ |
|
|
|
|
private readonly DatabaseHelper _databaseHelper; |
|
|
|
|
private readonly EmailService _emailService; |
|
|
|
|
private string _selectedAttachmentPath; // Store the file path of the selected attachment |
|
|
|
|
|
|
|
|
|
public IncidentReportPage() |
|
|
|
|
{ |
|
|
|
@ -17,6 +22,12 @@ namespace Justice.Views |
|
|
|
|
_emailService = new EmailService(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async Task SetLoadingStateAsync(bool isLoading) |
|
|
|
|
{ |
|
|
|
|
LoadingIndicator.IsRunning = isLoading; |
|
|
|
|
LoadingIndicator.IsVisible = isLoading; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected override async void OnAppearing() |
|
|
|
|
{ |
|
|
|
|
base.OnAppearing(); |
|
|
|
@ -29,14 +40,58 @@ namespace Justice.Views |
|
|
|
|
{ |
|
|
|
|
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync(); |
|
|
|
|
LocationEntry.Text = address; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
LocationEntry.Text = "Unable to fetch location."; |
|
|
|
|
Console.WriteLine($"Error fetching location: {ex.Message}"); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
await SetLoadingStateAsync(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async void OnChooseFileClicked(object sender, EventArgs e) |
|
|
|
|
{ |
|
|
|
|
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 |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
var pickOptions = new PickOptions |
|
|
|
|
{ |
|
|
|
|
PickerTitle = "Select a file", |
|
|
|
|
FileTypes = customFileType |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var result = await FilePicker.PickAsync(pickOptions); |
|
|
|
|
|
|
|
|
|
if (result != null) |
|
|
|
|
{ |
|
|
|
|
// Save the file path |
|
|
|
|
_selectedAttachmentPath = result.FullPath; |
|
|
|
|
AttachmentLabel.Text = $"Selected File: {Path.GetFileName(_selectedAttachmentPath)}"; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
AttachmentLabel.Text = "No file selected"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
Console.WriteLine($"File selection error: {ex.Message}"); |
|
|
|
|
await DisplayAlert("Error", "Failed to select file.", "OK"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnSubmitReportClicked(object sender, EventArgs e) |
|
|
|
|
{ |
|
|
|
|
if (string.IsNullOrWhiteSpace(ReporterNameEntry.Text) || |
|
|
|
@ -58,18 +113,19 @@ namespace Justice.Views |
|
|
|
|
Longitude = longitude, |
|
|
|
|
Address = address, |
|
|
|
|
DateTime = DateTime.Now, |
|
|
|
|
AttachmentPath = null // Optional |
|
|
|
|
AttachmentPath = _selectedAttachmentPath // Save the attachment path |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// Save to database |
|
|
|
|
// Save the report to the database |
|
|
|
|
await SetLoadingStateAsync(true); |
|
|
|
|
await _databaseHelper.InsertAsync(report); |
|
|
|
|
|
|
|
|
|
// Get recipient email based on incident type |
|
|
|
|
// Get the recipient email based on the incident type |
|
|
|
|
string recipientEmail = _emailService.GetRecipientEmail(report.IncidentType); |
|
|
|
|
|
|
|
|
|
// Prepare email content |
|
|
|
|
|
|
|
|
|
// Prepare the email content |
|
|
|
|
string emailBody = $"Incident Report:\n\n" + |
|
|
|
|
$"Name: {report.ReporterName}\n" + |
|
|
|
|
$"Type: {report.IncidentType}\n" + |
|
|
|
@ -80,8 +136,8 @@ namespace Justice.Views |
|
|
|
|
$"Date/Time: {report.DateTime}"; |
|
|
|
|
|
|
|
|
|
// Send email |
|
|
|
|
bool emailSent = await _emailService.SendEmailAsync(recipientEmail, "Incident Report", emailBody); |
|
|
|
|
|
|
|
|
|
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(recipientEmail, "Incident Report", emailBody, _selectedAttachmentPath); |
|
|
|
|
|
|
|
|
|
if (emailSent) |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("Success", "Incident report submitted and email alert sent.", "OK"); |
|
|
|
@ -95,15 +151,11 @@ namespace Justice.Views |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("Error", $"Failed to submit report: {ex.Message}", "OK"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async void OnChooseFileClicked(object sender, EventArgs e) |
|
|
|
|
{ |
|
|
|
|
var result = await FilePicker.PickAsync(); |
|
|
|
|
if (result != null) |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
await DisplayAlert("File Selected", $"File: {result.FileName}", "OK"); |
|
|
|
|
await SetLoadingStateAsync(false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|