<?php
$rows = $rows ?? [];
$warehouse_name = (string)($warehouse_name ?? 'المخزن');
$userName = (string)($userName ?? '');
$reportDate = (string)($reportDate ?? date('Y-m-d'));
$total_cost_value = (float)($total_cost_value ?? 0);
$only_nonzero = (int)($only_nonzero ?? 0);
$totalQty = 0.0;
foreach ($rows as $r) { $totalQty += (float)($r['qty'] ?? 0); }

// Long stock reports need controlled pagination. The first page has title/cards,
// so it intentionally carries fewer rows; continuation pages must stay below
// the real printable height after adding the compact page header.
$firstPageRows = 16;
$nextPageRows = 20;
$chunks = [];
$remainingRows = array_values($rows);
$chunks[] = array_splice($remainingRows, 0, $firstPageRows);
while ($remainingRows) {
    $chunks[] = array_splice($remainingRows, 0, $nextPageRows);
}
if (!$chunks) { $chunks = [[]]; }
$totalPages = count($chunks);
?>

<?php foreach ($chunks as $pageIndex => $pageRows): ?>
  <?php if ($pageIndex > 0): ?><div class="print-page-break inventory-report-break"></div><?php endif; ?>
  <section class="inventory-report-page<?= $pageIndex === 0 ? ' is-first-page' : ' is-continuation-page' ?>">
    <?php if ($pageIndex === 0): ?>
      <section class="pi-hero inventory-report-hero">
        <div class="pi-title-wrap">
          <h1 class="pi-title">تقرير المخزون</h1>
          <div class="pi-title-en">STOCK REPORT</div>
        </div>
      </section>

      <section class="pi-party-summary-grid inventory-report-summary">
        <div class="pi-party-card">
          <div class="pi-card-head">بيانات التقرير</div>
          <div class="pi-party-name"><?= e($warehouse_name) ?></div>
          <div class="pi-party-meta-grid">
            <div><span>تاريخ التقرير</span><strong><?= e($reportDate) ?></strong></div>
            <div><span>بواسطة</span><strong><?= e($userName !== '' ? $userName : '-') ?></strong></div>
            <div><span>وضع الطباعة</span><strong><?= $only_nonzero === 1 ? 'إخفاء الأصناف صفر' : 'كل الأصناف' ?></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)) ?></strong></div>
          </div>
        </div>
      </section>
    <?php else: ?>
      <section class="inventory-continuation-head">
        <div>
          <strong>تقرير المخزون</strong>
          <span><?= e($warehouse_name) ?></span>
        </div>
        <div>
          <span>تاريخ التقرير</span>
          <strong><?= e($reportDate) ?></strong>
        </div>
        <div>
          <span>صفحة</span>
          <strong><?= (int)($pageIndex + 1) ?> / <?= (int)$totalPages ?></strong>
        </div>
      </section>
    <?php endif; ?>

    <section class="pi-table-section inventory-report-table-section">
      <div class="pi-section-title">أرصدة الأصناف - صفحة <?= (int)($pageIndex + 1) ?> / <?= (int)$totalPages ?></div>
      <div class="pi-table-wrap inventory-report-table-wrap">
        <table class="pi-table inventory-print-table inventory-stock-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 foreach ($pageRows as $localIndex => $r): $rowNo = ($pageIndex === 0 ? 0 : $firstPageRows + (($pageIndex - 1) * $nextPageRows)) + $localIndex + 1; ?>
              <tr>
                <td class="text-center"><?= $rowNo ?></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 (!$pageRows): ?><tr><td colspan="6" class="text-center">لا توجد بيانات</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </section>
  </section>
<?php endforeach; ?>

<section class="pi-bottom-grid keep-together inventory-report-total-section">
  <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)) ?></td></tr>
    </table>
  </div>
</section>
