<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cek Stok</title>
<style type="text/css">
  body { font-family: Arial, Helvetica, sans-serif; margin: 20px; color:#000; }
  h2, h3 { text-align:center; margin: 2px 0; }
  .info { text-align:center; font-size:12px; margin-bottom:12px; }
  .toolbar { text-align:center; margin-bottom:15px; }
  .toolbar button { padding:8px 16px; font-size:14px; cursor:pointer; }
  table { border-collapse: collapse; width:100%; font-size:13px; }
  th, td { border:1px solid #000; padding:5px 8px; vertical-align: top; }
  th { background:#d9d9d9; text-align:center; }
  td.c { text-align:center; }
  .hasil { height:22px; }

  /* ===== baris JUDUL GROUP per outlet (biru) ===== */
  tr.grp td {
     background:#2f6fb0; color:#fff; font-weight:bold; text-align:left;
     padding:7px 8px; border:1px solid #000; border-top:3px solid #000;
     font-size:13px;
  }
  tr.grp td .sub { float:right; font-weight:bold; }

  @media print {
    .toolbar { display:none !important; }
    body { margin:0; }

    /* PENTING: jangan paksa ukuran/orientasi kertas di sini.
       Biarkan ikut pilihan pada dialog Cetak (tegak / mendatar).
       Memaksa 'size' di CSS sementara dialog berbeda = penyebab halaman kosong. */
    @page { margin: 8mm; }

    /* warna baris judul outlet (biru) tetap muncul saat dicetak */
    tr.grp td { -webkit-print-color-adjust:exact; print-color-adjust:exact; }

    /* judul kolom (No, Kode Bill, ...) diulang otomatis di setiap halaman */
    thead { display: table-header-group; }

    /* satu baris data tidak boleh terbelah ke dua halaman */
    tbody tr { page-break-inside: avoid; break-inside: avoid; }

    /* baris judul outlet (biru) tidak boleh terpisah dari baris di bawahnya */
    tr.grp { page-break-inside: avoid; break-inside: avoid;
             page-break-after: avoid;  break-after: avoid; }

    /* tabel & isi tabel BEBAS dipindah halaman (biar tidak menyisakan ruang kosong) */
    table, tbody { page-break-inside: auto; break-inside: auto; }
  }
</style>
</head>
<body>

<div class="toolbar">
  <button type="button" onclick="window.print();">🖨 Cetak / Simpan PDF</button>
</div>

<h2>CEK STOK <?php echo strtoupper($outlet_title); ?></h2>
<h3>PERIODE <?php echo (strlen($date_1) ? $date_1 : '...').' s/d '.(strlen($date_2) ? $date_2 : '...'); ?></h3>
<div class="info">
  Dicetak: <?php echo $tanggal_cetak; ?> &nbsp;|&nbsp;
  Total Bill: <?php echo $total_bill; ?> &nbsp;|&nbsp;
  Total PCS: <?php echo $total_pcs; ?>
  <?php if (!empty($is_all_outlet)) { echo '&nbsp;|&nbsp; (di-group per outlet)'; } ?>
</div>

<table>
  <thead>
    <tr>
      <th width="40">No</th>
      <th width="150">KODE BILL</th>
      <th width="130">TGL MSK</th>
      <th>NAMA PELANGGAN</th>
      <th width="50">PCS</th>
      <th width="90">Posisi Stok</th>
      <th width="130">Hasil Cek</th>
    </tr>
  </thead>
  <tbody>
  <?php
  if (count($groups) == 0) {
      echo "<tr><td colspan='7' class='c'>Tidak ada data stok (semua sudah di-pickup, atau tidak ada bill pada periode ini).</td></tr>";
  }

  foreach ($groups as $g) {

      // ---- JUDUL GROUP outlet + subtotal (HANYA saat mode SEMUA OUTLET) ----
      if (!empty($is_all_outlet)) {
          echo "<tr class='grp'><td colspan='7'>";
          echo "<span class='sub'>Total Bill: ".$g['total_bill']." &nbsp;|&nbsp; Total PCS: ".$g['total_pcs']."</span>";
          echo "📍 OUTLET: ".htmlspecialchars((string)$g['outlet_name']);
          echo "</td></tr>";
      }

      // ---- baris bill dalam outlet ini (urut dari yang paling lama) ----
      $no = 0;
      foreach ($g['rows'] as $r) {
          $no++;
          $tgl = (strlen($r->date) && $r->date != '0000-00-00 00:00:00') ? date('d/m/Y H.i', strtotime($r->date)) : '';
          echo "<tr>";
          echo "<td class='c'>".$no."</td>";
          echo "<td>".htmlspecialchars((string)$r->code)."</td>";
          echo "<td class='c'>".$tgl."</td>";
          echo "<td>".htmlspecialchars((string)$r->customer_name)."</td>";
          echo "<td class='c'>".(int)$r->total_pcs."</td>";
          echo "<td class='c'>".htmlspecialchars((string)$r->stock_position)."</td>";
          echo "<td class='hasil'></td>";
          echo "</tr>";
      }
  }
  ?>
  </tbody>
</table>

<?php if (!empty($auto_print)) { ?>
<script type="text/javascript">
  window.addEventListener('load', function(){ setTimeout(function(){ window.print(); }, 400); });
</script>
<?php } ?>

</body>
</html>