<?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'];

$patient_id = $_GET['patient_id'] ?? null;
$vitals = null;
$patient_name = '';

if ($patient_id) {
    $patient_query = $conn->query("SELECT first_name, last_name FROM patients WHERE patient_id = $patient_id");
    if ($patient_query->num_rows > 0) {
        $patient = $patient_query->fetch_assoc();
        $patient_name = $patient['first_name'] . ' ' . $patient['last_name'];
    }
    
    $vitals = $conn->query("
        SELECT * FROM vitals 
        WHERE patient_id = $patient_id AND recorded_by_nurse_id = $nurse_id
        ORDER BY recorded_date DESC
    ");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vitals Reports - 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-info">
    <div class="container">
        <span class="navbar-brand"><i class="fas fa-chart-line"></i> Vitals Reports</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>Select Patient to View Vitals History</h5>
        </div>
        <div class="card-body">
            <form method="GET" class="mb-4">
                <div class="input-group">
                    <input type="text" name="patient_id" class="form-control" placeholder="Enter Patient ID..." value="<?= htmlspecialchars($patient_id) ?>">
                    <button type="submit" class="btn btn-primary">Load Reports</button>
                </div>
            </form>
            
            <?php if($patient_id && $vitals): ?>
                <h5>Vitals History for: <?= htmlspecialchars($patient_name) ?></h5>
                <?php if($vitals->num_rows > 0): ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr><th>Date/Time</th><th>BP</th><th>Heart Rate</th><th>Temperature</th><th>O2 Sat</th><th>Weight</th><th>BMI</th><th>Notes</th></tr>
                            </thead>
                            <tbody>
                            <?php while($v = $vitals->fetch_assoc()): ?>
                                <tr>
                                    <td><?= date('M d, Y H:i', strtotime($v['recorded_date'])) ?></td>
                                    <td><?= $v['blood_pressure'] ?? 'N/A' ?></td>
                                    <td><?= $v['heart_rate'] ?? 'N/A' ?></td>
                                    <td><?= $v['temperature'] ?? 'N/A' ?> °C</td>
                                    <td><?= $v['oxygen_saturation'] ?? 'N/A' ?> %</td>
                                    <td><?= $v['weight'] ?? 'N/A' ?> kg</td>
                                    <td><?= $v['bmi'] ?? 'N/A' ?></td>
                                    <td><?= $v['notes'] ?? '-' ?></td>
                                \,<
                            <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info">No vitals recorded for this patient yet.</div>
                <?php endif; ?>
            <?php elseif($patient_id): ?>
                <div class="alert alert-warning">Patient not found.</div>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>