<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['nurse_id'])) {
    header("Location: login.php");
    exit();
}

include '../include/config.php';

$nurse_id = $_SESSION['nurse_id'];
$nurse_name = $_SESSION['nurse_name'];
$nurse_uhd_id = $_SESSION['nurse_uhd_id'] ?? 'NHS-' . str_pad($nurse_id, 4, '0', STR_PAD_LEFT);
$nurse_credentials = $_SESSION['nurse_credentials'] ?? 'Registered Nurse';

// Create duty roster table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS nurse_duty_roster (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nurse_id INT NOT NULL,
    duty_date DATE NOT NULL,
    shift ENUM('morning', 'afternoon', 'night', 'off') DEFAULT 'off',
    start_time TIME,
    end_time TIME,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_duty (nurse_id, duty_date)
)");

// Create notifications table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS nurse_notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nurse_id INT NOT NULL,
    sender_type VARCHAR(50),
    sender_id INT,
    title VARCHAR(255),
    message TEXT,
    is_read TINYINT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

// Get nurse profile
$nurse_info = $conn->query("SELECT * FROM nurses WHERE nurse_id = $nurse_id")->fetch_assoc();

// Get unread notifications count
$unread_notifications = 0;
$notif_result = $conn->query("SELECT COUNT(*) as cnt FROM nurse_notifications WHERE nurse_id = $nurse_id AND is_read = 0");
if ($notif_result) $unread_notifications = $notif_result->fetch_assoc()['cnt'] ?? 0;

// Get unread chat messages count
$unread_chats = 0;
$chat_check = $conn->query("SHOW TABLES LIKE 'chat_messages'");
if ($chat_check && $chat_check->num_rows > 0) {
    $chat_result = $conn->query("SELECT COUNT(*) as cnt FROM chat_messages WHERE receiver_id = $nurse_id AND receiver_type = 'nurse' AND is_read = 0");
    if ($chat_result) $unread_chats = $chat_result->fetch_assoc()['cnt'] ?? 0;
}

// Get today's duty
$today_duty = $conn->query("SELECT * FROM nurse_duty_roster WHERE nurse_id = $nurse_id AND duty_date = CURDATE()")->fetch_assoc();

// Get weekly duty schedule
$weekly_duty = $conn->query("SELECT * FROM nurse_duty_roster WHERE nurse_id = $nurse_id AND duty_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) ORDER BY duty_date");

