|
|
|
using ChildGuard.Services;
|
|
|
|
using Justice.Helpers;
|
|
|
|
using Justice.Models;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Justice.Views
|
|
|
|
{
|
|
|
|
public partial class IncidentReportPage : ContentPage
|
|
|
|
{
|
|
|
|
private readonly DatabaseHelper _databaseHelper;
|
|
|
|
private readonly EmailService _emailService;
|
|
|
|
|
|
|
|
public IncidentReportPage()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
|
|
|
_databaseHelper = new DatabaseHelper();
|
|
|
|
_emailService = new EmailService();
|
|
|
|
}
|
|
|
|
|
|
|
|
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}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = null // Optional
|
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Save to database
|
|
|
|
await _databaseHelper.InsertAsync(report);
|
|
|
|
|
|
|
|
// Get recipient email based on incident type
|
|
|
|
string recipientEmail = _emailService.GetRecipientEmail(report.IncidentType);
|
|
|
|
|
|
|
|
// Prepare 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.SendEmailAsync(recipientEmail, "Incident Report", emailBody);
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void OnChooseFileClicked(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
var result = await FilePicker.PickAsync();
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
await DisplayAlert("File Selected", $"File: {result.FileName}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|