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

$patients = $conn->query("SELECT DISTINCT p.patient_id, p.first_name, p.last_name 
                          FROM patients p 
                          JOIN appointments a ON p.patient_id = a.patient_id 
                          WHERE a.doctor_id = $doctor_id");

$msg = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $patient_id = $_POST['patient_id'];
    $systolic = $_POST['systolic'];
    $diastolic = $_POST['diastolic'];
    $weight = $_POST['weight'];
    $heart_rate = $_POST['heart_rate'];
    $stmt = $conn->prepare("INSERT INTO vitals (patient_id, systolic, diastolic, weight, heart_rate) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("iiidi", $patient_id, $systolic, $diastolic, $weight, $heart_rate);
    if ($stmt->execute()) $msg = "Vitals recorded!";
    else $msg = "Error: " . $conn->error;
}
?>
<!DOCTYPE html>
<html><head><title>Add Vitals</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body><nav class="navbar navbar-dark bg-dark"><div class="container"><span class="navbar-brand"><i class="fas fa-heartbeat"></i> Add Vitals</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">Record Vitals</div><div class="card-body"><?php if($msg) echo "<div class='alert alert-success'>$msg</div>"; ?>
<form method="POST"><div class="mb-3"><label>Patient</label><select name="patient_id" class="form-control" required><?php while($p=$patients->fetch_assoc()) echo "<option value='{$p['patient_id']}'>{$p['first_name']} {$p['last_name']}</option>"; ?></select></div>
<div class="row"><div class="col-md-6 mb-3"><label>Systolic</label><input type="number" name="systolic" class="form-control" required></div><div class="col-md-6 mb-3"><label>Diastolic</label><input type="number" name="diastolic" class="form-control" required></div></div>
<div class="row"><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>Heart Rate</label><input type="number" name="heart_rate" class="form-control"></div></div>
<button type="submit" class="btn btn-primary w-100">Save Vitals</button></form>
</div></div></div></body>
</html>