<?php
session_start();
if (!isset($_SESSION['staff_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

$totalPatients = $conn->query("SELECT COUNT(*) as cnt FROM patients")->fetch_assoc()['cnt'];
$totalAppointments = $conn->query("SELECT COUNT(*) as cnt FROM appointments")->fetch_assoc()['cnt'];
$totalRevenue = $conn->query("SELECT SUM(amount) as total FROM payments WHERE status='completed'")->fetch_assoc()['total'] ?? 0;
$pendingAppointments = $conn->query("SELECT COUNT(*) as cnt FROM appointments WHERE status='pending'")->fetch_assoc()['cnt'];
$activePatients = $conn->query("SELECT COUNT(*) as cnt FROM patients WHERE portal_active=1")->fetch_assoc()['cnt'];
?>
<!DOCTYPE html>
<html><head><title>Reports</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body><div class="container mt-4"><div class="card"><div class="card-header bg-success text-white"><h4>System Reports</h4></div><div class="card-body">
<ul class="list-group">
    <li class="list-group-item d-flex justify-content-between"><strong>Total Patients:</strong><span><?= $totalPatients ?></span></li>
    <li class="list-group-item d-flex justify-content-between"><strong>Active Patients:</strong><span><?= $activePatients ?></span></li>
    <li class="list-group-item d-flex justify-content-between"><strong>Total Appointments:</strong><span><?= $totalAppointments ?></span></li>
    <li class="list-group-item d-flex justify-content-between"><strong>Pending Appointments:</strong><span><?= $pendingAppointments ?></span></li>
    <li class="list-group-item d-flex justify-content-between"><strong>Total Revenue:</strong><span>₦<?= number_format($totalRevenue, 2) ?></span></li>
</ul>
<a href="dashboard.php" class="btn btn-secondary mt-3">Back</a>
</div></div></div></body>
</html>