<?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>
  <table class="table">
    <thead>
      <tr>
        <th>المورد</th>
        <th class="text-center">عدد الفواتير</th>
        <th class="text-end">قبل الخصم</th>
        <th class="text-end">الخصم</th>
        <th class="text-end">VAT</th>
        <th class="text-end">ص/ت</th>
        <th class="text-end">الإجمالي</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($rows)): ?>
        <tr><td colspan="7" class="text-center muted">لا يوجد بيانات</td></tr>
      <?php else: foreach ($rows as $r): ?>
        <tr>
          <td><?= e($r['supplier_name'] ?? '') ?></td>
          <td class="text-center"><?= e((string)($r['inv_count'] ?? 0)) ?></td>
          <td class="text-end"><?= e(format_money($r['subtotal'] ?? 0)) ?></td>
          <td class="text-end"><?= e(format_money($r['discount_amount'] ?? 0)) ?></td>
          <td class="text-end"><?= e(format_money($r['tax_amount'] ?? 0)) ?></td>
          <td class="text-end"><?= e(format_money($r['wht_amount'] ?? 0)) ?></td>
          <td class="text-end fw-bold"><?= e(format_money($r['total'] ?? 0)) ?></td>
        </tr>
      <?php endforeach; endif; ?>
    </tbody>
    <tfoot>
      <tr>
        <th>الإجمالي</th>
        <th class="text-center"><?= e((string)($totals['inv_count'] ?? 0)) ?></th>
        <th class="text-end"><?= e(format_money($totals['subtotal'] ?? 0)) ?></th>
        <th class="text-end"><?= e(format_money($totals['discount_amount'] ?? 0)) ?></th>
        <th class="text-end"><?= e(format_money($totals['tax_amount'] ?? 0)) ?></th>
        <th class="text-end"><?= e(format_money($totals['wht_amount'] ?? 0)) ?></th>
        <th class="text-end fw-bold"><?= e(format_money($totals['total'] ?? 0)) ?></th>
      </tr>
    </tfoot>
  </table>
</div>
