<?php
$bal = (float)$opening;
$totalIn = 0.0;
$totalOut = 0.0;
?>

<section class="pi-hero">
  <div class="pi-title-wrap">
    <h1 class="pi-title">حركة صنف</h1>
    <div class="pi-title-en">ITEM MOVEMENT</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($item_name) ?></div>
    <div class="pi-party-meta-grid">
      <div><span>باركود</span><strong><?= e($item_barcode ? (string)$item_barcode : '-') ?></strong></div>
      <div><span>المخزن</span><strong><?= e($warehouse_name) ?></strong></div>
      <div><span>الفترة</span><strong><?= e($from) ?> إلى <?= e($to) ?></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($opening)) ?></strong></div>
      <div class="pi-stat-box is-accent"><span>عدد الحركات</span><strong><?= e((string)count($moves ?? [])) ?></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 style="width:120px">التاريخ</th>
          <th style="width:140px">المرجع</th>
          <th class="col-qty">وارد</th>
          <th class="col-qty">منصرف</th>
          <th class="col-qty">الرصيد</th>
          <th>بواسطة</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($moves ?? []) as $m): ?>
          <?php
            $in = (float)($m['qty_in'] ?? 0);
            $out = (float)($m['qty_out'] ?? 0);
            $totalIn += $in;
            $totalOut += $out;
            $bal += ($in - $out);
            $ref = strtoupper((string)($m['ref_type'] ?? '')) . '#' . (string)($m['ref_id'] ?? '');
          ?>
          <tr>
            <td class="text-center"><?= e(substr((string)($m['created_at'] ?? ''), 0, 19)) ?></td>
            <td class="text-center"><?= e($ref) ?></td>
            <td class="text-center"><?= e($in ? format_qty($in) : '-') ?></td>
            <td class="text-center"><?= e($out ? format_qty($out) : '-') ?></td>
            <td class="text-center is-strong"><?= e(format_qty($bal)) ?></td>
            <td class="text-center"><?= e((string)($m['username'] ?? '-')) ?></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($moves)): ?><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($totalIn)) ?></td></tr>
      <tr><td>إجمالي منصرف</td><td><?= e(format_qty($totalOut)) ?></td></tr>
      <tr class="is-grand"><td>الرصيد الختامي</td><td><?= e(format_qty($bal)) ?></td></tr>
    </table>
  </div>
</section>
