<?php
$totalItems = count($lines ?? []);
$totalQty = 0.0;
$totalValue = 0.0;
foreach (($lines ?? []) as $ln) {
  $qty = (float)($ln['qty'] ?? 0);
  $cost = (float)($ln['cost'] ?? 0);
  $totalQty += $qty;
  $totalValue += ($qty * $cost);
}
$docNo = trim((string)($doc['doc_no'] ?? ''));
$docDate = trim((string)substr((string)($doc['doc_date'] ?? ''), 0, 10));
$status = trim((string)($doc['status'] ?? ''));
$notesText = trim((string)($notes ?? ''));
?>

<?php if (($print_format ?? 'a4') === 'receipt'): ?>
<div class='grid2'>
  <div class='box'><div class='field-title'>المخزن</div><div class='field-value'><?= e((string)($warehouse_name ?? '-')) ?></div></div>
  <div class='box'><div class='field-title'>ملاحظات</div><div class='field-value'><?= e($notesText !== '' ? $notesText : '-') ?></div></div>
</div>
<table class='table'><thead><tr><th style='width:44px'>#</th><th>الصنف</th><th style='width:100px'>الوحدة</th><th style='width:110px'>الكمية</th><th style='width:110px'>التكلفة</th><th style='width:120px'>الإجمالي</th></tr></thead><tbody>
<?php $i=0; foreach (($lines ?? []) as $ln): $i++; $lineTotal=((float)($ln['qty'] ?? 0))*((float)($ln['cost'] ?? 0)); ?>
<tr><td><?= $i ?></td><td><?= e((string)($ln['item_name'] ?? '')) ?></td><td><?= e((string)($ln['unit_name'] ?? '-')) ?></td><td><?= e(format_qty($ln['qty'] ?? 0)) ?></td><td><?= e(format_money($ln['cost'] ?? 0)) ?></td><td><?= e(format_money($lineTotal)) ?></td></tr>
<?php endforeach; ?></tbody></table>
<div class='pieces'>عدد الأصناف: <?= e((string)$totalItems) ?> | إجمالي الكمية: <?= e(format_qty($totalQty)) ?> | إجمالي القيمة: <?= e(format_money($totalValue)) ?></div>
<?php return; endif; ?>

<section class="pi-hero">
  <div class="pi-title-wrap">
    <h1 class="pi-title">افتتاحي مخزون</h1>
    <div class="pi-title-en">OPENING STOCK</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($docNo !== '' ? $docNo : 'افتتاحي مخزون') ?></div>
    <div class="pi-party-meta-grid">
      <div><span>المخزن</span><strong><?= e((string)($warehouse_name ?? '-')) ?></strong></div>
      <div><span>التاريخ</span><strong><?= e($docDate !== '' ? $docDate : '-') ?></strong></div>
      <div><span>الحالة</span><strong><?= e($status !== '' ? $status : '-') ?></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($totalValue)) ?></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 class="col-unit">الوحدة</th><th class="col-qty">الكمية</th><th class="col-price">التكلفة</th><th class="col-total">الإجمالي</th></tr></thead>
      <tbody>
      <?php $i=0; foreach (($lines ?? []) as $ln): $i++; $lineTotal=((float)($ln['qty'] ?? 0))*((float)($ln['cost'] ?? 0)); ?>
        <tr>
          <td class="text-center"><?= $i ?></td>
          <td><div class="pi-item-name"><?= e((string)($ln['item_name'] ?? '')) ?></div></td>
          <td class="text-center"><?= e((string)($ln['unit_name'] ?? '-')) ?></td>
          <td class="text-center"><?= e(format_qty($ln['qty'] ?? 0)) ?></td>
          <td class="text-left"><?= e(format_money($ln['cost'] ?? 0)) ?></td>
          <td class="text-left is-strong"><?= e(format_money($lineTotal)) ?></td>
        </tr>
      <?php endforeach; ?>
      <?php if (!$lines): ?><tr><td colspan="6" class="text-center">لا توجد أصناف</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</section>

<section class="pi-bottom-grid keep-together">
  <div class="pi-terms-card">
    <div class="pi-card-head">ملاحظات</div>
    <div class="pi-notes-box"><?= e($notesText !== '' ? $notesText : 'لا توجد ملاحظات إضافية.') ?></div>
  </div>
  <div class="pi-total-card">
    <div class="pi-card-head">الملخص المالي</div>
    <table class="pi-total-table">
      <tr><td>عدد الأصناف</td><td><?= e((string)$totalItems) ?></td></tr>
      <tr><td>إجمالي الكمية</td><td><?= e(format_qty($totalQty)) ?></td></tr>
      <tr class="is-grand"><td>إجمالي القيمة</td><td><?= e(format_money($totalValue)) ?></td></tr>
    </table>
  </div>
</section>
