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

$search = $_GET['search'] ?? '';
$patients = [];
if ($search) {
    $search = $conn->real_escape_string($search);
    $patients = $conn->query("
        SELECT p.*, d.name as doctor_name
        FROM patients p
        LEFT JOIN nurse_patient_assignments npa ON p.patient_id = npa.patient_id AND npa.nurse_id = $nurse_id
        LEFT JOIN doctors d ON npa.doctor_id = d.doctor_id
        WHERE p.first_name LIKE '%$search%' OR p.last_name LIKE '%$search%' OR p.patient_id LIKE '%$search%'
        LIMIT 20
    ");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Search 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">
</head>
<body>
<nav class="navbar navbar-dark bg-success">
    <div class="container">
        <span class="navbar-brand"><i class="fas fa-search"></i> Patient Search</span>
        <a href="dashboard.php" class="btn btn-light btn-sm">Back to Dashboard</a>
    </div>
</nav>
<div class="container mt-4">
    <div class="card">
        <div class="card-header">
            <h5>Search Patients</h5>
        </div>
        <div class="card-body">
            <form method="GET" class="mb-4">
                <div class="input-group">
                    <input type="text" name="search" class="form-control" placeholder="Enter patient name or ID..." value="<?= htmlspecialchars($search) ?>">
                    <button type="submit" class="btn btn-primary">Search</button>
                </div>
            </form>
            
            <?php if($search && $patients && $patients->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr><th>ID</th><th>Name</th><th>Phone</th><th>Email</th><th>Doctor</th><th>Action</th></tr>
                        </thead>
                        <tbody>
                        <?php while($p = $patients->fetch_assoc()): ?>
                            <tr>
                                <td><?= $p['patient_id'] ?></td>
                                <td><?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?></td>
                                <td><?= $p['phone'] ?? 'N/A' ?></td>
                                <td><?= $p['email'] ?? 'N/A' ?></td>
                                <td><?= $p['doctor_name'] ?? 'Not assigned' ?></td>
                                <td><a href="view_patient.php?id=<?= $p['patient_id'] ?>" class="btn btn-sm btn-info">View</a></td>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            <?php elseif($search): ?>
                <div class="alert alert-warning">No patients found.</div>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>