<?php
// ✅ 1. التحقق من بيانات المستخدم وصلاحياته (بدون أي حذف)
if (isset($_SESSION['user']) && is_array($_SESSION['user'])) {
    $user = $_SESSION['user'];
    $user_role = $user['role'] ?? 'guest'; 
    $user_name = $user['full_name'] ?? 'المستخدم';
} else {
    $user_role = 'guest'; 
    $user_name = 'زائر';
    $user = ['full_name' => 'زائر', 'role' => 'guest'];
}

// ✅ 2. تحديد اللغة والاتجاه
$current_lang = $_SESSION['lang'] ?? 'ar';
$dir = ($current_lang == 'ar') ? 'rtl' : 'ltr';

// دالة الترجمة المحمية
if (!function_exists('__')) {
    function __($key) {
        global $translations;
        return $translations[$key] ?? $key;
    }
}

// ✅ 3. تحديد المسارات النشطة بدقة (تم تعديل المسار ليعمل داخل مجلد system دائماً)
$base_path = '/system/'; 
$current_path = basename($_SERVER['PHP_SELF']);
$request_uri = $_SERVER['REQUEST_URI'];
$current_folder = '';
$folders = ['/students/', '/courses/', '/enroll/', '/payments/', '/expenses/', '/reports/', '/user/', '/attendance/', '/announcements/'];
foreach ($folders as $f) {
    if (strpos($request_uri, $f) !== false) { $current_folder = $f; break; }
}

// ✅ 4. بناء مصفوفة الروابط (تخصيص الصلاحيات مع الترجمة)
$nav_items = [];
$nav_items[__('home')] = ['url' => $base_path . 'dashboard.php', 'icon' => 'fas fa-home', 'folder' => ''];

if ($user_role === 'teacher') {
    $nav_items[__('attendance')] = ['url' => $base_path . 'attendance/attendance.php', 'icon' => 'fas fa-clipboard-check', 'folder' => '/attendance/'];
} 
elseif ($user_role === 'staff') {
    $nav_items[__('students')] = ['url' => $base_path . 'students/list.php', 'icon' => 'fas fa-user-graduate', 'folder' => '/students/'];
    $nav_items[__('courses')] = ['url' => $base_path . 'courses/list.php', 'icon' => 'fas fa-book-open', 'folder' => '/courses/'];
    $nav_items[__('attendance')] = ['url' => $base_path . 'attendance/attendance.php', 'icon' => 'fas fa-clipboard-check', 'folder' => '/attendance/'];
    $nav_items[__('enroll')] = ['url' => $base_path . 'enroll/enroll.php', 'icon' => 'fas fa-user-plus', 'folder' => '/enroll/'];
    $nav_items[__('payments')] = ['url' => $base_path . 'payments/report.php', 'icon' => 'fas fa-money-check-alt', 'folder' => '/payments/'];
}
elseif ($user_role === 'admin') {
    $nav_items[__('students')] = ['url' => $base_path . 'students/list.php', 'icon' => 'fas fa-user-graduate', 'folder' => '/students/'];
    $nav_items[__('courses')] = ['url' => $base_path . 'courses/list.php', 'icon' => 'fas fa-book-open', 'folder' => '/courses/'];
    $nav_items[__('enroll')] = ['url' => $base_path . 'enroll/enroll.php', 'icon' => 'fas fa-user-plus', 'folder' => '/enroll/'];
    $nav_items[__('payments')] = ['url' => $base_path . 'payments/report.php', 'icon' => 'fas fa-money-check-alt', 'folder' => '/payments/'];
    $nav_items[__('expenses')] = ['url' => $base_path . 'expenses/report.php', 'icon' => 'fas fa-hand-holding-usd', 'folder' => '/expenses/'];
    $nav_items[__('announcements')] = ['url' => $base_path . 'announcements/announcements.php', 'icon' => 'fas fa-ad', 'folder' => '/announcements/'];
    $nav_items[__('reports')] = ['url' => $base_path . 'reports/reports_dashboard.php', 'icon' => 'fas fa-chart-bar', 'folder' => '/reports/'];
    $nav_items[__('users')] = ['url' => $base_path . 'user/list.php', 'icon' => 'fas fa-users-cog', 'folder' => '/user/'];
}
?>

