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

// Get unique patients from appointments
$patients = $conn->query("SELECT DISTINCT p.patient_id, p.first_name, p.last_name, p.email, p.phone 
                          FROM patients p 
                          JOIN appointments a ON p.patient_id = a.patient_id 
                          WHERE a.doctor_id = $doctor_id 
                          ORDER BY p.first_name");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Patients - UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>body{background:#0a3d2f;}.navbar{background:#064e3b;}.card{border-radius:20px;background:rgba(255,255,255,0.95);}</style>
</head>
<body>
<nav class="navbar navbar-dark"><div class="container"><span class="navbar-brand"><i class="fas fa-users"></i> My Patients</span><a href="dashboard.php" class="btn btn-light btn-sm">Back</a></div></nav>
<div class="container mt-4"><div class="card"><div class="card-header bg-success text-white">Patient List</div><div class="card-body">
<?php if($patients && $patients->num_rows > 0): ?>
<div class="table-responsive"><table class="table table-hover"><thead><tr><th>Name</th><th>Email</th><th>Phone</th><th>Action</th></tr></thead><tbody>
<?php while($p = $patients->fetch_assoc()): ?>
<tr><td><?= htmlspecialchars($p['first_name'].' '.$p['last_name']) ?></td><td><?= $p['email'] ?></td><td><?= $p['phone'] ?></td><td><a href="view_patient_record.php?id=<?= $p['patient_id'] ?>" class="btn btn-sm btn-primary">View Record</a></td></tr>
<?php endwhile; ?></tbody></table></div>
<?php else: ?><p class="text-muted">No patients assigned yet.</p><?php endif; ?>
</div></div></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>