<?php defined('ABSPATH') || exit;
$pg = $_GET['page'] ?? 'dzqcod';
$plan = \DZQCOD\Core::opt('license_plan', 'free');

$menu = [
    ['slug'=>'dzqcod',             'icon'=>'📊','label'=>'لوحة التحكم',    'group'=>'main'],
    ['slug'=>'dzqcod-orders',      'icon'=>'📦','label'=>'الطلبات',        'group'=>'main',   'badge'=>'orders'],
    ['slug'=>'dzqcod-products',    'icon'=>'🏷️','label'=>'المنتجات',       'group'=>'main'],
    ['slug'=>'dzqcod-pos',         'icon'=>'🖥️','label'=>'نقطة البيع POS', 'group'=>'main'],
    ['slug'=>'dzqcod-workshop',    'icon'=>'🏭','label'=>'لوحة الورشة',    'group'=>'main'],
    ['slug'=>'dzqcod-call-center', 'icon'=>'📞','label'=>'مركز الاتصال',   'group'=>'main',   'badge'=>'calls'],
    ['slug'=>'_sep','label'=>'العمليات'],
    ['slug'=>'dzqcod-shipping',    'icon'=>'🚚','label'=>'الشحن',          'group'=>'ops',    'req'=>['yalidine','zr-express','ecotrack']],
    ['slug'=>'dzqcod-printing',    'icon'=>'🖨️','label'=>'الطباعة',        'group'=>'ops'],
    ['slug'=>'_sep','label'=>'التسويق'],
    ['slug'=>'dzqcod-campaigns',   'icon'=>'📈','label'=>'الحملات',        'group'=>'mktg',   'req'=>['utm-tracking','meta-capi','tiktok-events','google-ads']],
    ['slug'=>'dzqcod-offers',      'icon'=>'🎁','label'=>'العروض',         'group'=>'mktg'],
    ['slug'=>'dzqcod-sales-boost', 'icon'=>'🛍️','label'=>'زيادة المبيعات', 'group'=>'mktg',   'req'=>['upsell','order-bump','abandoned-cart','loyalty','referral']],
    ['slug'=>'dzqcod-post-order',  'icon'=>'✅','label'=>'صفحات التأكيد',  'group'=>'mktg'],
    ['slug'=>'_sep','label'=>'التحليلات'],
    ['slug'=>'dzqcod-profit',      'icon'=>'💹','label'=>'حاسبة الأرباح',  'group'=>'analytics','req'=>['profit-calc','analytics']],
    ['slug'=>'dzqcod-smart-ops',   'icon'=>'🧠','label'=>'الأتمتة الذكية', 'group'=>'analytics','req'=>['smart-confirm','auto-ship','followup','workflows','abandoned-cart']],
    ['slug'=>'dzqcod-fraud',       'icon'=>'🛡️','label'=>'مكافحة الاحتيال','group'=>'analytics','req'=>['fraud-detect','ip-geo']],
    ['slug'=>'_sep','label'=>'النظام'],
    ['slug'=>'dzqcod-integrations','icon'=>'🔗','label'=>'التكامل',        'group'=>'sys',    'req'=>['google-sheets','zapier-make','tracking-page','reviews','email-marketing','crm']],
    ['slug'=>'dzqcod-addons',      'icon'=>'🧩','label'=>'الإضافات',       'group'=>'sys'],
    ['slug'=>'dzqcod-form-design', 'icon'=>'🎨','label'=>'تصميم النموذج',  'group'=>'sys'],
    ['slug'=>'dzqcod-settings',    'icon'=>'⚙️','label'=>'الإعدادات',      'group'=>'sys'],
    ['slug'=>'dzqcod-support',     'icon'=>'🎧','label'=>'الدعم الفني',    'group'=>'sys'],
    ['slug'=>'dzqcod-page-generator','icon'=>'📄','label'=>'مولّد الصفحات','group'=>'sys'],
];

// Dynamic addon pages
$_inst_nav = \DZQCOD\Modules::installed_addons();
$_all_nav = \DZQCOD\Modules::get_addons();
$_addon_pages = [];
foreach($_all_nav as $_a){
    $_s = $_a['slug'] ?? '';
    if(!$_s || empty($_inst_nav[$_s])) continue;
    if(has_action('dzqcod_admin_page_'.$_s)){
        $_addon_pages[] = ['slug'=>'dzqcod-addon-'.$_s, 'icon'=>$_a['icon']??'🧩', 'label'=>$_a['name']??$_s, 'group'=>'sys'];
    }
}
if(!empty($_addon_pages)){
    // Insert before settings
    $menu = array_merge(
        array_slice($menu, 0, -2),
        [['slug'=>'_sep','label'=>'الإضافات المثبتة']],
        $_addon_pages,
        array_slice($menu, -2)
    );
}

$badges = [];
try { $badges['orders'] = (int) wc_orders_count('wc-pending-call'); $badges['calls'] = $badges['orders']; }
catch (\Exception $e) { $badges = ['orders'=>0,'calls'=>0]; }
$_inst = \DZQCOD\Modules::installed_addons();
$_any = function($slugs) use ($_inst){ foreach($slugs as $s) if(!empty($_inst[$s])) return true; return false; };
?>
<div class="dza-layout">
<!-- ═══ SIDEBAR ═══ -->
<nav class="dza-side" id="dza-side">
    <div class="dza-side-head">
        <span class="dza-side-logo">⚡ DZQCOD</span>
        <span class="dza-side-ver">v<?php echo esc_html(DZQCOD_VER); ?></span>
    </div>
    <div class="dza-side-menu">
        <?php foreach ($menu as $m):
            if ($m['slug'] === '_sep'): ?>
                <div class="dza-side-sep"><?php echo esc_html($m['label']); ?></div>
            <?php else:
                $active = ($pg === $m['slug']);
                $bc = isset($m['badge']) ? ($badges[$m['badge']] ?? 0) : 0;
                $locked = !empty($m['req']) && !$_any($m['req']);
                if($locked) continue; // ← hide entirely if addon not active
            ?>
            <a href="<?php echo esc_url(admin_url("admin.php?page={$m['slug']}")); ?>"
               class="dza-side-link<?php echo esc_html($active?' active':''); ?>">
                <span class="dza-side-icon"><?php echo esc_html($m['icon']); ?></span>
                <span class="dza-side-text"><?php echo esc_html($m['label']); ?></span>
                <?php if ($bc > 0): ?><span class="dza-side-badge"><?php echo esc_html($bc); ?></span><?php endif; ?>
            </a>
            <?php endif;
        endforeach; ?>
    </div>
    <div class="dza-side-foot">
        <div class="dza-side-plan <?php echo esc_attr($plan); ?>">
            <?php echo esc_html(strtoupper($plan)); ?>
        </div>
        <div style="text-align:center;padding:8px 12px;font-size:10px;color:#94a3b8;line-height:1.6">
            <div style="font-weight:700;color:#64748b">⚡ NIBRA TECHNOLOGY</div>
            <a href="https://productes.nibra.cloud/product/dzqcod" target="_blank" style="color:#6366f1;text-decoration:none;font-weight:600">🎧 الدعم الفني</a>
        </div>
    </div>
</nav>
<!-- ═══ MAIN CONTENT ═══ -->
<div class="dza-main" id="dza-main">
<button class="dza-menu-btn" id="dza-menu-btn" onclick="document.getElementById('dza-side').classList.toggle('open')">☰ القائمة</button>
