<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>آکادمی حنفی‌کار | پنل فروش</title>
    <link rel="icon" type="image/png" href="/public/logo.png">
    <link rel="stylesheet" href="/assets/vendor/tailwind/tailwind.min.css">
    <link rel="stylesheet" href="/assets/vendor/fontawesome/css/all.min.css">
    <!--
      PERF: all scripts use `defer` so they no longer block HTML parsing / first paint.
      Deferred scripts execute in document order after the HTML is parsed and before
      DOMContentLoaded, which preserves the required jQuery -> datepicker ordering and
      guarantees these libs are ready before app.js (also deferred) initializes.
      The three heavy export/chart libs (apexcharts ~528KB, jspdf ~356KB, xlsx ~932KB)
      are intentionally NOT loaded here — app.js lazy-loads them on first use via
      ensureVendor(), so screens that never export/chart never download ~1.8MB.
    -->
    <script defer src="/assets/vendor/sweetalert2/sweetalert2.all.min.js"></script>
    <!-- jQuery must be loaded before persian-datepicker -->
    <script defer src="/assets/vendor/jquery/jquery-3.6.0.min.js"></script>
    <!-- Persian date core (used by existing app.js conversion helpers) -->
    <script defer src="/assets/vendor/persian-date/persian-date.min.js"></script>
    <!-- Custom Jalali Date Picker + jQuery compatibility adapter -->
    <?php
        $jdpVersion = @filemtime(__DIR__ . '/../../assets/vendor/neo-datepicker/jalali-datepicker.js') ?: time();
        $jdpAdapterVersion = @filemtime(__DIR__ . '/../../assets/vendor/neo-datepicker/persian-datepicker-adapter.js') ?: time();
    ?>
    <script defer src="/assets/vendor/neo-datepicker/jalali-datepicker.js?v=<?php echo $jdpVersion; ?>"></script>
    <script defer src="/assets/vendor/neo-datepicker/persian-datepicker-adapter.js?v=<?php echo $jdpAdapterVersion; ?>"></script>
    <!-- Moment.js (fallback conversions still supported in app.js) -->
    <script defer src="/assets/vendor/moment/moment.min.js"></script>
    <?php $mainCssVersion = @filemtime(__DIR__ . '/../../assets/css/main.css') ?: time(); ?>
    <link rel="stylesheet" href="/assets/css/main.css?v=<?php echo $mainCssVersion; ?>">
</head>
<body class="h-screen overflow-hidden">

<?php echo $content ?? ''; ?>

<script>
    // Define APP_USER and CURRENT_USER_ID before loading app.js
    <?php
    $appDebugEnv = getenv('APP_DEBUG');
    $appEnv = strtolower((string)(getenv('APP_ENV') ?: ''));
    $appDebug = $appDebugEnv !== false
        ? in_array(strtolower((string)$appDebugEnv), ['1', 'true', 'yes', 'on'], true)
        : ($appEnv !== 'production');
    $featureHistoryEnv = getenv('FEATURE_SPA_HISTORY_V1');
    $featureHistory = $featureHistoryEnv !== false
        ? in_array(strtolower((string)$featureHistoryEnv), ['1', 'true', 'yes', 'on'], true)
        : true;
    $featureFreshnessEnv = getenv('FEATURE_MODAL_FRESHNESS_V1');
    $featureFreshness = $featureFreshnessEnv !== false
        ? !in_array(strtolower((string)$featureFreshnessEnv), ['0', 'false', 'no', 'off'], true)
        : true;
    ?>
    window.APP_DEBUG = <?php echo $appDebug ? 'true' : 'false'; ?>;
    window.FEATURE_SPA_HISTORY_V1 = <?php echo $featureHistory ? 'true' : 'false'; ?>;
    window.FEATURE_MODAL_FRESHNESS_V1 = <?php echo $featureFreshness ? 'true' : 'false'; ?>;
    window.APP_USER = <?php echo json_encode([
        'role' => $_SESSION['role'] ?? 'guest',
        'didar_id' => $_SESSION['didar_id'] ?? null,
        'name' => $_SESSION['name'] ?? ''
    ], JSON_UNESCAPED_UNICODE); ?>;
    window.CURRENT_USER_ID = <?php echo $_SESSION['user_id'] ?? 0; ?>;
</script>
	<?php $loggerJsVersion = @filemtime(__DIR__ . '/../../assets/js/logger.js') ?: time(); ?>
	<script defer src="/assets/js/logger.js?v=<?php echo $loggerJsVersion; ?>"></script>
	<?php $appJsVersion = @filemtime(__DIR__ . '/../../assets/js/app.js') ?: time(); ?>
	<script defer src="/assets/js/app.js?v=<?php echo $appJsVersion; ?>"></script>
<script>
    // Initialize on page load
    document.addEventListener('DOMContentLoaded', () => {
        <?php if(isset($_SESSION['user_id'])): ?>
            // Set user display
            const userDisplay = document.getElementById('user-display');
            if(userDisplay) {
                const role = '<?php echo $_SESSION['role'] ?? ''; ?>';
                let roleText = '';
                if (role === 'admin') roleText = ' (مدیر)';
                else if (role === 'crm_specialist') roleText = ' (کارشناس CRM)';
                else if (role === 'accountant') roleText = ' (حسابدار)';
                
                userDisplay.textContent = '<?php echo addslashes($_SESSION['name'] ?? ''); ?>' + roleText;
            }
            <?php if(($_SESSION['role'] ?? '') === 'admin'): ?>
                const adminSection = document.getElementById('admin-section');
                if(adminSection) adminSection.classList.remove('hidden');
            <?php endif; ?>
            if (typeof app !== 'undefined') {
                app.init();
                // Update sound button after init
                setTimeout(() => {
                    if (app.updateSoundButton) {
                        app.updateSoundButton();
                    }
                }, 100);
            }
        <?php endif; ?>
    });
</script>
</body>
</html>

