<?php
require_once __DIR__ . '/../../db/config.php';
require_once __DIR__ . '/../../helpers.php';
require_once __DIR__ . '/../../includes/auth.php';

// Check Auth
check_auth();
$current_user = current_user();

$db = db();
$lang = $_SESSION['lang'];
$section = $section ?? 'dashboard';
$message = $message ?? '';

// Check for flash message in session
if (isset($_SESSION['flash_message'])) {
    $message = $_SESSION['flash_message'];
    unset($_SESSION['flash_message']);
}

// Fetch company settings for dynamic branding
$stmt = $db->query("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('company_name', 'company_logo', 'company_favicon')");
$site_settings = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $site_settings[$row['setting_key']] = $row['setting_value'];
}
$site_name = !empty($site_settings['company_name']) ? $site_settings['company_name'] : __('hospital_management');
$site_logo = !empty($site_settings['company_logo']) ? $site_settings['company_logo'] : null;
$site_favicon = !empty($site_settings['company_favicon']) ? $site_settings['company_favicon'] : null;

?>
<!DOCTYPE html>
<html lang="<?php echo $_SESSION['lang']; ?>" dir="<?php echo get_dir(); ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($site_name); ?></title>
    <?php if ($site_favicon): ?>
    <link rel="icon" type="image/x-icon" href="<?php echo htmlspecialchars($site_favicon); ?>">
    <?php endif; ?>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <?php if (is_rtl()): ?>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css">
    <?php endif; ?>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Tajawal:wght@400;500;700&display=swap" rel="stylesheet">
    <!-- Select2 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" />
    <!-- Summernote Lite CSS -->
    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.css" rel="stylesheet">
    <!-- Flatpickr CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
    
    <script>
    const SYSTEM_SETTINGS = <?php echo json_encode(get_system_settings()); ?>;
    const CURRENCY_SYMBOL = SYSTEM_SETTINGS.currency_symbol || '$';
    const DECIMAL_DIGITS = parseInt(SYSTEM_SETTINGS.decimal_digits || 2);
    
    function formatCurrency(amount) {
        return CURRENCY_SYMBOL + ' ' + parseFloat(amount).toFixed(DECIMAL_DIGITS);
    }
    </script>
    
    <style>
        body { font-family: 'Inter', 'Tajawal', sans-serif; background-color: #f4f7f6; }
        .sidebar { height: 100vh; width: 250px; background-color: #002D62; color: white; transition: all 0.3s; flex-shrink: 0; overflow-y: auto; position: sticky; top: 0; }
        .sidebar::-webkit-scrollbar { width: 6px; }
        .sidebar::-webkit-scrollbar-track { background: transparent; }
        .sidebar::-webkit-scrollbar-thumb { background-color: rgba(255, 255, 255, 0.2); border-radius: 3px; }
        .sidebar::-webkit-scrollbar-thumb:hover { background-color: rgba(255, 255, 255, 0.4); }
        .sidebar-link { color: #cfd8dc; text-decoration: none; padding: 12px 20px; display: block; border-left: 4px solid transparent; }
        .sidebar-link:hover, .sidebar-link.active { background-color: #003a80; color: white; border-left-color: #4fc3f7; }
        .sidebar-submenu { background-color: #001f44; padding-left: 20px; }
        <?php if (is_rtl()): ?>
        .sidebar-submenu { padding-left: 0; padding-right: 20px; }
        <?php endif; ?>
        .main-content { flex: 1; padding: 25px; min-width: 0; }
        .card { border: none; border-radius: 8px; box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075); margin-bottom: 20px; }
        .stat-card { padding: 20px; text-align: center; }
        .stat-card i { font-size: 2.5rem; color: #0056b3; margin-bottom: 10px; }
        .btn-primary { background-color: #0056b3; border-color: #0056b3; }
        .table thead th { background-color: #f8f9fa; border-bottom: 2px solid #dee2e6; color: #495057; font-weight: 600; }
        .navbar { background-color: white; border-bottom: 1px solid #e0e0e0; }
        .card-header, .modal-header { background-color: #002D62 !important; color: white !important; border-bottom: none; }
        .card-header .fw-bold, .modal-title { color: white !important; }
        .modal-header .btn-close { filter: invert(1) grayscale(100%) brightness(200%); }
        .card-header i { color: white !important; }
        <?php if (is_rtl()): ?>
        .sidebar-link { border-left: 0; border-right: 4px solid transparent; }
        .sidebar-link:hover, .sidebar-link.active { border-right-color: #4fc3f7; }
        <?php endif; ?>
        
        /* Select2 custom styling to match Bootstrap 5 */
        .select2-container--bootstrap-5 .select2-selection {
            border: 1px solid #dee2e6;
            border-radius: 0.375rem;
            min-height: calc(1.5em + 0.75rem + 2px);
        }

        /* Summernote custom styling */
        .note-editor.note-frame { border: 1px solid #dee2e6; border-radius: 0.375rem; }
        .note-editor.note-airframe .note-editing-area .note-editable, .note-editor.note-frame .note-editing-area .note-editable { background-color: white; }
    </style>
</head>
<body>

<div class="d-flex">
    <!-- Sidebar -->
    <div class="sidebar d-none d-md-block">
        <div class="p-4 text-center">
            <?php if ($site_logo): ?>
                <img src="<?php echo htmlspecialchars($site_logo); ?>" alt="Logo" class="img-fluid mb-2" style="max-height: 50px;">
            <?php else: ?>
                <h5 class="fw-bold"><i class="bi bi-hospital"></i> <?php echo htmlspecialchars($site_name); ?></h5>
            <?php endif; ?>
        </div>
        <nav class="mt-3">
            <?php if (has_permission('dashboard')): ?>
            <a href="dashboard.php" class="sidebar-link <?php echo $section === 'dashboard' ? 'active' : ''; ?>"><i class="bi bi-speedometer2 me-2"></i> <?php echo __('dashboard'); ?></a>
            <?php endif; ?>
            
            <?php if (has_permission('patients')): ?>
            <a href="patients.php" class="sidebar-link <?php echo $section === 'patients' ? 'active' : ''; ?>"><i class="bi bi-people me-2"></i> <?php echo __('patients'); ?></a>
            <?php endif; ?>

            <?php if (has_permission('visits')): ?>
            <a href="visits.php" class="sidebar-link <?php echo $section === 'visits' ? 'active' : ''; ?>"><i class="bi bi-clipboard2-pulse me-2"></i> <?php echo __('visits'); ?></a>
            <?php endif; ?>

            <?php if (has_permission('appointments')): ?>
            <a href="appointments.php" class="sidebar-link <?php echo $section === "appointments" ? "active" : ""; ?>"><i class="bi bi-calendar-event me-2"></i> <?php echo __("appointments"); ?></a>
            <?php endif; ?>

            <?php if (has_permission('home_visits')): ?>
            <a href="home_visits.php" class="sidebar-link <?php echo $section === 'home_visits' ? 'active' : ''; ?>"><i class="bi bi-house-heart me-2"></i> <?php echo __('home_visits'); ?></a>
            <?php endif; ?>

            <?php if (has_permission('queue')): ?>
            <a href="queue.php" class="sidebar-link <?php echo $section === 'queue' ? 'active' : ''; ?>"><i class="bi bi-list-ol me-2"></i> <?php echo __('queue_management'); ?></a>
            <?php endif; ?>
            
            <?php if (has_permission('laboratory')): ?>
            <a href="#labSubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['laboratory_tests', 'test_groups', 'laboratory_inquiries']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-prescription2 me-2"></i> <?php echo __('laboratory'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['laboratory_tests', 'test_groups', 'laboratory_inquiries']) ? 'show' : ''; ?>" id="labSubmenu">
                <div class="sidebar-submenu">
                    <a href="laboratory_tests.php" class="sidebar-link py-2 <?php echo $section === 'laboratory_tests' ? 'active' : ''; ?>"><i class="bi bi-list-check me-2"></i> <?php echo __('tests'); ?></a>
                    <a href="test_groups.php" class="sidebar-link py-2 <?php echo $section === 'test_groups' ? 'active' : ''; ?>"><i class="bi bi-collection me-2"></i> <?php echo __('test_groups'); ?></a>
                    <a href="laboratory_inquiries.php" class="sidebar-link py-2 <?php echo $section === 'laboratory_inquiries' ? 'active' : ''; ?>"><i class="bi bi-question-circle me-2"></i> <?php echo __('inquiries'); ?></a>
                </div>
            </div>
            <?php endif; ?>

            <?php if (has_permission('xray')): ?>
            <!-- X-Ray Module -->
            <a href="#xraySubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['xray_tests', 'xray_groups', 'xray_inquiries']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-x-diamond me-2"></i> <?php echo __('xray'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['xray_tests', 'xray_groups', 'xray_inquiries']) ? 'show' : ''; ?>" id="xraySubmenu">
                <div class="sidebar-submenu">
                    <a href="xray_tests.php" class="sidebar-link py-2 <?php echo $section === 'xray_tests' ? 'active' : ''; ?>"><i class="bi bi-list-check me-2"></i> <?php echo __('tests'); ?></a>
                    <a href="xray_groups.php" class="sidebar-link py-2 <?php echo $section === 'xray_groups' ? 'active' : ''; ?>"><i class="bi bi-collection me-2"></i> <?php echo __('groups'); ?></a>
                    <a href="xray_inquiries.php" class="sidebar-link py-2 <?php echo $section === 'xray_inquiries' ? 'active' : ''; ?>"><i class="bi bi-question-circle me-2"></i> <?php echo __('inquiries'); ?></a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if (has_permission('pharmacy')): ?>
            <!-- Pharmacy Module -->
            <a href="#pharmacySubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['pharmacy_inventory', 'pharmacy_pos', 'pharmacy_sales', 'drugs', 'drugs_groups', 'suppliers', 'pharmacy_purchases', 'pharmacy_purchase_returns', 'pharmacy_alerts', 'pharmacy_reports']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-capsule me-2"></i> <?php echo __('pharmacy'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['pharmacy_inventory', 'pharmacy_pos', 'pharmacy_sales', 'drugs', 'drugs_groups', 'suppliers', 'pharmacy_purchases', 'pharmacy_purchase_returns', 'pharmacy_alerts', 'pharmacy_reports']) ? 'show' : ''; ?>" id="pharmacySubmenu">
                <div class="sidebar-submenu">
                    <a href="pharmacy_inventory.php" class="sidebar-link py-2 <?php echo $section === 'pharmacy_inventory' ? 'active' : ''; ?>"><i class="bi bi-boxes me-2"></i> <?php echo __('inventory'); ?></a>
                    <a href="pharmacy_pos.php" class="sidebar-link py-2 <?php echo $section === 'pharmacy_pos' ? 'active' : ''; ?>"><i class="bi bi-cart-check me-2"></i> <?php echo __('pos'); ?></a>
                    <a href="pharmacy_sales.php" class="sidebar-link py-2 <?php echo $section === 'pharmacy_sales' ? 'active' : ''; ?>"><i class="bi bi-receipt me-2"></i> <?php echo __('sales_history'); ?></a>
                    <a href="pharmacy_purchases.php" class="sidebar-link py-2 <?php echo $section === 'pharmacy_purchases' ? 'active' : ''; ?>"><i class="bi bi-basket2 me-2"></i> <?php echo __('purchases'); ?></a>
                    <a href="pharmacy_purchase_returns.php" class="sidebar-link py-2 <?php echo $section === 'pharmacy_purchase_returns' ? 'active' : ''; ?>"><i class="bi bi-arrow-return-left me-2"></i> <?php echo __('purchase_returns'); ?></a>
                    <a href="pharmacy_alerts.php" class="sidebar-link py-2 <?php echo $section === 'pharmacy_alerts' ? 'active' : ''; ?>"><i class="bi bi-exclamation-triangle me-2"></i> <?php echo __('alerts'); ?></a>
                    <a href="pharmacy_reports.php" class="sidebar-link py-2 <?php echo $section === "pharmacy_reports" ? "active" : ""; ?>"><i class="bi bi-file-earmark-bar-graph me-2"></i> <?php echo __("reports"); ?></a>
                    <div class="border-top my-1 border-secondary" style="border-color: #0d4680 !important;"></div>
                    <a href="drugs.php" class="sidebar-link py-2 <?php echo $section === 'drugs' ? 'active' : ''; ?>"><i class="bi bi-list-check me-2"></i> <?php echo __('drugs'); ?></a>
                    <a href="drugs_groups.php" class="sidebar-link py-2 <?php echo $section === 'drugs_groups' ? 'active' : ''; ?>"><i class="bi bi-collection me-2"></i> <?php echo __('groups'); ?></a>
                    <a href="suppliers.php" class="sidebar-link py-2 <?php echo $section === 'suppliers' ? 'active' : ''; ?>"><i class="bi bi-truck me-2"></i> <?php echo __('suppliers'); ?></a>
                </div>
            </div>
            <?php endif; ?>

            <?php if (has_permission('inventory')): ?>
            <!-- Stock Management Module -->
            <a href="#inventorySubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['inventory_dashboard', 'inventory_items', 'inventory_categories', 'inventory_transactions', 'inventory_reports']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-box-seam me-2"></i> <?php echo __('stock_management'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['inventory_dashboard', 'inventory_items', 'inventory_categories', 'inventory_transactions', 'inventory_reports']) ? 'show' : ''; ?>" id="inventorySubmenu">
                <div class="sidebar-submenu">
                    <a href="inventory_dashboard.php" class="sidebar-link py-2 <?php echo $section === 'inventory_dashboard' ? 'active' : ''; ?>"><i class="bi bi-speedometer2 me-2"></i> <?php echo __('dashboard'); ?></a>
                    <a href="inventory_items.php" class="sidebar-link py-2 <?php echo $section === 'inventory_items' ? 'active' : ''; ?>"><i class="bi bi-boxes me-2"></i> <?php echo __('items'); ?></a>
                    <a href="inventory_categories.php" class="sidebar-link py-2 <?php echo $section === 'inventory_categories' ? 'active' : ''; ?>"><i class="bi bi-tags me-2"></i> <?php echo __('categories'); ?></a>
                    <a href="inventory_transactions.php" class="sidebar-link py-2 <?php echo $section === 'inventory_transactions' ? 'active' : ''; ?>"><i class="bi bi-arrow-left-right me-2"></i> <?php echo __('transactions'); ?></a>
                    <a href="inventory_reports.php" class="sidebar-link py-2 <?php echo $section === 'inventory_reports' ? 'active' : ''; ?>"><i class="bi bi-file-earmark-bar-graph me-2"></i> <?php echo __('reports'); ?></a>
                </div>
            </div>
            <?php endif; ?>

            <?php if (has_permission('billing')): ?>
            <a href="billing.php" class="sidebar-link <?php echo $section === 'billing' ? 'active' : ''; ?>"><i class="bi bi-receipt me-2"></i> <?php echo __('billing'); ?></a>
            <?php endif; ?>
            
            <?php if (has_permission('insurance')): ?>
            <a href="insurance.php" class="sidebar-link <?php echo $section === 'insurance' ? 'active' : ''; ?>"><i class="bi bi-shield-check me-2"></i> <?php echo __('insurance'); ?></a>
            <?php endif; ?>
            
            <?php if (has_permission('reports')): ?>
            <a href="#reportsSubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['reports', 'sick_leave_report']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-bar-chart-line me-2"></i> <?php echo __('reports'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['reports', 'sick_leave_report']) ? 'show' : ''; ?>" id="reportsSubmenu">
                <div class="sidebar-submenu">
                    <a href="reports.php" class="sidebar-link py-2 <?php echo $section === 'reports' ? 'active' : ''; ?>"><i class="bi bi-graph-up me-2"></i> <?php echo __('general_statistics'); ?></a>
                    <a href="sick_leave_report.php" class="sidebar-link py-2 <?php echo $section === 'sick_leave_report' ? 'active' : ''; ?>"><i class="bi bi-file-medical me-2"></i> <?php echo __('sick_leave_report'); ?></a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if (has_permission('users')): ?>
            <a href="#usersSubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['users', 'roles']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-people-fill me-2"></i> <?php echo __('users_management'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['users', 'roles']) ? 'show' : ''; ?>" id="usersSubmenu">
                <div class="sidebar-submenu">
                    <a href="users.php" class="sidebar-link py-2 <?php echo $section === 'users' ? 'active' : ''; ?>"><i class="bi bi-person me-2"></i> <?php echo __('users'); ?></a>
                    <a href="roles.php" class="sidebar-link py-2 <?php echo $section === 'roles' ? 'active' : ''; ?>"><i class="bi bi-shield-lock me-2"></i> <?php echo __('roles'); ?></a>
                </div>
            </div>
            <?php endif; ?>

            
            <?php if (has_permission('hr')): ?>
            <a href="#hrSubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ["hr_dashboard", "hr_attendance", "hr_leaves"]) ? "active" : ""; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-person-badge-fill me-2"></i> <?php echo __("hr_management"); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ["hr_dashboard", "hr_attendance", "hr_leaves"]) ? "show" : ""; ?>" id="hrSubmenu">
                <div class="sidebar-submenu">
                    <a href="hr_dashboard.php" class="sidebar-link py-2 <?php echo $section === "hr_dashboard" ? "active" : ""; ?>"><i class="bi bi-speedometer2 me-2"></i> <?php echo __("dashboard"); ?></a>
                    <a href="hr_attendance.php" class="sidebar-link py-2 <?php echo $section === "hr_attendance" ? "active" : ""; ?>"><i class="bi bi-clock-history me-2"></i> <?php echo __("attendance"); ?></a>
                    <a href="hr_leaves.php" class="sidebar-link py-2 <?php echo $section === "hr_leaves" ? "active" : ""; ?>"><i class="bi bi-calendar-range me-2"></i> <?php echo __("leave_requests"); ?></a>
                </div>
            </div>
            <?php endif; ?>

            <?php if (has_permission('settings')): ?>
            <a href="#settingsSubmenu" data-bs-toggle="collapse" class="sidebar-link <?php echo in_array($section, ['employees', 'positions', 'company_profile', 'cities', 'services', 'departments', 'queue_ads']) ? 'active' : ''; ?> d-flex justify-content-between align-items-center">
                <span><i class="bi bi-gear me-2"></i> <?php echo __('settings'); ?></span>
                <i class="bi bi-chevron-down small"></i>
            </a>
            <div class="collapse <?php echo in_array($section, ['employees', 'positions', 'company_profile', 'cities', 'services', 'departments', 'queue_ads']) ? 'show' : ''; ?>" id="settingsSubmenu">
                <div class="sidebar-submenu">
                    <a href="settings.php" class="sidebar-link py-2 <?php echo $section === 'company_profile' ? 'active' : ''; ?>"><i class="bi bi-building me-2"></i> <?php echo __('company_profile'); ?></a>
                    <a href="employees.php" class="sidebar-link py-2 <?php echo $section === 'employees' ? 'active' : ''; ?>"><i class="bi bi-person-workspace me-2"></i> <?php echo __('employees'); ?></a>
                    <a href="positions.php" class="sidebar-link py-2 <?php echo $section === 'positions' ? 'active' : ''; ?>"><i class="bi bi-diagram-2 me-2"></i> <?php echo __('positions'); ?></a>
                    <a href="departments.php" class="sidebar-link py-2 <?php echo $section === 'departments' ? 'active' : ''; ?>"><i class="bi bi-diagram-3 me-2"></i> <?php echo __('departments'); ?></a>
                    <a href="hospital_services.php" class="sidebar-link py-2 <?php echo $section === 'services' ? 'active' : ''; ?>"><i class="bi bi-activity me-2"></i> <?php echo __('services'); ?></a>
                    <a href="cities.php" class="sidebar-link py-2 <?php echo $section === 'cities' ? 'active' : ''; ?>"><i class="bi bi-building me-2"></i> <?php echo __('cities'); ?></a>
                    <a href="queue_ads.php" class="sidebar-link py-2 <?php echo $section === 'queue_ads' ? 'active' : ''; ?>"><i class="bi bi-megaphone me-2"></i> <?php echo __('queue_ads'); ?></a>
                </div>
            </div>
            <?php endif; ?>
        </nav>
    </div>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Top Navbar -->
        <nav class="navbar navbar-expand-lg navbar-light mb-4 rounded shadow-sm px-3">
            <div class="container-fluid p-0">
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#topNav">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse justify-content-end" id="topNav">
                    <div class="d-flex align-items-center">
                        <a href="?lang=<?php echo get_lang_code() === 'ar' ? 'en' : 'ar'; ?>" class="btn btn-outline-secondary btn-sm me-3">
                            <i class="bi bi-translate"></i> <?php echo get_lang_name(); ?>
                        </a>
                        <div class="dropdown">
                            <a class="nav-link dropdown-toggle d-flex align-items-center" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                <?php 
                                $user_avatar = !empty($current_user['avatar']) ? $current_user['avatar'] : "https://ui-avatars.com/api/?name=" . urlencode($current_user['name']) . "&background=0056b3&color=fff";
                                ?>
                                <img src="<?php echo htmlspecialchars($user_avatar); ?>" class="rounded-circle me-2" width="32" height="32" style="object-fit: cover;">
                                <span><?php echo htmlspecialchars($current_user['name']); ?></span>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end shadow border-0">
                                <li><a class="dropdown-item" href="profile.php"><i class="bi bi-person me-2"></i> <?php echo __('profile'); ?> (<?php echo htmlspecialchars($current_user['role_slug']); ?>)</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li><a class="dropdown-item text-danger" href="logout.php"><i class="bi bi-box-arrow-right me-2"></i> <?php echo __('logout'); ?></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </nav>

        <?php if ($message): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo $message; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <?php endif; ?>