<?php
include "config.php";
include "header.php";

$pid = $_GET['id'];

// ===============================
// Fetch Patient Details
// ===============================
$patient = mysqli_query($conn,
"SELECT * FROM patients WHERE id='$pid'");
$p = mysqli_fetch_assoc($patient);
?>

<!-- ================= PATIENT DETAILS ================= -->

<div class="card mb-3">
<div class="card-body">

<h4>Patient Details</h4>

<table class="table table-bordered">
<tr>
<td><b>Name</b></td>
<td><?php echo $p['name']; ?></td>

<td><b>Age</b></td>
<td><?php echo $p['age']; ?></td>
</tr>

<tr>
<td><b>Gender</b></td>
<td><?php echo $p['gender']; ?></td>

<td><b>Mobile</b></td>
<td><?php echo $p['mobile']; ?></td>
</tr>

<tr>
<td><b>Address</b></td>
<td colspan="3"><?php echo $p['address']; ?></td>
</tr>
</table>

</div>
</div>

<!-- ================= YOUR ORIGINAL HISTORY CODE ================= -->

<h4>Patient Prescription History</h4>

<table class="table table-bordered">
<tr>
<th>Date</th>
<th>Diagnosis</th>
<th>View</th>
</tr>

<?php
$q = mysqli_query($conn,"
SELECT id,diagnosis,created_at
FROM prescriptions
WHERE patient_id='$pid'
ORDER BY id DESC
");

while($row=mysqli_fetch_assoc($q)){
echo "
<tr>
<td>{$row['created_at']}</td>
<td>{$row['diagnosis']}</td>
<td>
<a class='btn btn-sm btn-primary'
href='print_prescription.php?id={$row['id']}'>
Open
</a>
</td>
</tr>
";
}
?>

</table>

<?php include "footer.php"; ?>