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

<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 compact">
    <tbody>
      <tr>
        <td>عدد الفواتير</td><td class="text-end"><?= e((string)($summary['count'] ?? 0)) ?></td>
        <td>قبل الخصم</td><td class="text-end"><?= e(format_money($summary['subtotal'] ?? 0)) ?></td>
      </tr>
      <tr>
        <td>الخصم</td><td class="text-end"><?= e(format_money($summary['discount'] ?? 0)) ?></td>
        <td>الصافي بعد الخصم</td><td class="text-end"><?= e(format_money($summary['net'] ?? 0)) ?></td>
      </tr>
      <tr>
        <td>VAT</td><td class="text-end"><?= e(format_money($summary['vat'] ?? 0)) ?></td>
        <td>ص/ت</td><td class="text-end"><?= e(format_money($summary['wht'] ?? 0)) ?></td>
      </tr>
      <tr>
        <td colspan="2"></td>
        <td class="fw-bold">الإجمالي النهائي</td><td class="text-end fw-bold"><?= e(format_money($summary['total'] ?? 0)) ?></td>
      </tr>
    </tbody>
  </table>
</div>

<div class="box">
  <div class="box-title">تفاصيل الفواتير</div>
  <table class="table">
    <thead>
      <tr>
        <th>رقم</th>
        <th>التاريخ</th>
        <th>المورد</th>
        <th>المخزن</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="9" class="text-center muted">لا يوجد بيانات</td></tr>
      <?php else: foreach ($rows as $r): ?>
        <tr>
          <td><?= e($r['inv_no']) ?></td>
          <td><?= e(substr((string)$r['inv_date'],0,10)) ?></td>
          <td><?= e($r['supplier_name'] ?? '') ?></td>
          <td><?= e($r['warehouse_name'] ?? '') ?></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>
  </table>
</div>
