<?php
// Expected: $title, $meta, $rows, $totals
?>

<div class="doc-title"><?= e($title ?? '') ?></div>

<?php if (!empty($meta)): ?>
  <div class="meta-grid">
    <?php foreach ($meta as $k=>$v): ?>
      <div class="meta-item"><span class="label"><?= e((string)$k) ?>:</span> <span class="value"><?= e((string)$v) ?></span></div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

<div class="box">
  <div class="box-title">النتائج</div>
  <div class="muted" style="margin-bottom:6px;">إجمالي الكمية: <strong><?= e(number_format((float)($totals['qty'] ?? 0), 3)) ?></strong> | إجمالي القيمة: <strong><?= e(format_money($totals['total'] ?? 0)) ?></strong></div>
  <table class="table">
    <thead>
      <tr>
        <th>الصنف</th>
        <th class="text-end">الكمية</th>
        <th class="text-end">متوسط السعر</th>
        <th class="text-end">إجمالي القيمة</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($rows)): ?>
        <tr><td colspan="4" class="text-center muted">لا يوجد بيانات</td></tr>
      <?php else: foreach ($rows as $r): ?>
        <tr>
          <td><?= e($r['item_name'] ?? '') ?></td>
          <td class="text-end"><?= e(number_format((float)($r['qty'] ?? 0), 3)) ?></td>
          <td class="text-end"><?= e(format_money($r['avg_price'] ?? 0)) ?></td>
          <td class="text-end fw-bold"><?= e(format_money($r['total'] ?? 0)) ?></td>
        </tr>
      <?php endforeach; endif; ?>
    </tbody>
  </table>
</div>
