<?php
// Warehouse document print body
// Vars: $doc, $lines, $fromWh, $toWh, $createdBy, $notes
$totalItems = count($lines ?? []);
$totalQty = 0.0;
foreach (($lines ?? []) as $ln) {
  $totalQty += (float)($ln['qty'] ?? 0);
}
$typeLabel = doc_type_label((string)($doc['doc_type'] ?? ''));
$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)($fromWh ?? '-')) ?></div></div>
  <?php if (!empty($toWh) && $toWh !== '-'): ?><div class='box'><div class='field-title'>إلى مخزن</div><div class='field-value'><?= e((string)$toWh) ?></div></div><?php endif; ?>
  <div class='box'><div class='field-title'>أنشئ بواسطة</div><div class='field-value'><?= e((string)($createdBy ?? '-')) ?></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:120px'>الكمية</th></tr></thead><tbody>
<?php $i=0; foreach (($lines ?? []) as $ln): $i++; ?>
<tr><td><?= $i ?></td><td><?= e((string)($ln['item_name'] ?? '')) ?></td><td><?= e((string)((($ln['unit_name'] ?? '') !== '') ? ($ln['unit_name'] . (!empty($ln['unit_symbol']) ? ' (' . $ln['unit_symbol'] . ')' : '')) : ((!empty($ln['unit_symbol'])) ? $ln['unit_symbol'] : '-'))) ?></td><td><?= e(format_qty($ln['qty'] ?? 0)) ?></td></tr>
<?php endforeach; ?></tbody></table>
<div class='pieces'>إجمالي عدد الأصناف: <?= e((string)$totalItems) ?> | إجمالي عدد القطع: <?= e(format_qty($totalQty)) ?></div>
<?php return; endif; ?>

<section class="pi-hero">
  <div class="pi-title-wrap">
    <h1 class="pi-title"><?= e('إذن مخزن - ' . $typeLabel) ?></h1>
    <div class="pi-title-en">WAREHOUSE DOCUMENT</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($typeLabel) ?></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((string)$totalItems) ?></strong></div>
      <div class="pi-stat-box is-accent"><span>إجمالي الكمية</span><strong><?= e(format_qty($totalQty)) ?></strong></div>
    </div>
  </div>
</section>

<section class="print-section">
  <div class="section-heading">بيانات الحركة</div>
  <div class="hero-grid">
    <div class="panel">
      <div class="panel-head">بيانات المخزن</div>
      <div class="panel-body detail-grid">
        <div class="detail-item"><div class="detail-label">من / المخزن</div><div class="detail-value"><?= e((string)($fromWh ?? '-')) ?></div></div>
        <div class="detail-item"><div class="detail-label">إلى مخزن</div><div class="detail-value"><?= e((!empty($toWh) && $toWh !== '-') ? (string)$toWh : '-') ?></div></div>
        <div class="detail-item"><div class="detail-label">أنشئ بواسطة</div><div class="detail-value"><?= e((string)($createdBy ?? '-')) ?></div></div>
        <div class="detail-item"><div class="detail-label">ملاحظات</div><div class="detail-value"><?= e($notesText !== '' ? $notesText : '-') ?></div></div>
      </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></tr></thead>
      <tbody>
      <?php $i=0; foreach (($lines ?? []) as $ln): $i++; ?>
        <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'] ?? '') !== '') ? ($ln['unit_name'] . (!empty($ln['unit_symbol']) ? ' (' . $ln['unit_symbol'] . ')' : '')) : ((!empty($ln['unit_symbol'])) ? $ln['unit_symbol'] : '-'))) ?></td>
          <td class="text-center is-strong"><?= e(format_qty($ln['qty'] ?? 0)) ?></td>
        </tr>
      <?php endforeach; ?>
      <?php if (!$lines): ?><tr><td colspan="4" class="text-center">لا توجد أصناف</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</section>
