<?php
declare(strict_types=1);

function render_header(string $page, string $title, string $subtitle = ''): void
{
    $current = user();
    $flash = take_flash();
    $unreadNotifications = (int)scalar('SELECT COUNT(*) FROM notifications WHERE company_id=? AND user_id=? AND read_at IS NULL',[(int)$current['company_id'],(int)$current['id']]);
    $nav = [
        ['dashboard','الرئيسية','⌂'], ['projects','المشاريع','▣'], ['budgets','الموازنات والتكاليف','▨'], ['journals','القيود اليومية','▤'],
        ['procurement','طلبات الشراء','▧'], ['quotations','عروض الأسعار','▦'], ['purchases','المشتريات','◫'], ['inventory','المخزون','◇'], ['certificates','المستخلصات','▧'],
        ['invoices','فواتير العملاء','▦'], ['customer_receipts','تحصيل العملاء','◎'], ['payables','مطابقة الموردين','≋'], ['supplier_payments','سداد الموردين','◉'], ['treasury','الخزينة','▥'], ['banking','البنوك والشيكات','▤'],
        ['project_controls','المناقصات والتغييرات','⌁'], ['workforce','الموظفون والرواتب','♙'], ['assets','الأصول','▣'], ['equipment','تشغيل المعدات','♜'], ['compliance','الإقفال والضريبة','✓'], ['einvoicing','الفاتورة الإلكترونية','▦'], ['analytics','التحليلات المالية','⌁'], ['reports','التقارير','▥'], ['statements','القوائم المالية','▤'], ['documents','المرفقات','▧'], ['notifications','التنبيهات','◌'], ['print','الطباعة','▱'], ['security','الأمان والتدقيق','⚿'], ['settings','الإعدادات','⚙'],
    ];
    ?><!doctype html><html lang="ar" dir="rtl"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title><?= e($title) ?> | ركائز</title><link rel="stylesheet" href="<?= e(app_url('assets/style.css')) ?>"></head><body class="app-body">
    <div class="app-shell"><aside class="sidebar" id="sidebar"><div class="brand"><span class="brand-logo">ر</span><div><strong>ركائز</strong><small>للإدارة المالية</small></div><button class="mobile-close" type="button" data-close-menu>×</button></div><nav>
    <?php $routes=['dashboard'=>'dashboard.php','projects'=>'projects.php','budgets'=>'budgets.php','journals'=>'journals.php','procurement'=>'procurement.php','quotations'=>'quotations.php','purchases'=>'purchases.php','inventory'=>'inventory.php','certificates'=>'certificates.php','invoices'=>'invoices.php','customer_receipts'=>'customer_receipts.php','payables'=>'payables.php','supplier_payments'=>'supplier_payments.php','treasury'=>'treasury.php','banking'=>'banking.php','project_controls'=>'project_controls.php','workforce'=>'workforce.php','assets'=>'assets.php','equipment'=>'equipment.php','compliance'=>'compliance.php','einvoicing'=>'einvoicing.php','analytics'=>'analytics.php','reports'=>'reports.php','statements'=>'statements.php','documents'=>'documents.php','notifications'=>'notifications.php','print'=>'print.php','security'=>'security.php','settings'=>'index.php?page=settings'];foreach ($nav as [$key,$label,$icon]): $href=$routes[$key]??'dashboard.php'; ?>
      <a class="nav-link <?= active_page($key, $page) ?>" href="<?= e(app_url($href)) ?>"><i><?= e($icon) ?></i><span><?= e($label) ?></span><?php if ($key === 'notifications' && $unreadNotifications): ?><b><?= $unreadNotifications>99?'99+':$unreadNotifications ?></b><?php endif; ?></a>
    <?php endforeach; ?></nav><div class="system-status"><span></span><div><strong>النظام متصل</strong><small>قاعدة MySQL نشطة</small></div></div></aside>
    <button class="menu-overlay" data-close-menu aria-label="إغلاق القائمة"></button>
    <div class="workspace"><header class="topbar"><div class="company-info"><button class="menu-btn" type="button" data-open-menu>☰</button><div><strong><?= e((string)$current['company_name']) ?></strong><small><?= e((string)($current['branch_name'] ?: 'الفرع الرئيسي')) ?></small></div></div><form class="top-search" action="<?= e(app_url('search.php')) ?>" method="get"><span>⌕</span><input type="search" name="q" minlength="2" placeholder="ابحث في النظام..."></form><div class="profile"><a class="notification-link" href="<?= e(app_url('notifications.php')) ?>" title="التنبيهات">◌<?php if($unreadNotifications):?><b><?= $unreadNotifications>99?'99+':$unreadNotifications ?></b><?php endif;?></a><span class="avatar"><?= e(initial((string)$current['full_name'])) ?></span><div><strong><?= e((string)$current['full_name']) ?></strong><small><?= e((string)($current['role_name'] ?: 'مستخدم')) ?></small></div><a href="<?= e(app_url('logout.php')) ?>" title="تسجيل الخروج">↪</a></div></header>
    <main class="main-content"><header class="page-heading"><div><p class="eyebrow">نظام الإدارة المالية للمقاولات</p><h1><?= e($title) ?></h1><?php if ($subtitle): ?><p><?= e($subtitle) ?></p><?php endif; ?></div></header>
    <?php if ($flash): ?><div class="alert <?= e($flash['type']) ?>"><?= e($flash['message']) ?></div><?php endif; ?>
    <?php
}

function render_footer(): void
{
    ?></main><footer class="app-footer">نظام ركائز المالي — الإصدار 3.0.0</footer></div></div><script src="<?= e(app_url('assets/app.js')) ?>"></script></body></html><?php
}
