<?php
declare(strict_types=1);

if (!function_exists('render_header')) {
    function render_header(string $title='Dashboard'): void {
        global $me, $settings, $page;
        $shopName = $settings['shop_name_ar'] ?? ($_ENV['APP_NAME'] ?? 'Fakhr System');
        $logoPath = $settings['logo_path'] ?? 'assets/img/default-logo.png';
        $fullName = $me['full_name'] ?? 'Guest';
        ?>
        <!doctype html><html lang="ar" dir="rtl"><head>
        <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?= function_exists('e') ? e($title) : htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?> - <?= function_exists('e') ? e($shopName) : htmlspecialchars($shopName, ENT_QUOTES, 'UTF-8') ?></title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
        <link href="<?= function_exists('e') && function_exists('base_url') ? e(base_url('assets/style.css')) : 'assets/style.css' ?>" rel="stylesheet"></head>
        <body>
        <nav class="navbar navbar-expand-lg navbar-dark sticky-top"><div class="container-fluid">
          <a class="navbar-brand logo-box" href="<?= function_exists('e') && function_exists('base_url') ? e(base_url('?page=dashboard')) : '?' ?>"><img src="<?= function_exists('e') ? e($logoPath) : htmlspecialchars($logoPath, ENT_QUOTES, 'UTF-8') ?>"><span><?= function_exists('e') ? e($shopName) : htmlspecialchars($shopName, ENT_QUOTES, 'UTF-8') ?></span></a>
          <div class="d-flex gap-2 align-items-center text-white"><span class="small"><?= function_exists('e') ? e($fullName) : htmlspecialchars($fullName, ENT_QUOTES, 'UTF-8') ?></span><?php if (function_exists('base_url')): ?><a class="btn btn-sm btn-outline-light" href="<?= e(base_url('?page=logout')) ?>">خروج</a><?php endif; ?></div>
        </div></nav>
        <div class="container-fluid"><div class="row">
          <aside class="col-lg-2 d-none d-lg-block sidebar p-3">
            <div class="nav flex-column">
              <?php if (function_exists('base_url') && function_exists('e')): ?>
              <a class="nav-link <?= ($page ?? '')==='dashboard'?'active':'' ?>" href="<?= e(base_url('?page=dashboard')) ?>">لوحة التحكم</a>
              <a class="nav-link <?= ($page ?? '')==='invoices'?'active':'' ?>" href="<?= e(base_url('?page=invoices')) ?>">الرسبشن والفواتير</a>
              <a class="nav-link <?= ($page ?? '')==='items'?'active':'' ?>" href="<?= e(base_url('?page=items')) ?>">القطع والفئات</a>
              <a class="nav-link <?= ($page ?? '')==='returns'?'active':'' ?>" href="<?= e(base_url('?page=returns')) ?>">الإرجاع</a>
              <a class="nav-link <?= ($page ?? '')==='customers'?'active':'' ?>" href="<?= e(base_url('?page=customers')) ?>">العملاء</a>
              <a class="nav-link <?= ($page ?? '')==='reports'?'active':'' ?>" href="<?= e(base_url('?page=reports')) ?>">التقارير</a>
              <a class="nav-link <?= ($page ?? '')==='audit'?'active':'' ?>" href="<?= e(base_url('?page=audit')) ?>">سجل الحركات</a>
              <a class="nav-link <?= ($page ?? '')==='settings'?'active':'' ?>" href="<?= e(base_url('?page=settings')) ?>">الإعدادات</a>
              <a class="nav-link" target="_blank" href="<?= e(base_url('?page=public_screen')) ?>">شاشة الانتظار</a>
              <?php endif; ?>
            </div>
          </aside>
          <main class="col-lg-10 p-4">
            <?php if(function_exists('flash') && ($msg=flash('success'))): ?><div class="alert alert-success"><?= e($msg) ?></div><?php endif; ?>
            <?php if(function_exists('flash') && ($msg=flash('error'))): ?><div class="alert alert-danger"><?= e($msg) ?></div><?php endif; ?>
        <?php
    }
}

if (!function_exists('render_footer')) {
    function render_footer(): void {
        echo "</main></div></div></body></html>";
    }
}
