<?php
$totalQty = 0.0;
foreach (($rows ?? []) as $r) { $totalQty += (float)($r['qty'] ?? 0); }
$supplierName = trim((string)($sup['name'] ?? ''));
$supplierCode = trim((string)($sup['code'] ?? ''));
$only_nonzero = (int)($only_nonzero ?? 0);
?>

<section class="pi-hero">
  <div class="pi-title-wrap">
    <h1 class="pi-title">تقرير مخزون الطرف</h1>
    <div class="pi-title-en">SUPPLIER STOCK REPORT</div>
  </div>
</section>

<section class="pi-party-summary-grid">
  <div class="pi-party-card">
    <div class="pi-card-head">بيانات الطرف</div>
    <div class="pi-party-name"><?= e($supplierName !== '' ? $supplierName : '-') ?></div>
    <div class="pi-party-meta-grid">
      <div><span>كود الطرف</span><strong><?= e($supplierCode !== '' ? $supplierCode : '-') ?></strong></div>
      <div><span>المخزن</span><strong><?= e((string)$warehouse_name) ?></strong></div>
      <div><span>تاريخ التقرير</span><strong><?= e((string)$reportDate) ?></strong></div>
    </div>
  </div>
  <div class="pi-summary-card">
    <div class="pi-card-head">ملخص المخزون</div>
    <div class="pi-summary-stats">
      <div class="pi-stat-box"><span>إجمالي الكمية</span><strong><?= e(format_qty($totalQty)) ?></strong></div>
      <div class="pi-stat-box is-accent"><span>إجمالي قيمة التكلفة</span><strong><?= e(format_money($total_cost_value ?? 0)) ?></strong></div>
    </div>
  </div>
</section>

<section class="pi-table-section">
  <div class="pi-section-title">أرصدة أصناف الطرف</div>
  <div class="pi-table-wrap">
    <table class="pi-table inventory-print-table">
      <thead>
        <tr>
          <th class="col-no">#</th>
          <th>الصنف / البيان</th>
          <th style="width:130px">باركود</th>
          <th class="col-price">التكلفة</th>
          <th class="col-price">السعر</th>
          <th class="col-qty">الرصيد</th>
        </tr>
      </thead>
      <tbody>
        <?php $i=0; foreach (($rows ?? []) as $r): $i++; ?>
          <tr>
            <td class="text-center"><?= $i ?></td>
            <td><div class="pi-item-name"><?= e((string)($r['name'] ?? '')) ?></div></td>
            <td class="text-center"><?= e((string)($r['barcode'] ?? '-')) ?></td>
            <td class="text-left"><?= e(format_money($r['cost'] ?? 0)) ?></td>
            <td class="text-left"><?= e(format_money($r['price'] ?? 0)) ?></td>
            <td class="text-center is-strong"><?= e(format_qty($r['qty'] ?? 0)) ?></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?><tr><td colspan="6" class="text-center">لا توجد بيانات</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</section>

<section class="pi-bottom-grid keep-together">
  <div></div>
  <div class="pi-total-card">
    <div class="pi-card-head">إجمالي التقرير</div>
    <table class="pi-total-table">
      <tr><td>إجمالي عدد القطع</td><td><?= e(format_qty($totalQty)) ?></td></tr>
      <tr><td>إجمالي عدد الأصناف</td><td><?= e((string)count($rows ?? [])) ?></td></tr>
      <tr class="is-grand"><td>إجمالي قيمة التكلفة</td><td><?= e(format_money($total_cost_value ?? 0)) ?></td></tr>
    </table>
  </div>
</section>
