<?php
// Base URL helper for redirects - only define if not already defined
if (!function_exists('redirect')) {
    function redirect($page) {
        $base = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';
        header('Location: ' . $base . $page);
        exit();
    }
}

// تضمين نظام الترجمة
require_once __DIR__ . '/translation.php';

// تحديد اللغة واتجاه النص
$current_lang = Translation::getLanguage();
$text_direction = Translation::getDirection();
$text_align = Translation::getTextAlign();
?>
<!DOCTYPE html>
<html lang="<?php echo $current_lang; ?>" dir="<?php echo $text_direction; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo isset($page_title) ? $page_title : __('system_name') . ' - ' . __('hospital_name'); ?></title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    
    <!-- Custom CSS -->
    <link href="assets/css/style.css?v=<?php echo time(); ?>" rel="stylesheet">
    <link href="assets/css/print.css?v=<?php echo time(); ?>" rel="stylesheet">
    <link href="assets/css/rtl-ltr.css?v=<?php echo time(); ?>" rel="stylesheet">
    <link href="assets/css/mobile-fixes.css?v=<?php echo time(); ?>" rel="stylesheet">
    
    <!-- DataTables CSS -->
    <link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    
    <!-- DataTables JS -->
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
    
    <!-- Chart.js for Analytics -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <!-- Print and Export Enhancement -->
    <script src="assets/js/print-export.js"></script>
    
    <!-- Hospital Logo and Branding -->
    <link rel="icon" type="image/png" href="assets/images/1222f7f3-2c1c-4177-8f88-794699164d74.png">

    <style>
        /* Fallback / Critical CSS to prevent "eaten" text and missing styles */
        .main-content {
            padding-top: 100px !important;
        }
        @media (max-width: 992px) {
            .main-content {
                padding-top: 75px !important;
                overflow-x: hidden !important;
            }
        }
        @media (max-width: 480px) {
            .main-content {
                padding-top: 68px !important;
            }
        }
        .header h1 {
            line-height: 1.6 !important;
            padding-top: 10px !important;
        }
        /* Ensure stat boxes always have their colors */
        .stat-box.blue { background: linear-gradient(135deg, #26a69a 0%, #00897b 100%) !important; }
        .stat-box.orange { background: linear-gradient(135deg, #ff8a65 0%, #f4511e 100%) !important; }
        .stat-box.teal { background: linear-gradient(135deg, #4fc3f7 0%, #039be5 100%) !important; }
        .stat-box.red { background: linear-gradient(135deg, #ec407a 0%, #d81b60 100%) !important; }
        .stat-box.green { background: linear-gradient(135deg, #81c784 0%, #4caf50 100%) !important; }
        .stat-box.purple { background: linear-gradient(135deg, #9575cd 0%, #673ab7 100%) !important; }
    </style>
</head>
<body>
    <!-- Notification Bell -->
    <button class="notification-bell" id="notificationBell" style="display: none;">
        <i class="fas fa-bell"></i>
        <span class="notification-badge">0</span>
    </button>

    <!-- Mobile Menu Button -->
    <button class="mobile-menu-btn" id="mobileMenuBtn" onclick="toggleSidebarFallback()">
        <i class="fas fa-bars"></i>
    </button>
    
    <!-- Sidebar Overlay -->
    <div class="sidebar-overlay" onclick="toggleSidebarFallback()"></div>

    <script>
        function toggleSidebarFallback() {
            const sidebar = document.getElementById('sidebar');
            const overlay = document.querySelector('.sidebar-overlay');
            if (sidebar) {
                sidebar.classList.toggle('show');
                if (overlay) overlay.classList.toggle('show');
                document.body.style.overflow = sidebar.classList.contains('show') ? 'hidden' : '';
            }
        }
    </script>

    <!-- Sidebar -->
    <?php include 'includes/sidebar.php'; ?>

    <!-- Main Wrapper -->
    <div class="main-wrapper">
        <!-- Main Content -->
        <div class="main-content">
            <!-- Top Header Bar -->
             <div class="top-header-bar no-print">
                 <div class="container-fluid d-flex justify-content-between align-items-center h-100 px-4">
                     <div class="page-info">
                         <h4 class="hospital-name-top mb-0"><?php echo __('hospital_name'); ?></h4>
                     </div>
                     <div class="top-bar-actions d-flex align-items-center gap-3">
                        <!-- Language Switcher inside the bar -->
                        <div class="language-switcher-btns">
                            <?php
                            $available_languages = Translation::getAvailableLanguages();
                            foreach ($available_languages as $lang_code => $lang_info) {
                                $active_class = ($current_lang === $lang_code) ? 'active' : '';
                                echo '<a href="change_language.php?lang=' . $lang_code . '" class="btn btn-sm ' . $active_class . '">';
                                echo $lang_info['flag'] . ' ' . $lang_info['name'];
                                echo '</a>';
                            }
                            ?>
                        </div>
                    </div>
                </div>
            </div>