<?php
$rows = $rows ?? [];
$zoneLabel = trim((string) ($zoneLabel ?? '전체'));
$printedAt = trim((string) ($printedAt ?? date('Y.m.d')));
$chunks = array_chunk($rows, 12);
$totalPages = count($chunks);
?>
<!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>지정판매소 바코드</title>
  <style>
    body { margin: 0; font-family: Arial, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif; color: #222; background: #fff; }
    .page { width: 210mm; min-height: 297mm; margin: 0 auto; padding: 14mm 12mm 12mm; box-sizing: border-box; }
    .title { text-align: center; font-size: 42px; letter-spacing: 1px; font-weight: 500; margin: 0 0 14px; }
    .meta { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #555; padding-bottom: 4px; font-size: 13px; margin-bottom: 8px; }
    .meta .center { font-weight: 700; }
    .cards { display: flex; flex-wrap: wrap; align-content: flex-start; }
    .card { width: 33.3333%; padding: 0 8px 12px; box-sizing: border-box; }
    .barcode-wrap { min-height: 40px; }
    .barcode-svg { width: 100%; max-width: 270px; height: 22px; }
    .code-text { text-align: center; margin-top: 1px; font-size: 16px; letter-spacing: 0.35px; }
    .name-text { text-align: center; margin-top: 5px; font-size: 14px; line-height: 1.2; word-break: keep-all; }
    @media print {
      @page { size: A4; margin: 0; }
      .page { page-break-after: always; }
      .page:last-child { page-break-after: auto; }
    }
  </style>
</head>
<body>
<?= view('components/print_header', [
    'printTitle' => '지정판매소 바코드',
    'printExtraLines' => [
        '구역: ' . $zoneLabel,
        '출력일: ' . $printedAt,
    ],
]) ?>
<?php if ($rows === []): ?>
  <div class="page">
    <h1 class="title">지정판매소 바코드</h1>
    <p style="text-align:center; margin-top:30px; color:#666;">출력할 지정판매소가 없습니다.</p>
  </div>
<?php else: ?>
  <?php foreach ($chunks as $pageIndex => $pageRows): ?>
  <section class="page">
    <h1 class="title">지정판매소 바코드</h1>
    <div class="meta">
      <span>출 력 일 자: <?= esc($printedAt) ?></span>
      <span class="center"><?= esc($zoneLabel) ?></span>
      <span>페&nbsp;&nbsp;이&nbsp;&nbsp;지: <?= (int) ($pageIndex + 1) ?> / <?= (int) $totalPages ?></span>
    </div>
    <div class="cards">
      <?php foreach ($pageRows as $row): ?>
      <?php
        $code = trim((string) ($row->ds_shop_no ?? ''));
        $nm   = trim((string) ($row->ds_name ?? ''));
        $rep  = trim((string) ($row->ds_rep_name ?? ''));
        $label = trim($nm . ($rep !== '' ? ('-' . $rep) : ''));
      ?>
      <div class="card">
        <div class="barcode-wrap">
          <svg class="barcode-svg" data-barcode="<?= esc($code, 'attr') ?>"></svg>
        </div>
        <div class="code-text"><?= esc($code) ?></div>
        <div class="name-text"><?= esc($label) ?></div>
      </div>
      <?php endforeach; ?>
    </div>
  </section>
  <?php endforeach; ?>
<?php endif; ?>

<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.6/dist/JsBarcode.all.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
  var svgs = document.querySelectorAll('svg[data-barcode]');
  svgs.forEach(function (svg) {
    var code = (svg.getAttribute('data-barcode') || '').trim();
    if (!code) return;
    try {
      JsBarcode(svg, code, {
        format: 'CODE128',
        displayValue: false,
        margin: 0,
        height: 16,
        width: 1.28
      });
    } catch (e) {
      svg.outerHTML = '<div style="font-size:12px;color:#b91c1c;">바코드 생성 실패: ' + code + '</div>';
    }
  });
  if (window.location.search.indexOf('autoprint=1') >= 0) {
    setTimeout(function () { window.print(); }, 200);
  }
});
</script>
</body>
</html>
