<!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">
    <script src="/assets/vendor/sweetalert2/sweetalert2.all.min.js"></script>
    <!-- jQuery must be loaded before persian-datepicker -->
    <script src="/assets/vendor/jquery/jquery-3.6.0.min.js"></script>
    <script src="/assets/vendor/apexcharts/apexcharts.min.js"></script>
    <script src="/assets/vendor/jspdf/jspdf.umd.min.js"></script>
    <!-- Persian date core (used by existing app.js conversion helpers) -->
    <script src="/assets/vendor/persian-date/persian-date.min.js"></script>
    <!-- Custom Jalali Date Picker + jQuery compatibility adapter -->
    <script src="/assets/vendor/neo-datepicker/jalali-datepicker.js"></script>
    <script src="/assets/vendor/neo-datepicker/persian-datepicker-adapter.js"></script>
    <!-- Moment.js (fallback conversions still supported in app.js) -->
    <script 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 src="/assets/js/logger.js?v=<?php echo $loggerJsVersion; ?>"></script>
	<?php $appJsVersion = @filemtime(__DIR__ . '/../../assets/js/app.js') ?: time(); ?>
	<script 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>

