<?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['id'] ?? 0;
$patient = $conn->query("SELECT * FROM patients WHERE patient_id = $patient_id")->fetch_assoc();
$vitals = $conn->query("SELECT * FROM vitals WHERE patient_id = $patient_id ORDER BY recorded_date DESC LIMIT 10");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Patient Details - 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-primary">
    <div class="container">
        <span class="navbar-brand"><i class="fas fa-user-injured"></i> Patient Details</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 bg-primary text-white">
            <h5><?= htmlspecialchars($patient['first_name'] . ' ' . $patient['last_name']) ?></h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <p><strong>Patient ID:</strong> <?= $patient['patient_id'] ?></p>
                    <p><strong>Phone:</strong> <?= $patient['phone'] ?? 'N/A' ?></p>
                    <p><strong>Email:</strong> <?= $patient['email'] ?? 'N/A' ?></p>
                </div>
                <div class="col-md-6">
                    <p><strong>Date of Birth:</strong> <?= $patient['dob'] ?? 'N/A' ?></p>
                    <p><strong>Gender:</strong> <?= $patient['gender'] ?? 'N/A' ?></p>
                    <p><strong>Address:</strong> <?= $patient['address'] ?? 'N/A' ?></p>
                </div>
            </div>
            
            <h6 class="mt-4">Recent Vitals</h6>
            <?php if($vitals && $vitals->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr><th>Date</th><th>BP</th><th>Heart Rate</th><th>Temp</th><th>O2 Sat</th><th>Weight</th></tr>
                        </thead>
                        <tbody>
                        <?php while($v = $vitals->fetch_assoc()): ?>
                            <tr>
                                <td><?= date('M d, Y', 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>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
                <a href="view_reports.php?patient_id=<?= $patient_id ?>" class="btn btn-info btn-sm">View Full History</a>
            <?php else: ?>
                <p class="text-muted">No vitals recorded yet.</p>
            <?php endif; ?>
            
            <button class="btn btn-success mt-3" onclick="showVitalForm(<?= $patient_id ?>, '<?= htmlspecialchars($patient['first_name'] . ' ' . $patient['last_name']) ?>')">
                <i class="fas fa-heartbeat"></i> Record New Vitals
            </button>
        </div>
    </div>
</div>

<!-- Vital Signs Modal (same as dashboard) -->
<div class="modal fade" id="vitalModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title"><i class="fas fa-heartbeat"></i> Record Vital Signs</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="save_vitals.php">
                <div class="modal-body">
                    <input type="hidden" name="patient_id" id="vital_patient_id">
                    <div class="alert alert-info" id="patient_name_display"></div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label>Blood Pressure</label>
                            <input type="text" name="blood_pressure" class="form-control" placeholder="e.g., 120/80">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Heart Rate (bpm)</label>
                            <input type="number" name="heart_rate" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Temperature (°C)</label>
                            <input type="number" step="0.1" name="temperature" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Oxygen Saturation (%)</label>
                            <input type="number" name="oxygen_saturation" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Weight (kg)</label>
                            <input type="number" step="0.1" name="weight" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Height (cm)</label>
                            <input type="number" step="0.1" name="height" class="form-control">
                        </div>
                        <div class="col-12 mb-3">
                            <label>Notes</label>
                            <textarea name="notes" class="form-control" rows="2"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-success">Save Vitals</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function showVitalForm(patientId, patientName) {
    document.getElementById('vital_patient_id').value = patientId;
    document.getElementById('patient_name_display').innerHTML = '<strong>Patient:</strong> ' + patientName;
    var myModal = new bootstrap.Modal(document.getElementById('vitalModal'));
    myModal.show();
}
</script>
</body>
</html>