<?php
session_start();
include "config.php";

/* ===============================
   AUTH CHECK
================================ */
if(!isset($_SESSION['patient_mobile'])){
   header("Location: patient_login.php");
   exit;
}

$mobile = $_SESSION['patient_mobile'];

/* ===============================
   FETCH PATIENT
================================ */
$p = mysqli_query($conn,"SELECT * FROM patients WHERE mobile='$mobile'");
$patient = mysqli_fetch_assoc($p);

$pid = $patient['id'];

/* ===============================
   FETCH APPOINTMENTS
================================ */
$app = mysqli_query($conn,"
SELECT * FROM appointments 
WHERE mobile='$mobile'
ORDER BY id DESC
");

/* ===============================
   FETCH PRESCRIPTIONS
================================ */
$pres = mysqli_query($conn,"
SELECT * FROM prescriptions 
WHERE patient_id='$pid'
ORDER BY id DESC
");
?>

<!DOCTYPE html>
<html>
<head>
<title>Patient Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/style.css">
</head>

<body>

<div class="container">

<!-- ================= HEADER ================= -->

<div class="patient-header">
   <h2>Welcome <?php echo $patient['name']; ?></h2>
   <p>Mobile: <?php echo $mobile; ?></p>
</div>
<br>
<!-- ================= BUTTONS ================= -->
<div class="patient-header">
<div class="top-actions">
   <a href="patient_book_appointment.php" class="btn btn-primary">
      Book New Appointment
   </a>

   <a href="patient_logout.php" class="btn btn-danger">
      Logout
   </a>
</div>

</div>
<br>
<br>
<!-- ================= APPOINTMENTS ================= -->

<h3 class="section-title">My Appointments</h3>

<?php if(mysqli_num_rows($app)==0){ ?>

<p>No Appointments Found</p>

<?php } else { ?>

<div class="card table-wrap">
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>Reason</th>
<th>Status</th>
</tr>

<?php
while($a=mysqli_fetch_assoc($app)){
   $status = $a['status'] ?? "Pending";
   $class = ($status=="Approved") ? "status-approved" : "status-pending";

   echo "<tr>
   <td>{$a['visit_date']}</td>
   <td>{$a['visit_time']}</td>
   <td>{$a['reason']}</td>
   <td class='$class'>$status</td>
   </tr>";
   
}
?>

</table>
</div>

<?php } ?>


<!-- ================= PRESCRIPTIONS ================= -->

<h3 class="section-title">Your Prescriptions</h3>

<?php if(mysqli_num_rows($pres)==0){ ?>

<p>No Prescriptions Found</p>

<?php } ?>

<?php while($row=mysqli_fetch_assoc($pres)){ ?>

<div class="card prescription-box">

<div class="prescription-head">
Diagnosis: <?php echo $row['diagnosis']; ?><br>
Advice: <?php echo $row['advice']; ?><br>
Date:<?php echo $row['created_at']; ?><br>
</div>

<div class="table-wrap">
<table>
<tr>
<th>Medicine</th>
<th>Dosage</th>
<th>Frequency</th>
<th>Duration</th>
</tr>

<?php
$items = mysqli_query($conn,"
SELECT * FROM prescription_items 
WHERE prescription_id='".$row['id']."'
");

while($m=mysqli_fetch_assoc($items)){
echo "<tr>
<td>{$m['medicine_name']}</td>
<td>{$m['dosage']}</td>
<td>{$m['frequency']}</td>
<td>{$m['duration']}</td>
</tr>";
}
?>

</table>
</div>

</div>

<?php } ?>

</div>

</body>
</html>