<?php
// Include functions if not already included
if (!function_exists('getUnreadNotificationsCount')) {
    require_once __DIR__ . '/functions.php';
}

// Include page permissions helper
require_once __DIR__ . '/page_permissions_helper.php';
?>
<nav id="sidebar" class="sidebar">
    <div class="sidebar-content">
        <!-- Hospital Logo/Header -->
        <div class="sidebar-header">
            <div class="hospital-logo">
                <div class="logo-icon">
                    <img src="assets/images/1222f7f3-2c1c-4177-8f88-794699164d74.png" alt="Hospital Logo">
                </div>
                <div class="logo-text">
                    <h5><?php echo __('hospital_name'); ?></h5>
                    <small><?php echo __('hospital_name_en'); ?></small>
                </div>
            </div>
            <button class="close-btn" id="sidebarCloseBtn" aria-label="Close menu">
                <i class="fas fa-times"></i>
            </button>
            <div class="welcome-msg text-center mt-3 w-100">
                <p class="mb-1 fw-bold"><?php echo __('system_name'); ?></p>
                <small class="opacity-75"><?php echo __('welcome'); ?>, <?php echo htmlspecialchars($_SESSION['full_name'] ?? ''); ?></small>
            </div>
        </div>

        <!-- Main Navigation -->
        <div class="nav-section">
            <div class="nav-section-title"><?php echo __('main_menu'); ?></div>
            <ul class="nav flex-column">
                <!-- لوحة التحكم -->
                <?php if (PagePermissions::canAccessPage('dashboard')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'admin_dashboard.php' ? 'active' : ''; ?>" href="admin_dashboard.php">
                        <i class="fas fa-tachometer-alt"></i>
                        <span><?php echo __('dashboard'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- إدارة المستخدمين -->
                <?php if (PagePermissions::canAccessPage('users')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'complete_user_management.php' ? 'active' : ''; ?>" href="complete_user_management.php">
                        <i class="fas fa-user-shield"></i>
                        <span><?php echo __('user_management'); ?></span>
                    </a>
                </li>
                <?php endif; ?>

                <!-- إدارة الأقسام -->
                <?php if (PagePermissions::canAccessPage('units')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'manage_units.php' ? 'active' : ''; ?>" href="manage_units.php">
                        <i class="fas fa-hospital-user"></i>
                        <span><?php echo __('manage_units'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- إدارة المرضى -->
                <?php if (PagePermissions::canAccessPage('patients')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo in_array(basename($_SERVER['PHP_SELF']), ['manage_patients.php', 'patients.php', 'add_patient.php', 'edit_patient.php', 'view_patient.php']) ? 'active' : ''; ?>" href="manage_patients.php">
                        <i class="fas fa-users"></i>
                        <span><?php echo __('patient_management'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- إدارة المضادات الحيوية -->
                <?php if (PagePermissions::canAccessPage('antibiotics')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo in_array(basename($_SERVER['PHP_SELF']), ['manage_antibiotics.php', 'antibiotics.php', 'add_antibiotic.php', 'edit_antibiotic.php']) ? 'active' : ''; ?>" href="manage_antibiotics.php">
                        <i class="fas fa-pills"></i>
                        <span><?php echo __('antibiotic_management'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- التقارير -->
                <?php if (PagePermissions::canAccessPage('reports')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo in_array(basename($_SERVER['PHP_SELF']), ['reports.php', 'clinical_pharmacy_intervention.php', 'view_medical_event.php', 'edit_report.php']) ? 'active' : ''; ?>" href="reports.php">
                        <i class="fas fa-clipboard-list"></i>
                        <span><?php echo __('reports_monitoring'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- مركز التقارير -->
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'reports_center.php' ? 'active' : ''; ?>" href="reports_center.php">
                        <i class="fas fa-chart-bar"></i>
                        <span><?php echo __('reports_center'); ?></span>
                    </a>
                </li>
            </ul>
        </div>

        <!-- Department Reports - تقارير الأقسام -->
        <div class="nav-section">
            <div class="nav-section-title"><?php echo __('department_reports'); ?></div>
            <ul class="nav flex-column">
                <!-- تقرير العناية المركزة -->
                <?php if (PagePermissions::canAccessPage('icu')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'icu_management.php' ? 'active' : ''; ?>" href="icu_management.php">
                        <i class="fas fa-hospital"></i>
                        <span><?php echo __('icu_report'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- تقرير الحضانة -->
                <?php if (PagePermissions::canAccessPage('nursery')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'nursery_management.php' ? 'active' : ''; ?>" href="nursery_management.php">
                        <i class="fas fa-baby"></i>
                        <span><?php echo __('nursery_report'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- إدارة العمليات -->
                <?php if (PagePermissions::canAccessPage('operations')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'operations_management.php' ? 'active' : ''; ?>" href="operations_management.php">
                        <i class="fas fa-procedures"></i>
                        <span><?php echo __('operations_data_entry'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- الصيدلية الإكلينيكية -->
                <?php if (PagePermissions::canAccessPage('pharmacy')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'clinical_pharmacy_intervention.php' ? 'active' : ''; ?>" href="clinical_pharmacy_intervention.php">
                        <i class="fas fa-pills"></i>
                        <span><?php echo __('clinical_pharmacy_intervention'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- المتابعة والمراقبة -->
                <?php if (PagePermissions::canAccessPage('monitoring')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'monitoring_management.php' ? 'active' : ''; ?>" href="monitoring_management.php">
                        <i class="fas fa-eye"></i>
                        <span><?php echo __('monitoring_management'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- جدول الصيادلة -->
                <?php if (PagePermissions::canAccessPage('pharmacist_schedule')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'pharmacist_schedule.php' ? 'active' : ''; ?>" href="pharmacist_schedule.php">
                        <i class="fas fa-calendar-alt"></i>
                        <span><?php echo __('pharmacist_schedule'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- تقرير فريق العمليات -->
                <?php if (PagePermissions::canAccessPage('operations_team_report')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'operations_team_report.php' ? 'active' : ''; ?>" href="operations_team_report.php">
                        <i class="fas fa-user-md"></i>
                        <span><?php echo __('operations_team_report'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- الأدوية الموزعة لكل عملية -->
                <?php if (PagePermissions::canAccessPage('medication_dispensed_per_operation')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'medication_dispensed_per_operation.php' ? 'active' : ''; ?>" href="medication_dispensed_per_operation.php">
                        <i class="fas fa-syringe"></i>
                        <span><?php echo __('medication_dispensed_per_operation'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- إدارة تقارير الصيدلية الداخلية -->
                <?php if (PagePermissions::canAccessPage('pharmacy_internal_reports')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'pharmacy_internal_reports_management.php' ? 'active' : ''; ?>" href="pharmacy_internal_reports_management.php">
                        <i class="fas fa-file-medical-alt"></i>
                        <span><?php echo __('internal_team_report'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
            </ul>
        </div>

        <!-- Quick Tools - الأدوات السريعة -->
        <div class="nav-section">
            <div class="nav-section-title"><?php echo __('quick_tools'); ?></div>
            <ul class="nav flex-column">
                <!-- مؤشرات الأداء -->
                <?php if (PagePermissions::canAccessPage('kpi')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'key_performance_indicators.php' ? 'active' : ''; ?>" href="key_performance_indicators.php">
                        <i class="fas fa-chart-line"></i>
                        <span><?php echo __('kpi'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- البحث المتقدم -->
                <?php if (PagePermissions::canAccessPage('advanced_search')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'advanced_search.php' ? 'active' : ''; ?>" href="advanced_search.php">
                        <i class="fas fa-search"></i>
                        <span><?php echo __('advanced_search'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- سجل النشاطات -->
                <?php if (PagePermissions::canAccessPage('activity_log')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'activity_log.php' ? 'active' : ''; ?>" href="activity_log.php">
                        <i class="fas fa-history"></i>
                        <span><?php echo __('activity_log'); ?></span>
                    </a>
                </li>
                <?php endif; ?>

                <!-- الإشعارات -->
                <?php if (PagePermissions::canAccessPage('notifications')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'notifications.php' ? 'active' : ''; ?>" href="notifications.php">
                        <i class="fas fa-bell"></i>
                        <span><?php echo __('notifications'); ?></span>
                    </a>
                </li>
                <?php endif; ?>

                <!-- تقارير إدارة المضادات -->
                <?php if (PagePermissions::canAccessPage('antibiotic_management_reports')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'antibiotic_management_reports.php' ? 'active' : ''; ?>" href="antibiotic_management_reports.php">
                        <i class="fas fa-file-medical"></i>
                        <span><?php echo __('antibiotic_management_reports'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
                
                <!-- الإعدادات -->
                <?php if (PagePermissions::canAccessPage('settings')): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'settings.php' ? 'active' : ''; ?>" href="settings.php">
                        <i class="fas fa-cog"></i>
                        <span><?php echo __('settings'); ?></span>
                    </a>
                </li>
                <?php endif; ?>
            </ul>
        </div>
    </div>

    <!-- User Menu - قائمة المستخدم -->
    <div class="sidebar-footer">
            <div class="dropup">
                <a class="nav-link dropdown-toggle d-flex align-items-center gap-2" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-boundary="viewport">
                    <i class="fas fa-user-circle fs-5"></i>
                    <span class="text-truncate"><?php echo htmlspecialchars($_SESSION['full_name'] ?? __('user', 'المستخدم')); ?></span>
                </a>
                <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
                    <!-- الملف الشخصي -->
                    <li><a class="dropdown-item" href="profile.php">
                        <i class="fas fa-user me-2"></i> <?php echo __('profile'); ?>
                    </a></li>
                    
                    <li><hr class="dropdown-divider"></li>
                    
                    <!-- تسجيل الخروج -->
                    <li><a class="dropdown-item text-danger" href="logout.php">
                        <i class="fas fa-sign-out-alt me-2"></i> <?php echo __('logout'); ?>
                    </a></li>
                </ul>
            </div>
    </div>
</nav>