// Get assigned patients
$assigned_patients = $conn->query("
    SELECT npa.*, p.first_name, p.last_name, p.patient_id, p.phone, p.email
    FROM nurse_patient_assignments npa
    JOIN patients p ON npa.patient_id = p.patient_id
    WHERE npa.nurse_id = $nurse_id AND npa.status = 'active'
    ORDER BY npa.assigned_date DESC
");

// Get vitals count today
$vitals_today = 0;
$vitals_res = $conn->query("SELECT COUNT(*) as cnt FROM vitals WHERE recorded_by_nurse_id = $nurse_id AND DATE(recorded_date) = CURDATE()");
if ($vitals_res) $vitals_today = $vitals_res->fetch_assoc()['cnt'] ?? 0;

// Get recent vitals - FIXED: removed the problematic JOIN
$recent_vitals = $conn->query("
    SELECT v.* 
    FROM vitals v 
    WHERE v.recorded_by_nurse_id = $nurse_id 
    ORDER BY v.vital_id DESC LIMIT 10
");

// Get all contacts for chat
$doctors = $conn->query("SELECT doctor_id as id, name as name, 'doctor' as type FROM doctors WHERE status = 'active'");
$other_nurses = $conn->query("SELECT nurse_id as id, CONCAT(first_name, ' ', last_name) as name, 'nurse' as type FROM nurses WHERE nurse_id != $nurse_id AND status = 'active'");
$staff = $conn->query("SELECT staff_id as id, CONCAT(first_name, ' ', last_name) as name, 'staff' as type FROM staff WHERE status = 'active'");

// Profile photo
$profile_pic = 'https://ui-avatars.com/api/?background=8B7355&color=fff&name=' . urlencode($nurse_name);
if (!empty($nurse_info['profile_pic']) && file_exists("../uploads/" . $nurse_info['profile_pic'])) {
    $profile_pic = "../uploads/" . $nurse_info['profile_pic'];
}

// Handle duty reporting
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['report_duty'])) {
    $shift = $_POST['shift'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];
    $conn->query("INSERT INTO nurse_duty_roster (nurse_id, duty_date, shift, start_time, end_time) 
                  VALUES ($nurse_id, CURDATE(), '$shift', '$start_time', '$end_time')
                  ON DUPLICATE KEY UPDATE shift = '$shift', start_time = '$start_time', end_time = '$end_time'");
    echo "<script>alert('Duty reported successfully!'); window.location.href='';</script>";
}

// Handle vitals save via AJAX
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['save_vitals'])) {
    $patient_id = intval($_POST['patient_id']);
    $blood_pressure = $_POST['blood_pressure'];
    $heart_rate = intval($_POST['heart_rate']);
    $temperature = floatval($_POST['temperature']);
    $oxygen_saturation = intval($_POST['oxygen_saturation']);
    $weight = floatval($_POST['weight']);
    
    $query = "INSERT INTO vitals (patient_id, recorded_by_nurse_id, blood_pressure, heart_rate, temperature, oxygen_saturation, weight, recorded_date) 
              VALUES ($patient_id, $nurse_id, '$blood_pressure', $heart_rate, $temperature, $oxygen_saturation, $weight, NOW())";
    
    if ($conn->query($query)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'error' => $conn->error]);
    }
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nurse Dashboard - Unity Healthcare</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">
    <style>
        body { 
            background: #3d2b1f; 
            font-family: 'Segoe UI', sans-serif; 
            display: flex; 
            flex-direction: column; 
            min-height: 100vh; 
        }
        .navbar { background: #2c1a0e !important; box-shadow: 0 2px 10px rgba(0,0,0,0.3); padding: 12px 0; border-bottom: 2px solid #d4a017; }
        .navbar-brand img { height: 45px; margin-right: 12px; }
        .brand-title { font-size: 1.4rem; font-weight: 800; line-height: 1.2; color: #d4a017; }
        .brand-sub { font-size: 0.7rem; font-weight: 400; letter-spacing: 1px; color: #c0c0c0; }
        .icon-btn { background: transparent; border: none; color: white; font-size: 1.2rem; position: relative; margin-left: 15px; transition: 0.3s; }
        .icon-btn:hover { color: #d4a017; transform: scale(1.1); }
        .badge-count { position: absolute; top: -8px; right: -12px; background: #dc3545; color: white; border-radius: 50%; padding: 2px 6px; font-size: 10px; font-weight: bold; }
        .profile-card { background: linear-gradient(135deg, #2c1a0e, #1a0f08); border-radius: 20px; padding: 20px; color: white; margin-bottom: 20px; border: 1px solid #d4a017; }
        .profile-pic { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; border: 3px solid #d4a017; }
        .stat-card { background: #2c1a0e; border-radius: 15px; padding: 20px; text-align: center; color: white; transition: 0.3s; border: 1px solid #d4a017; }
        .stat-card:hover { transform: translateY(-5px); background: #3d2b1f; }
        .chat-card { background: #2c1a0e; border: 1px solid #d4a017; border-radius: 20px; padding: 20px; text-align: center; transition: 0.3s; cursor: pointer; color: white; }
        .chat-card:hover { transform: translateY(-5px); background: #3d2b1f; }
        .chat-card i { font-size: 2.5rem; margin-bottom: 10px; color: #d4a017; }
        .patient-card { background: #2c1a0e; border-radius: 15px; padding: 15px; margin-bottom: 15px; transition: 0.2s; border: 1px solid #d4a017; color: white; }
        .patient-card:hover { transform: translateX(5px); background: #3d2b1f; }
        .feature-card { background: #2c1a0e; border: 1px solid #d4a017; border-radius: 15px; padding: 20px 10px; text-align: center; transition: 0.3s; cursor: pointer; color: white; }
        .feature-card:hover { transform: translateY(-5px); background: #3d2b1f; }
        .feature-card i { font-size: 2rem; margin-bottom: 10px; color: #d4a017; }
        footer { background: #1a0f08; color: #c0c0c0; margin-top: auto; padding: 30px 0 20px; border-top: 1px solid #d4a017; }
        footer a { color: #d4a017; transition: 0.2s; }
        footer a:hover { color: white; text-decoration: underline; }
        .modal-content { background: #2c1a0e; border: 1px solid #d4a017; color: white; }
        .modal-header { border-bottom-color: #d4a017; }
        .modal-footer { border-top-color: #d4a017; }
        .form-control, .form-select { background: #3d2b1f; border: 1px solid #d4a017; color: white; }
        .form-control:focus, .form-select:focus { background: #4a3828; color: white; border-color: #d4a017; box-shadow: 0 0 5px #d4a017; }
        .shift-badge { display: inline-block; padding: 5px 15px; border-radius: 20px; font-size: 12px; font-weight: bold; }
        .shift-morning { background: #ffc107; color: #333; }
        .shift-afternoon { background: #17a2b8; color: white; }
        .shift-night { background: #343a40; color: white; }
        .shift-off { background: #dc3545; color: white; }
        .table-dark { background-color: #2c1a0e; }
        .table-dark th { border-color: #d4a017; }
        .btn-outline-warning { border-color: #d4a017; color: #d4a017; }
        .btn-outline-warning:hover { background: #d4a017; color: #1a0f08; }
        a { text-decoration: none; }
        @media (max-width: 768px) { .container { padding: 10px; } .profile-pic { width: 60px; height: 60px; } }
    </style>
</head>
<body>

<!-- Header -->
<nav class="navbar navbar-dark">
    <div class="container">
        <a class="navbar-brand d-flex align-items-center" href="../index.php">
            <img src="https://od.lk/s/MzdfODgyMDMwNTZf/IMG-20260414-WA0014%20%281%29.jpg" alt="UHD Logo">
            <div>
                <div class="brand-title">UNITY HEALTHCARE DATABASE</div>
                <div class="brand-sub">NIGERIA</div>
            </div>
        </a>
        <div class="d-flex align-items-center">
            <a href="#" class="icon-btn" title="Notifications" onclick="alert('No new notifications')">
                <i class="fas fa-bell fa-lg"></i>
                <?php if($unread_notifications > 0): ?>
                    <span class="badge-count"><?= $unread_notifications ?></span>
                <?php endif; ?>
            </a>
            <a href="chat.php" class="icon-btn" title="Messages">
                <i class="fas fa-envelope fa-lg"></i>
                <?php if($unread_chats > 0): ?>
                    <span class="badge-count"><?= $unread_chats ?></span>
                <?php endif; ?>
            </a>
            <span class="text-white mx-2 d-none d-sm-block">Nurse <?= htmlspecialchars($nurse_name) ?></span>
            <a href="logout.php" class="btn btn-danger btn-sm"><i class="fas fa-sign-out-alt"></i> Logout</a>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <!-- Profile Card -->
    <div class="profile-card">
        <div class="row align-items-center">
            <div class="col-auto">
                <img src="<?= $profile_pic ?>" class="profile-pic" alt="Profile">
            </div>
            <div class="col">
                <h3>Nurse <?= htmlspecialchars($nurse_name) ?></h3>
                <p class="mb-1"><i class="fas fa-user-nurse"></i> <?= htmlspecialchars($nurse_credentials) ?></p>
                <p class="mb-1"><i class="fas fa-id-card"></i> Staff ID: <?= htmlspecialchars($nurse_uhd_id) ?></p>
            </div>
            <div class="col-auto">
                <?php if($today_duty): ?>
                    <span class="shift-badge shift-<?= $today_duty['shift'] ?>">
                        <?= ucfirst($today_duty['shift']) ?> Shift
                    </span>
                <?php else: ?>
                    <button class="btn btn-outline-light btn-sm" data-bs-toggle="modal" data-bs-target="#dutyModal">
                        <i class="fas fa-clock"></i> Report Duty
                    </button>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Statistics -->
    <div class="row mb-4 g-3">
        <div class="col-md-3 col-6">
            <div class="stat-card"><i class="fas fa-users fa-2x mb-2"></i><h3><?= $assigned_patients->num_rows ?></h3><p>Assigned Patients</p></div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card"><i class="fas fa-heartbeat fa-2x mb-2"></i><h3><?= $vitals_today ?></h3><p>Vitals Today</p></div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card"><i class="fas fa-calendar-check fa-2x mb-2"></i><h3><?= $today_duty ? ucfirst($today_duty['shift']) : '--' ?></h3><p>Today's Shift</p></div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card"><i class="fas fa-comments fa-2x mb-2"></i><h3><?= $unread_chats ?></h3><p>Unread Messages</p></div>
        </div>
    </div>

    <!-- Chat Cards -->
    <div class="row g-4 mb-4">
        <div class="col-md-3 col-6"><div class="chat-card" onclick="window.location.href='chat.php'"><i class="fas fa-user-md"></i><h6>Doctor Chat</h6><small>Connect with doctors</small></div></div>
        <div class="col-md-3 col-6"><div class="chat-card" onclick="window.location.href='chat.php'"><i class="fas fa-user-circle"></i><h6>Patient Chat</h6><small>Message patients</small></div></div>
        <div class="col-md-3 col-6"><div class="chat-card" onclick="window.location.href='chat.php'"><i class="fas fa-user-nurse"></i><h6>Nurse Chat</h6><small>Team collaboration</small></div></div>
        <div class="col-md-3 col-6"><div class="chat-card" onclick="window.location.href='chat.php'"><i class="fas fa-headset"></i><h6>Staff Chat</h6><small>Admin support</small></div></div>
    </div>

    <!-- Assigned Patients -->
    <div class="card mb-4" style="background: #2c1a0e; border: 1px solid #d4a017;">
        <div class="card-header" style="background: #1a0f08; color: #d4a017; border-bottom: 1px solid #d4a017;">
            <h5 class="mb-0"><i class="fas fa-users me-2"></i>My Assigned Patients</h5>
        </div>
        <div class="card-body">
            <?php if($assigned_patients && $assigned_patients->num_rows > 0): ?>
                <?php while($p = $assigned_patients->fetch_assoc()): ?>
                    <div class="patient-card">
                        <div class="row align-items-center">
                            <div class="col-md-4">
                                <strong><?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?></strong><br>
                                <small>ID: <?= $p['patient_id'] ?></small>
                            </div>
                            <div class="col-md-3"><i class="fas fa-phone"></i> <?= $p['phone'] ?? 'N/A' ?></div>
                            <div class="col-md-3"><i class="fas fa-envelope"></i> <?= $p['email'] ?? 'N/A' ?></div>
                            <div class="col-md-2 text-end">
                                <button class="btn btn-sm btn-outline-warning" onclick="recordVitals(<?= $p['patient_id'] ?>, '<?= htmlspecialchars($p['first_name'] . ' ' . $p['last_name']) ?>')">
                                    <i class="fas fa-heartbeat"></i> Vitals
                                </button>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <p class="text-muted text-center">No patients assigned yet.</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- Recent Vitals -->
    <?php if($recent_vitals && $recent_vitals->num_rows > 0): ?>
    <div class="card mb-4" style="background: #2c1a0e; border: 1px solid #d4a017;">
        <div class="card-header" style="background: #1a0f08; color: #d4a017;">
            <h5 class="mb-0"><i class="fas fa-chart-line me-2"></i>Recent Vitals Recorded</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-dark table-hover">
                    <thead>
                        <tr><th>Patient ID</th><th>BP</th><th>Heart Rate</th><th>Temp</th><th>O2 Sat</th><th>Weight</th><th>Date</th></tr>
                    </thead>
                    <tbody>
                        <?php while($v = $recent_vitals->fetch_assoc()): ?>
                        <tr>
                            <td><?= $v['patient_id'] ?></td>
                            <td><?= $v['blood_pressure'] ?? 'N/A' ?></td>
                            <td><?= $v['heart_rate'] ?? 'N/A' ?> bpm</td>
                            <td><?= $v['temperature'] ?? 'N/A' ?> ”ĘC</td>
                            <td><?= $v['oxygen_saturation'] ?? 'N/A' ?> %</td>
                            <td><?= $v['weight'] ?? 'N/A' ?> kg</td>
                            <td><?= date('M d, H:i', strtotime($v['recorded_date'])) ?></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- Features -->
    <div class="row g-4 mb-4">
        <div class="col-6 col-md-3"><div class="feature-card" onclick="window.location.href='view_patient.php'"><i class="fas fa-user-injured"></i><h6>All Patients</h6><small>View all patients</small></div></div>
        <div class="col-6 col-md-3"><div class="feature-card" onclick="recordVitals()"><i class="fas fa-heartbeat"></i><h6>Record Vitals</h6><small>Add patient vitals</small></div></div>
        <div class="col-6 col-md-3"><div class="feature-card" onclick="window.location.href='view_reports.php'"><i class="fas fa-chart-line"></i><h6>Reports</h6><small>View vitals history</small></div></div>
        <div class="col-6 col-md-3"><div class="feature-card" data-bs-toggle="modal" data-bs-target="#dutyModal"><i class="fas fa-clock"></i><h6>My Schedule</h6><small>View duty roster</small></div></div>
    </div>
</div>

<!-- Duty Modal -->
<div class="modal fade" id="dutyModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-clock"></i> Report Your Duty</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Shift</label>
                        <select name="shift" class="form-select" required>
                            <option value="morning">Morning (8:00 AM - 4:00 PM)</option>
                            <option value="afternoon">Afternoon (4:00 PM - 12:00 AM)</option>
                            <option value="night">Night (12:00 AM - 8:00 AM)</option>
                            <option value="off">Day Off</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Start Time</label>
                        <input type="time" name="start_time" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">End Time</label>
                        <input type="time" name="end_time" class="form-control" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="report_duty" class="btn btn-warning">Report Duty</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Vitals Modal -->
<div class="modal fade" id="vitalsModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-heartbeat"></i> Record Vital Signs</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-info" id="vitalPatientInfo" style="background:#3d2b1f; color:#d4a017; border-color:#d4a017;"></div>
                <form id="vitalsAjaxForm">
                    <input type="hidden" id="vital_patient_id">
                    <div class="mb-3">
                        <label class="form-label">Blood Pressure</label>
                        <input type="text" id="blood_pressure" class="form-control" placeholder="120/80">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Heart Rate (bpm)</label>
                        <input type="number" id="heart_rate" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Temperature (”ĘC)</label>
                        <input type="number" step="0.1" id="temperature" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Oxygen Saturation (%)</label>
                        <input type="number" id="oxygen_saturation" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Weight (kg)</label>
                        <input type="number" step="0.1" id="weight" class="form-control">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-warning" onclick="saveVitals()">Save Vitals</button>
            </div>
        </div>
    </div>
</div>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-md-4 mb-4">
                <h5><i class="fas fa-heartbeat me-2"></i> Unity Healthcare DB</h5>
                <p>Nigeria's integrated health data platform connecting patients, doctors, and administrators.</p>
                <div>
                    <a href="#" class="me-3"><i class="fab fa-facebook-f"></i></a>
                    <a href="#" class="me-3"><i class="fab fa-twitter"></i></a>
                    <a href="#" class="me-3"><i class="fab fa-linkedin-in"></i></a>
                    <a href="#" class="me-3"><i class="fab fa-instagram"></i></a>
                </div>
            </div>
            <div class="col-md-4 mb-4">
                <h5>Quick Links</h5>
                <ul class="list-unstyled">
                    <li><a href="../about.php"><i class="fas fa-info-circle"></i> About UHD</a></li>
                    <li><a href="../how-it-works.php"><i class="fas fa-question-circle"></i> How it Works</a></li>
                    <li><a href="#" data-bs-toggle="modal" data-bs-target="#supportModal"><i class="fas fa-headset"></i> Live Support</a></li>
                </ul>
            </div>
            <div class="col-md-4 mb-4">
                <h5>Contact Us</h5>
                <p><i class="fab fa-whatsapp me-2"></i> WhatsApp: <a href="https://wa.me/2347089364492">+234 708 936 4492</a></p>
                <p><i class="fas fa-phone-alt me-2"></i> Phone: <a href="tel:+2347089364492">+234 708 936 4492</a></p>
                <p><i class="fas fa-envelope me-2"></i> Email: <a href="mailto:inq@uhd.com.ng">inq@uhd.com.ng</a></p>
            </div>
        </div>
        <div class="text-center pt-3 border-top border-secondary">
            <small>&copy; <?= date('Y') ?> Unity Healthcare Database. All rights reserved.</small><br>
            <small>Powered by <a href="https://www.fulgentdigit.com.ng" target="_blank">Fulgent Digital Clinic (R)</a></small>
        </div>
    </div>
</footer>

<!-- Support Modal -->
<div class="modal fade" id="supportModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header bg-warning text-dark">
                <h5 class="modal-title"><i class="fas fa-headset"></i> Live System Support</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Our support team is available 24/7. Reach us via:</p>
                <ul class="list-unstyled">
                    <li><i class="fab fa-whatsapp text-success me-2"></i> WhatsApp: <a href="https://wa.me/2347089364492">+234 708 936 4492</a></li>
                    <li><i class="fas fa-phone-alt text-primary me-2"></i> Phone: <a href="tel:+2347089364492">+234 708 936 4492</a></li>
                    <li><i class="fas fa-envelope text-danger me-2"></i> Email: <a href="mailto:inq@uhd.com.ng">inq@uhd.com.ng</a></li>
                </ul>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function recordVitals(patientId, patientName) {
    if (patientId) {
        $('#vital_patient_id').val(patientId);
        $('#vitalPatientInfo').html('<strong>Patient:</strong> ' + patientName);
    } else {
        $('#vitalPatientInfo').html('<strong>Please select a patient from the list</strong>');
        $('#vital_patient_id').val('');
    }
    $('#vitalsModal').modal('show');
}

function saveVitals() {
    var patientId = $('#vital_patient_id').val();
    if (!patientId) {
        alert('Please select a patient first');
        return;
    }
    
    $.ajax({
        url: window.location.href,
        type: 'POST',
        data: {
            save_vitals: 1,
            patient_id: patientId,
            blood_pressure: $('#blood_pressure').val(),
            heart_rate: $('#heart_rate').val(),
            temperature: $('#temperature').val(),
            oxygen_saturation: $('#oxygen_saturation').val(),
            weight: $('#weight').val()
        },
        dataType: 'json',
        success: function(response) {
            if (response.success) {
                alert('Vitals saved successfully!');
                $('#vitalsModal').modal('hide');
                location.reload();
            } else {
                alert('Error: ' + response.error);
            }
        },
        error: function() {
            alert('Error saving vitals');
        }
    });
}
</script>
</body>
</html>