<nav class="bg-white dark:bg-gray-900 shadow-lg sticky top-0 z-50 border-b border-gray-100 dark:border-gray-800 font-[Cairo]" dir="<?= $dir ?>">
    <div class="container mx-auto max-w-7xl px-4">
        <div class="flex flex-row justify-between items-center h-16">
            
            <div class="flex items-center gap-3">
                <button class="md:hidden p-2 rounded-lg text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800" id="menu-toggle">
                    <i class="fas fa-bars text-xl" id="menu-icon"></i>
                </button>
            </div>

            <div class="hidden md:flex flex-1 items-center justify-center px-4">
                <div class="flex flex-row items-center gap-1 py-1 overflow-x-auto no-scrollbar">
                    <?php foreach ($nav_items as $label => $item): 
                        $isActive = ($label === __('home') && $current_path === 'dashboard.php') || (!empty($item['folder']) && $current_folder === $item['folder']);
                        $baseStyles = "py-2 px-4 rounded-xl text-[14px] font-extrabold transition-all duration-300 flex items-center gap-2 whitespace-nowrap";
                        $activeStyles = "bg-indigo-600 text-white shadow-md";
                        $inactiveStyles = "text-gray-600 dark:text-gray-400 hover:text-indigo-600 hover:bg-indigo-50 dark:hover:bg-gray-800";
                    ?>
                        <a href="<?= $item['url'] ?>" class="<?= $isActive ? "$baseStyles $activeStyles" : "$baseStyles $inactiveStyles" ?>">
                            <i class="<?= $item['icon'] ?> text-base"></i>
                            <span><?= $label ?></span>
                        </a>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="relative shrink-0">
                <button id="user-menu-btn" class="flex flex-row items-center gap-3 py-1.5 px-3 bg-gray-50 dark:bg-gray-800 rounded-full border border-gray-200 dark:border-gray-700 hover:border-indigo-300 transition shadow-sm flex-nowrap">
                    <i class="fas fa-chevron-down text-[10px] text-gray-400 transition-transform duration-300" id="arrow-icon"></i>
                    <div class="<?= ($current_lang == 'ar') ? 'text-right' : 'text-left' ?> hidden sm:block whitespace-nowrap">
                        <p class="text-[14px] font-black text-gray-900 dark:text-white leading-none mb-0.5"><?= htmlspecialchars($user_name); ?></p>
                        <p class="text-[10px] text-indigo-500 font-bold leading-none uppercase tracking-wide"><?= htmlspecialchars($user_role); ?></p>
                    </div>
                    <div class="w-9 h-9 bg-indigo-600 rounded-full flex items-center justify-center text-white shrink-0 shadow-sm">
                        <i class="fas fa-user text-sm"></i>
                    </div>
                </button>

                <div id="user-menu" class="absolute <?= ($current_lang == 'ar') ? 'left-0' : 'right-0' ?> mt-3 w-48 bg-white dark:bg-gray-800 border dark:border-gray-700 rounded-2xl shadow-2xl hidden z-[60] overflow-hidden">
                    <div class="p-2 space-y-1">
                        <a href="<?= $base_path ?>profile/profile.php" class="flex items-center justify-between px-4 py-2 text-[14px] font-bold text-gray-700 dark:text-gray-200 hover:bg-indigo-50 dark:hover:bg-gray-700 rounded-xl transition">
                             <span><?= __('profile') ?></span> <i class="fas fa-id-card-alt text-indigo-500"></i>
                        </a>
                        <hr class="dark:border-gray-700">
                         <a href="<?= $base_path ?>profile/change_password.php" class="flex items-center justify-between px-4 py-2.5 text-[14px] font-bold text-gray-700 dark:text-gray-200 hover:bg-indigo-50 dark:hover:bg-gray-700 rounded-xl transition">
                         <span><?= __('change_password') ?></span> <i class="fas fa-shield-alt text-amber-500"></i>
                    </a>
                    <hr class="dark:border-gray-700">
                        <a href="<?= $base_path ?>auth/logout.php" class="flex items-center justify-between px-4 py-2 text-[14px] font-bold text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-xl transition">
                             <span><?= __('logout') ?></span> <i class="fas fa-power-off"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div id="mobile-menu" class="hidden md:hidden pb-4 pt-2 border-t dark:border-gray-800 space-y-1 overflow-y-auto max-h-[75vh]">
            <?php foreach ($nav_items as $label => $item): 
                $isActive = ($label === __('home') && $current_path === 'dashboard.php') || (!empty($item['folder']) && $current_folder === $item['folder']);
            ?>
                <a href="<?= $item['url'] ?>" class="flex items-center justify-between p-4 rounded-xl font-extrabold text-[17px] transition-all <?= $isActive ? 'bg-indigo-600 text-white shadow-lg' : 'text-gray-700 dark:text-gray-400 bg-gray-50 dark:bg-gray-800' ?>">
                    <span><?= $label ?></span> <i class="<?= $item['icon'] ?> text-lg"></i>
                </a>
            <?php endforeach; ?>
        </div>
    </div>
</nav>

<style>
    .no-scrollbar::-webkit-scrollbar { display: none; }
    .no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
    #mobile-menu { animation: slideInNav 0.3s ease-out; }
    @keyframes slideInNav { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }
</style>

<script>
    const menuToggle = document.getElementById('menu-toggle');
    const mobileMenu = document.getElementById('mobile-menu');
    const menuIcon = document.getElementById('menu-icon');
    const userBtn = document.getElementById('user-menu-btn');
    const userMenu = document.getElementById('user-menu');
    const arrowIcon = document.getElementById('arrow-icon');

    menuToggle.addEventListener('click', (e) => {
        e.stopPropagation();
        const isHidden = mobileMenu.classList.contains('hidden');
        mobileMenu.classList.toggle('hidden');
        menuIcon.className = isHidden ? 'fas fa-times text-xl' : 'fas fa-bars text-xl';
    });

    userBtn.addEventListener('click', (e) => {
        e.stopPropagation();
        userMenu.classList.toggle('hidden');
        arrowIcon.style.transform = userMenu.classList.contains('hidden') ? 'rotate(0deg)' : 'rotate(180deg)';
    });

    window.addEventListener('click', () => {
        userMenu.classList.add('hidden');
        arrowIcon.style.transform = 'rotate(0deg)';
    });
</script>