<?php
session_start();
include '../include/config.php';
$nurse_id = $_SESSION['nurse_id'];
$search = $_POST['search'] ?? '';

$query = "SELECT p.*, d.name as doctor_name 
          FROM nurse_patient_assignments npa 
          JOIN patients p ON npa.patient_id = p.patient_id 
          LEFT JOIN doctors d ON npa.doctor_id = d.doctor_id 
          WHERE npa.nurse_id = $nurse_id";
if ($search) {
    $query .= " AND (p.first_name LIKE '%$search%' OR p.last_name LIKE '%$search%' OR p.patient_id LIKE '%$search%')";
}
$result = $conn->query($query);

while($p = $result->fetch_assoc()):
?>
    <div class="patient-card" onclick="viewPatient(<?= $p['patient_id'] ?>)">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <strong><?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?></strong>
                <br>
                <small>ID: <?= $p['patient_id'] ?> | Dr. <?= $p['doctor_name'] ?? 'Not assigned' ?></small>
            </div>
            <button class="btn btn-sm btn-primary" onclick="event.stopPropagation(); recordVitals(<?= $p['patient_id'] ?>, '<?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?>')">
                <i class="fas fa-heartbeat"></i> Vitals
            </button>
        </div>
    </div>
<?php endwhile; ?>