<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Define site constants if not defined
if (!defined('SITE_NAME')) {
    define('SITE_NAME', 'Unity Healthcare Database');
}

// Hide header flag for special pages
$hide_header = $hide_header ?? false;

if (!$hide_header) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title><?= isset($page_title) ? $page_title . ' - ' : '' ?><?= SITE_NAME ?></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">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: #f5f7fb;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }
        
        .uhd-header {
            background: linear-gradient(135deg, #0a3d2f 0%, #064e3b 100%);
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }
        
        .top-bar {
            background: rgba(0,0,0,0.2);
            padding: 5px 0;
            font-size: 11px;
        }
        
        .top-bar .contact-info {
            display: flex;
            gap: 20px;
            justify-content: flex-end;
        }
        
        .top-bar .contact-info a {
            color: rgba(255,255,255,0.75);
            text-decoration: none;
            transition: 0.3s;
            font-size: 0.7rem;
        }
        
        .top-bar .contact-info a:hover {
            color: #d4a017;
        }
        
        .top-bar .contact-info i {
            margin-right: 5px;
            font-size: 0.65rem;
        }
        
        .main-header {
            padding: 8px 0;
        }
        
        .navbar-brand {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .logo-img {
            height: 40px;
            width: auto;
            border-radius: 8px;
        }
        
        .brand-text h1 {
            color: #d4a017;
            font-weight: 700;
            margin: 0;
            font-size: 1rem;
            letter-spacing: 0.5px;
        }
        
        .brand-text p {
            color: rgba(255,255,255,0.7);
            margin: 0;
            font-size: 0.55rem;
            letter-spacing: 0.5px;
        }
        
        .nav-link {
            color: rgba(255,255,255,0.85) !important;
            font-weight: 500;
            transition: 0.3s;
            font-size: 0.8rem;
            padding: 6px 12px !important;
            border-radius: 6px;
        }
        
        .nav-link:hover {
            color: #d4a017 !important;
            background: rgba(212,160,23,0.1);
        }
        
        .nav-link i {
            margin-right: 5px;
            font-size: 0.75rem;
        }
        
        .user-dropdown {
            position: relative;
            display: inline-block;
        }
        
        .user-dropdown-btn {
            background: transparent;
            border: none;
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 5px 10px;
            border-radius: 30px;
            cursor: pointer;
            transition: 0.3s;
        }
        
        .user-dropdown-btn:hover {
            background: rgba(255,255,255,0.1);
        }
        
        .user-avatar {
            width: 35px;
            height: 35px;
            background: linear-gradient(135deg, #d4a017, #b8860b);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #064e3b;
            font-weight: bold;
            font-size: 0.85rem;
        }
        
        .user-name {
            color: white;
            font-size: 0.85rem;
            font-weight: 500;
        }
        
        .dropdown-arrow {
            color: white;
            font-size: 0.7rem;
            transition: transform 0.3s;
        }
        
        .user-dropdown-content {
            display: none;
            position: absolute;
            right: 0;
            top: 45px;
            background: #064e3b;
            min-width: 240px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            z-index: 1000;
            border: 1px solid rgba(212,160,23,0.3);
        }
        
        .user-dropdown-content a {
            color: white;
            padding: 10px 18px;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 0.8rem;
            transition: 0.2s;
        }
        
        .user-dropdown-content a:hover {
            background: #d4a017;
            color: #064e3b;
        }
        
        .user-dropdown-content .divider {
            height: 1px;
            background: rgba(212,160,23,0.3);
            margin: 5px 0;
        }
        
        .show {
            display: block;
        }
        
        @media (max-width: 768px) {
            .top-bar .contact-info {
                justify-content: center;
                gap: 12px;
                flex-wrap: wrap;
            }
            .logo-img {
                height: 35px;
            }
            .brand-text h1 {
                font-size: 0.85rem;
            }
            .user-name {
                display: none;
            }
        }
    </style>
</head>
<body>

<header class="uhd-header">
    <div class="top-bar">
        <div class="container">
            <div class="contact-info">
                <a href="tel:+2347089364492"><i class="fas fa-phone-alt"></i> +234 708 936 4492</a>
                <a href="mailto:info@uhd.com.ng"><i class="fas fa-envelope"></i> info@uhd.com.ng</a>
                <a href="#"><i class="fas fa-clock"></i> 24/7 Available</a>
            </div>
        </div>
    </div>
    
    <div class="main-header">
        <div class="container">
            <nav class="navbar navbar-expand-lg p-0">
                <a class="navbar-brand" href="/index.php">
                    <img src="https://od.lk/s/MzdfODgyMDMwNTZf/IMG-20260414-WA0014%20%281%29.jpg" alt="UHD Logo" class="logo-img" onerror="this.src='https://ui-avatars.com/api/?background=0a3d2f&color=d4a017&name=UHD'">
                    <div class="brand-text">
                        <h1>UNITY HEALTHCARE DATABASE</h1>
                        <p>NIGERIA</p>
                    </div>
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav ms-auto align-items-center">
                        <?php if (isset($_SESSION['doctor_id']) || isset($_SESSION['user_id'])): ?>
                            <?php 
                                // Determine user role and ID
                                if (isset($_SESSION['doctor_id'])) {
                                    $user_role = 'doctor';
                                    $user_id = $_SESSION['doctor_id'];
                                    $user_name = $_SESSION['doctor_name'] ?? 'Doctor';
                                } elseif (isset($_SESSION['patient_id'])) {
                                    $user_role = 'patient';
                                    $user_id = $_SESSION['patient_id'];
                                    $user_name = $_SESSION['patient_name'] ?? 'Patient';
                                } elseif (isset($_SESSION['user_role'])) {
                                    $user_role = $_SESSION['user_role'];
                                    $user_name = $_SESSION['user_name'] ?? 'User';
                                } else {
                                    $user_role = 'guest';
                                    $user_name = 'Guest';
                                }
                            ?>
                            
                            <?php if ($user_role == 'patient'): ?>
                                <li class="nav-item"><a class="nav-link" href="/patient/dashboard.php"><i class="fas fa-home"></i> Dashboard</a></li>
                                <li class="nav-item"><a class="nav-link" href="/patient/appointments.php"><i class="fas fa-calendar-alt"></i> Appointments</a></li>
                                <li class="nav-item"><a class="nav-link" href="/patient/consultations.php"><i class="fas fa-video"></i> Consultations</a></li>
                                <li class="nav-item"><a class="nav-link" href="/patient/messages.php"><i class="fas fa-comments"></i> Messages</a></li>
                            <?php elseif ($user_role == 'doctor'): ?>
                                <li class="nav-item"><a class="nav-link" href="/doctor/dashboard.php"><i class="fas fa-home"></i> Dashboard</a></li>
                                <li class="nav-item"><a class="nav-link" href="/doctor/my_patients.php"><i class="fas fa-users"></i> My Patients</a></li>
                                <li class="nav-item"><a class="nav-link" href="/doctor/consultations.php"><i class="fas fa-video"></i> Consultations</a></li>
                                <li class="nav-item"><a class="nav-link" href="/doctor/doctor_engagement.php"><i class="fas fa-handshake"></i> Engagement</a></li>
                            <?php elseif ($user_role == 'nurse'): ?>
                                <li class="nav-item"><a class="nav-link" href="/nurse/dashboard.php"><i class="fas fa-home"></i> Dashboard</a></li>
                                <li class="nav-item"><a class="nav-link" href="/nurse/patients.php"><i class="fas fa-users"></i> Patients</a></li>
                                <li class="nav-item"><a class="nav-link" href="/nurse/vitals.php"><i class="fas fa-heartbeat"></i> Vitals</a></li>
                            <?php elseif ($user_role == 'staff'): ?>
                                <li class="nav-item"><a class="nav-link" href="/staff/dashboard.php"><i class="fas fa-home"></i> Dashboard</a></li>
                                <li class="nav-item"><a class="nav-link" href="/staff/patients.php"><i class="fas fa-users"></i> Patients</a></li>
                                <li class="nav-item"><a class="nav-link" href="/staff/billing.php"><i class="fas fa-file-invoice-dollar"></i> Billing</a></li>
                            <?php elseif ($user_role == 'admin'): ?>
                                <li class="nav-item"><a class="nav-link" href="/admin/dashboard.php"><i class="fas fa-home"></i> Dashboard</a></li>
                                <li class="nav-item"><a class="nav-link" href="/admin/manage_users.php"><i class="fas fa-users"></i> Users</a></li>
                                <li class="nav-item"><a class="nav-link" href="/admin/reports.php"><i class="fas fa-chart-line"></i> Reports</a></li>
                            <?php endif; ?>
                            
                            <li class="nav-item user-dropdown">
                                <button class="user-dropdown-btn" onclick="toggleDropdown()">
                                    <div class="user-avatar">
                                        <i class="fas fa-user-circle"></i>
                                    </div>
                                    <span class="user-name"><?= htmlspecialchars($user_name) ?></span>
                                    <i class="fas fa-chevron-down dropdown-arrow" id="dropdownArrow"></i>
                                </button>
                                <div class="user-dropdown-content" id="userDropdown">
                                    <a href="/<?= $user_role ?>/profile.php"><i class="fas fa-user-circle"></i> My Profile</a>
                                    <a href="/<?= $user_role ?>/change_password.php"><i class="fas fa-key"></i> Change Password</a>
                                    <a href="/<?= $user_role ?>/notifications.php"><i class="fas fa-bell"></i> Notifications</a>
                                    
                                    <?php if ($user_role == 'patient'): ?>
                                        <a href="/patient/medical_records.php"><i class="fas fa-folder-open"></i> Medical Records</a>
                                        <a href="/patient/prescriptions.php"><i class="fas fa-prescription-bottle"></i> Prescriptions</a>
                                        <a href="/patient/ai_summary.php"><i class="fas fa-robot"></i> AI Assistant</a>
                                    <?php elseif ($user_role == 'doctor'): ?>
                                        <a href="/doctor/patient_messages.php"><i class="fas fa-comments"></i> Patient Messages</a>
                                        <a href="/doctor/doctor_chat.php"><i class="fas fa-user-md"></i> Doctor Chat</a>
                                        <a href="/doctor/doctor_engagement.php"><i class="fas fa-handshake"></i> Engagement Hub</a>
                                        <a href="/doctor/doctor_payments.php"><i class="fas fa-money-bill-wave"></i> Payments</a>
                                        <a href="/doctor/ai_assistant.php"><i class="fas fa-robot"></i> AI Assistant</a>
                                    <?php elseif ($user_role == 'nurse'): ?>
                                        <a href="/nurse/chat.php"><i class="fas fa-comments"></i> Messages</a>
                                    <?php elseif ($user_role == 'staff'): ?>
                                        <a href="/staff/chat.php"><i class="fas fa-comments"></i> Messages</a>
                                    <?php endif; ?>
                                    
                                    <div class="divider"></div>
                                    <a href="/logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
                                </div>
                            </li>
                        <?php else: ?>
                            <li class="nav-item"><a class="nav-link" href="/index.php"><i class="fas fa-home"></i> Home</a></li>
                            <li class="nav-item"><a class="nav-link" href="/about.php"><i class="fas fa-info-circle"></i> About</a></li>
                            <li class="nav-item"><a class="nav-link" href="/services.php"><i class="fas fa-stethoscope"></i> Services</a></li>
                            <li class="nav-item"><a class="nav-link" href="/contact.php"><i class="fas fa-envelope"></i> Contact</a></li>
                            <li class="nav-item"><a class="nav-link" href="/patient/login.php"><i class="fas fa-user"></i> Patient Login</a></li>
                            <li class="nav-item"><a class="nav-link" href="/doctor/login.php"><i class="fas fa-user-md"></i> Doctor Login</a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </nav>
        </div>
    </div>
</header>

<script>
function toggleDropdown() {
    var dropdown = document.getElementById('userDropdown');
    var arrow = document.getElementById('dropdownArrow');
    
    if (dropdown.classList.contains('show')) {
        dropdown.classList.remove('show');
        if (arrow) arrow.style.transform = 'rotate(0deg)';
    } else {
        dropdown.classList.add('show');
        if (arrow) arrow.style.transform = 'rotate(180deg)';
    }
}

// Close dropdown when clicking outside
document.addEventListener('click', function(event) {
    var dropdown = document.getElementById('userDropdown');
    var button = document.querySelector('.user-dropdown-btn');
    
    if (button && !button.contains(event.target) && dropdown && !dropdown.contains(event.target)) {
        dropdown.classList.remove('show');
        var arrow = document.getElementById('dropdownArrow');
        if (arrow) arrow.style.transform = 'rotate(0deg)';
    }
});
</script>

<main>
<?php
} // end if hide_header
?>