You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
8.1 KiB
214 lines
8.1 KiB
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
|
|
{
|
|
public partial class IncidentReportPage : ContentPage
|
|
{
|
|
private readonly DatabaseHelper _databaseHelper;
|
|
private readonly EmailService _emailService;
|
|
private string _selectedAttachmentPath; // Store the file path of the selected attachment
|
|
|
|
public IncidentReportPage()
|
|
{
|
|
InitializeComponent();
|
|
_databaseHelper = new DatabaseHelper();
|
|
_emailService = new EmailService();
|
|
}
|
|
|
|
public async Task SetLoadingStateAsync(bool isLoading)
|
|
{
|
|
LoadingIndicator.IsRunning = isLoading;
|
|
LoadingIndicator.IsVisible = isLoading;
|
|
}
|
|
|
|
protected override async void OnAppearing()
|
|
{
|
|
base.OnAppearing();
|
|
await FetchLocationAsync();
|
|
}
|
|
|
|
private async Task FetchLocationAsync()
|
|
{
|
|
try
|
|
{
|
|
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) ||
|
|
IncidentTypePicker.SelectedItem == null ||
|
|
string.IsNullOrWhiteSpace(DescriptionEditor.Text))
|
|
{
|
|
await DisplayAlert("Error", "Please fill in all required fields.", "OK");
|
|
return;
|
|
}
|
|
await SetLoadingStateAsync(true); //show loader
|
|
try
|
|
{
|
|
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 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)
|
|
{
|
|
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(authority.Email, "Incident Report", emailBody, _selectedAttachmentPath);
|
|
|
|
if (emailSent)
|
|
{
|
|
Console.WriteLine($"Email sent to: {authority.Name}");
|
|
}
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await DisplayAlert("Error", $"Failed to submit report: {ex.Message}", "OK");
|
|
}
|
|
finally
|
|
{
|
|
await SetLoadingStateAsync(false);
|
|
}
|
|
*/
|
|
}
|
|
|
|
}
|
|
}
|
|
|