|
|
|
using Justice.Helpers;
|
|
|
|
using Justice.Models;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using Justice.Services;
|
|
|
|
|
|
|
|
namespace Justice.Views
|
|
|
|
{
|
|
|
|
public partial class AuthorityReportsPage : ContentPage
|
|
|
|
{
|
|
|
|
private readonly DatabaseHelper _dbHelper;
|
|
|
|
public ObservableCollection<IncidentReport> Reports { get; set; }
|
|
|
|
|
|
|
|
public AuthorityReportsPage()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
|
|
|
_dbHelper = new DatabaseHelper();
|
|
|
|
Reports = new ObservableCollection<IncidentReport>();
|
|
|
|
ReportsListView.ItemsSource = Reports;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async void OnAppearing()
|
|
|
|
{
|
|
|
|
base.OnAppearing();
|
|
|
|
await LoadReportsAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task LoadReportsAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Reports.Clear();
|
|
|
|
// Fetch reports from the database and sort them by DateTime in descending order
|
|
|
|
var reportsFromDb = await _dbHelper.GetAllAsync<IncidentReport>();
|
|
|
|
var sortedReports = reportsFromDb.OrderByDescending(r => r.DateTime).ToList();
|
|
|
|
|
|
|
|
foreach (var report in sortedReports)
|
|
|
|
{
|
|
|
|
Reports.Add(report);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", $"Failed to load reports: {ex.Message}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnUpdateStatusClicked(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
var button = sender as Button;
|
|
|
|
if (button?.CommandParameter is IncidentReport selectedReport)
|
|
|
|
{
|
|
|
|
selectedReport.Status = "Resolved";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _dbHelper.UpdateAsync(selectedReport);
|
|
|
|
await DisplayAlert("Success", "Status updated to Resolved.", "OK");
|
|
|
|
|
|
|
|
// Push notification to end user (dummy logic)
|
|
|
|
await DisplayAlert("Notification", $"Notification sent to {selectedReport.ReporterName}.", "OK");
|
|
|
|
await LoadReportsAsync();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", $"Failed to update status: {ex.Message}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void OnViewAttachmentClicked(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
var button = sender as Button;
|
|
|
|
if (button?.CommandParameter is IncidentReport selectedReport)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(selectedReport.AttachmentPath))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await Launcher.OpenAsync(new OpenFileRequest
|
|
|
|
{
|
|
|
|
File = new ReadOnlyFile(selectedReport.AttachmentPath)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", $"Failed to open attachment: {ex.Message}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", "No attachment found for this report.", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|