<?php
// app/layout.php
require_once __DIR__ . '/helpers.php';
$__pdo = db();
\App\Core\AppBootstrap::bootLayout($__pdo, !empty($_SESSION['user']));

$config = require __DIR__ . '/config.php';
$flash = flash_get();
?>
<!doctype html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= e($config['app']['name']) ?></title>
  <link href="<?= e(asset('assets/vendor/bootstrap/css/bootstrap.min.css')) ?>" rel="stylesheet">
  <link rel="stylesheet" href="<?= e(asset('assets/vendor/fontawesome/css/all.min.css')) ?>" />
  <link rel="stylesheet" href="<?= e(asset('assets/vendor/fonts/cairo/cairo.css')) ?>">
  <style>
    body { background:#f7f7fb; }
    .navbar-brand { font-weight:700; }
    .card { border:0; border-radius: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.06); }
    .btn { border-radius: 12px; }
    .form-control, .form-select { border-radius: 12px; }
    .table { background:#fff; border-radius: 14px; overflow:hidden; }
    .badge-soft { background: rgba(13,110,253,.1); color:#0d6efd; }

    /* Minimal AdminLTE-like "small-box" styles used across dashboards */
    .small-box { position: relative; display:block; border-radius: 14px; padding: 16px; overflow:hidden; box-shadow: 0 6px 20px rgba(0,0,0,.06); }
    .small-box .inner h3 { font-weight: 800; margin: 0 0 6px 0; font-size: 1.6rem; }
    .small-box .inner p { margin: 0; opacity: .95; }
    .small-box .icon { position:absolute; left: 14px; top: 10px; font-size: 46px; opacity: .25; }
    .small-box-footer { display:block; margin-top: 12px; color: rgba(255,255,255,.9); text-decoration:none; font-weight: 700; }
    .small-box-footer:hover { text-decoration: underline; color:#fff; }
  </style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container">
    <a class="navbar-brand" href="<?= e(url('/admin/dashboard')) ?>"><?= e($config['app']['name']) ?></a>
    <div class="collapse navbar-collapse">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <?php if (!empty($_SESSION['user'])): ?>
          <li class="nav-item"><a class="nav-link" href="<?= e(url('/inventory/items')) ?>">الأصناف</a></li>
          <li class="nav-item"><a class="nav-link" href="<?= e(url('/inventory/opening-stock')) ?>">رصيد افتتاحي</a></li>
          <?php if (can('sales.invoices.view')): ?>
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">المبيعات</a>
              <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="<?= e(url('/sales')) ?>">لوحة المبيعات</a></li>
                <li><a class="dropdown-item" href="<?= e(url('/sales/invoices')) ?>">فواتير المبيعات</a></li>
                <li><a class="dropdown-item" href="<?= e(url('/sales/invoices/form')) ?>">إنشاء فاتورة</a></li>
                <?php if (can('sales.receipts.view') || can('sales.receipts.create')): ?>
                  <li><hr class="dropdown-divider"></li>
                  <li><a class="dropdown-item" href="<?= e(url('/sales/receipts')) ?>">سندات القبض</a></li>
                  <?php if (can('sales.receipts.create')): ?>
                    <li><a class="dropdown-item" href="<?= e(url('/sales/receipts/form')) ?>">إنشاء سند قبض</a></li>
                  <?php endif; ?>
                <?php endif; ?>
                <?php if (can('sales.statement.view')): ?>
                  <li><a class="dropdown-item" href="<?= e(url('/reports/parties/customers')) ?>">كشف حساب عميل</a></li>
                <?php endif; ?>
                <li><hr class="dropdown-divider"></li>
                <li><a class="dropdown-item" href="<?= e(url('/parties/customers')) ?>">العملاء</a></li>
                <li><a class="dropdown-item" href="<?= e(url('/pos')) ?>">نقطة البيع</a></li>
              </ul>
            </li>
          <?php endif; ?>
          <li class="nav-item"><a class="nav-link" href="<?= e(url('/reports/inventory/stock')) ?>">تقرير المخزون</a></li>
        <?php endif; ?>
      </ul>
      <ul class="navbar-nav">
        <?php if (!empty($_SESSION['user'])): ?>
          <li class="nav-item"><span class="navbar-text me-3">مرحباً، <?= e($_SESSION['user']['full_name'] ?? $_SESSION['user']['username']) ?></span></li>
          <li class="nav-item"><a class="nav-link" href="<?= e(url('/logout')) ?>">خروج</a></li>
        <?php else: ?>
          <li class="nav-item"><a class="nav-link" href="<?= e(url('/login')) ?>">تسجيل الدخول</a></li>
        <?php endif; ?>
      </ul>
    </div>
  </div>
</nav>

<div class="container my-4">
  <?php if ($flash): ?>
    <div class="alert alert-<?= e($flash['type']) ?>"><?= e($flash['message']) ?></div>
  <?php endif; ?>

  <?= $content ?? '' ?>
  <?php if (isset($page, $total_pages) && (int)$total_pages > 1): ?>
    <?= render_pagination((int)$page, (int)$total_pages, (int)($total_rows ?? 0), (int)($from_row ?? 0), (int)($to_row ?? 0)) ?>
  <?php endif; ?>
</div>

<script src="<?= e(asset('assets/vendor/bootstrap/js/bootstrap.bundle.min.js')) ?>"></script>
</body>
</html>
