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

$patient_id = $_GET['id'] ?? 0;
if (!$patient_id) { header("Location: manage_patients.php"); exit(); }

$patient = $conn->query("SELECT * FROM patients WHERE patient_id = '$patient_id'")->fetch_assoc();
$appointments = $conn->query("SELECT * FROM appointments WHERE patient_id = '$patient_id' ORDER BY appointment_date DESC");
?>
<!DOCTYPE html>
<html><head><title>View Patient</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>Patient Details: <?= htmlspecialchars($patient['first_name'].' '.$patient['last_name']) ?></h4></div><div class="card-body">
<p><strong>Patient ID:</strong> <?= $patient['patient_id'] ?></p>
<p><strong>Email:</strong> <?= $patient['email'] ?></p>
<p><strong>Phone:</strong> <?= $patient['phone'] ?></p>
<p><strong>PIN:</strong> <?= substr($patient['patient_id'],0,4) ?></p>
<p><strong>Status:</strong> <?= $patient['portal_active'] ? 'Active' : 'Inactive' ?></p>
<h5>Appointments</h5>
<table class="table"><?php while($a=$appointments->fetch_assoc()): ?><tr><td><?= date('M d, Y',strtotime($a['appointment_date'])) ?></td><td><?= date('h:i A',strtotime($a['appointment_time'])) ?></td><td><?= $a['status'] ?></td></tr><?php endwhile; ?></table>
<a href="manage_patients.php" class="btn btn-secondary">Back</a>
</div></div></div></body>
</html>