<?php
include "config.php";

if (!isset($_GET['id'])) {
    die("Invalid Prescription ID");
}

$id = intval($_GET['id']);

/* ============================
   FETCH PRESCRIPTION + PATIENT
============================ */
$p = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT prescriptions.*, patients.name, patients.age, patients.gender, patients.mobile
FROM prescriptions
JOIN patients ON prescriptions.patient_id = patients.id
WHERE prescriptions.id = '$id'
"));

if (!$p) {
    die("Prescription not found");
}

/* ============================
   FETCH DOCTOR PROFILE
============================ */
$doc = mysqli_fetch_assoc(
    mysqli_query($conn, "SELECT * FROM doctor_profile LIMIT 1")
);

/* ============================
   PDF PATH & URL
============================ */
$pdf_dir = "uploads/prescriptions/";
$pdf_file = "prescription_$id.pdf";
$pdf_path = $pdf_dir . $pdf_file;
$pdf_url  = "https://kolhapurairport.com/clinic/" . $pdf_path;

/* ============================
   BUILD WHATSAPP MESSAGE
============================ */
$wa_msg  = "ðŸ©º *".$doc['clinic_name']."*\n";
$wa_msg .= "Dr. ".$doc['doctor_name']."\n";
$wa_msg .= $doc['qualification']."\n\n";

$wa_msg .= "*Patient:* ".$p['name']."\n";
$wa_msg .= "*Age:* ".$p['age']." | *Gender:* ".$p['gender']."\n";
$wa_msg .= "*Date:* ".date("d-m-Y", strtotime($p['created_at']))."\n\n";

$wa_msg .= "ðŸ“„ *Prescription PDF*\n";
$wa_msg .= $pdf_url;

$wa_msg = urlencode($wa_msg);
?>

<!DOCTYPE html>
<html>
<head>
<title>Prescription</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body { font-family: Arial; background:#f4f6fb; }
.container { max-width:900px; margin:auto; background:#fff; padding:20px; }
table { width:100%; border-collapse:collapse; }
th, td { border:1px solid #ccc; padding:8px; }
th { background:#f0f0f0; }
.btn { padding:8px 12px; color:#fff; text-decoration:none; border-radius:5px; margin-right:5px; display:inline-block; }
.btn-wa { background:#25D366; }
.btn-pdf { background:#007bff; }
.btn-med { background:#6f42c1; }
.btn-back { background:#6c757d; }
</style>
</head>

<body>

<div class="container">

<h2><?= htmlspecialchars($doc['clinic_name']) ?></h2>
<b>Dr. <?= htmlspecialchars($doc['doctor_name']) ?></b><br>
<?= htmlspecialchars($doc['qualification']) ?><br>
Reg No: <?= htmlspecialchars($doc['reg_no']) ?><br>
Mobile: <?= htmlspecialchars($doc['mobile']) ?>

<hr>

<b>Patient:</b> <?= htmlspecialchars($p['name']) ?><br>
<b>Age:</b> <?= $p['age'] ?> |
<b>Gender:</b> <?= htmlspecialchars($p['gender']) ?><br>
<b>Date:</b> <?= date("d-m-Y H:i", strtotime($p['created_at'])) ?>

<hr>

<b>Diagnosis:</b><br>
<?= nl2br(htmlspecialchars($p['diagnosis'])) ?>

<hr>

<table>
<tr>
<th>Medicine</th>
<th>Dosage</th>
<th>Frequency</th>
<th>Duration</th>
</tr>

<?php
$r = mysqli_query($conn, "SELECT * FROM prescription_items WHERE prescription_id='$id'");
while ($row = mysqli_fetch_assoc($r)) {
    echo "<tr>
        <td>".htmlspecialchars($row['medicine_name'])."</td>
        <td>".htmlspecialchars($row['dosage'])."</td>
        <td>".htmlspecialchars($row['frequency'])."</td>
        <td>".htmlspecialchars($row['duration'])."</td>
    </tr>";
}
?>
</table>

<hr>

<b>Advice:</b><br>
<?= nl2br(htmlspecialchars($p['advice'])) ?>

<br><br>

<?php if (!empty($doc['signature'])) { ?>
<img src="uploads/<?= htmlspecialchars($doc['signature']) ?>" width="120"><br>
Doctor Signature
<?php } ?>

<br><br>

<button onclick="window.print()">ðŸ–¨ Print</button>

<br><br>

<a class="btn btn-wa" target="_blank"
href="https://wa.me/91<?= $p['mobile'] ?>?text=<?= $wa_msg ?>">
ðŸ“² Send PDF on WhatsApp
</a>
<a href="send_whatsapp_pdf.php?id=<?=$id?>" 
style="padding:8px 12px;background:#25D366;color:white;
text-decoration:none;border-radius:5px;">
📲 Send WhatsApp (PDF)
</a>

<a class="btn btn-pdf"
href="prescription_pdf.php?id=<?= $id ?>">
ðŸ“„ Download PDF
</a>

<a class="btn btn-med"
href="medical_certificate.php?pid=<?= $p['patient_id'] ?>&prescription_id=<?= $id ?>">
ðŸ©º Medical Certificate
</a>

<a class="btn btn-back"
href="prescription.php?pid=<?= $p['patient_id'] ?>">
â¬… Back
</a>

</div>

</body>
</html>