<?php
session_start();
if (!isset($_SESSION['doctor_id'])) {
    header("Location: login.php");
    exit();
}
include '../include/config.php';
$patient_id = $_GET['id'] ?? 0;
if (!$patient_id) { header("Location: my_patients.php"); exit(); }

$patient = $conn->query("SELECT * FROM patients WHERE patient_id='$patient_id'")->fetch_assoc();
$activities = $conn->query("SELECT * FROM daily_activity WHERE patient_id='$patient_id' ORDER BY activity_date DESC LIMIT 10");
$prescriptions = $conn->query("SELECT * FROM prescriptions WHERE patient_id='$patient_id' ORDER BY created_at DESC");
$vitals = $conn->query("SELECT * FROM vitals WHERE patient_id='$patient_id' ORDER BY recorded_at DESC");
$appointments = $conn->query("SELECT * FROM appointments WHERE patient_id='$patient_id' ORDER BY appointment_date DESC");
?>
<!DOCTYPE html>
<html><head><title>Patient Record</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body><nav class="navbar navbar-dark bg-dark"><div class="container"><span class="navbar-brand">Patient Record</span><a href="my_patients.php" class="btn btn-light btn-sm">Back</a></div></nav>
<div class="container mt-4">
<div class="card mb-3"><div class="card-header bg-success text-white">Profile</div><div class="card-body"><strong><?= $patient['first_name'].' '.$patient['last_name'] ?></strong><br>Email: <?= $patient['email'] ?><br>Phone: <?= $patient['phone'] ?><br>Patient ID: <?= $patient['patient_id'] ?></div></div>
<div class="card mb-3"><div class="card-header bg-info text-white">Vitals</div><div class="card-body"><?php if($vitals->num_rows>0) while($v=$vitals->fetch_assoc()) echo "<div><strong>{$v['recorded_at']}</strong> - BP: {$v['systolic']}/{$v['diastolic']}, Weight: {$v['weight']}kg, HR: {$v['heart_rate']}</div>"; else echo "No vitals."; ?></div></div>
<div class="card mb-3"><div class="card-header bg-warning text-dark">Prescriptions</div><div class="card-body"><?php if($prescriptions->num_rows>0) while($p=$prescriptions->fetch_assoc()) echo "<div><strong>{$p['created_at']}</strong> - {$p['medication']} ({$p['dosage']})</div>"; else echo "No prescriptions."; ?></div></div>
<div class="card mb-3"><div class="card-header bg-secondary text-white">Activities</div><div class="card-body"><?php if($activities->num_rows>0) while($a=$activities->fetch_assoc()) echo "<div><strong>{$a['activity_date']}</strong> - {$a['activity_type']}: {$a['description']}</div>"; else echo "No activities."; ?></div></div>
<div class="card"><div class="card-header bg-primary text-white">Appointments</div><div class="card-body"><?php if($appointments->num_rows>0) while($a=$appointments->fetch_assoc()) echo "<div><strong>{$a['appointment_date']}</strong> - {$a['reason']} ({$a['status']})</div>"; else echo "No appointments."; ?></div></div>
</div></body>
</html>