<?php
include "config.php";

$pid = $_GET['pid'];
$pres_id = $_GET['prescription_id'];

$patient = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT * FROM patients WHERE id='$pid'
"));

$pres = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT * FROM prescriptions WHERE id='$pres_id'
"));

$doc = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT * FROM doctor_profile LIMIT 1
"));
?>

<!DOCTYPE html>
<html>
<head>
<title>Medical Certificate</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body{
    font-family: Arial;
    background:#fff;
}
.certificate{
    max-width:750px;
    margin:auto;
    padding:30px;
    border:1px solid #000;
}
h3{
    text-align:center;
    text-decoration:underline;
}
.signature{
    margin-top:50px;
}
</style>
</head>

<body>

<div class="certificate">

<!-- 🔹 SAME DOCTOR HEADER AS PRESCRIPTION -->
<div style="text-align:center;">
    <h2><?= $doc['clinic_name'] ?></h2>
    <b>Dr. <?= $doc['doctor_name'] ?></b><br>
    <?= $doc['qualification'] ?><br>
    Reg No: <?= $doc['reg_no'] ?><br>
    Mobile: <?= $doc['mobile'] ?>
</div>

<hr>

<h3>MEDICAL CERTIFICATE</h3>

<p>
This is to certify that <b><?= $patient['name'] ?></b>,
Age <b><?= $patient['age'] ?></b> years,
was under my treatment on
<b><?= date("d-m-Y",strtotime($pres['created_at'])) ?></b>.
</p>

<p>
He / She was suffering from:
<b><?= $pres['diagnosis'] ?></b>.
</p>

<p>
He / She is advised rest from
<b><?= date("d-m-Y") ?></b>
to
<b><?= date("d-m-Y", strtotime("+3 days")) ?></b>.
</p>

<p>
In my opinion, the patient is fit to resume duties thereafter.
</p>

<div class="signature">
<img src="uploads/<?= $doc['signature'] ?>" width="120"><br>
<b>Doctor Signature</b><br>
Date: <?= date("d-m-Y") ?>
</div>

</div>

<br>

<button onclick="window.print()">🖨 Print</button>

<a href="medical_certificate_pdf.php?pid=<?=$pid?>&prescription_id=<?=$pres_id?>">
📄 Download PDF
</a>

</body>
</html>