<?= view('components/print_header', ['printTitle' => '지정판매소 바코드 출력']) ?>
<style>
td.name-cell { max-width: 14rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
td.addr-cell { max-width: 24rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ds-bc-check { width: 14px; height: 14px; }
</style>

<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel">
  <div class="flex flex-wrap items-center justify-between gap-y-2">
    <span class="text-sm font-bold text-gray-700">지정판매소 바코드 출력</span>
    <div class="flex items-center gap-2">
      <button type="button" id="ds-bc-print-btn" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">인쇄</button>
    </div>
  </div>
</section>

<section class="p-2 bg-white border-b border-gray-200 no-print">
  <form id="ds-bc-filter-form" method="get" action="<?= mgmt_url('designated-shops/barcode') ?>" class="flex flex-wrap items-end gap-3">
    <div class="min-w-[12rem]">
      <label class="block text-xs text-gray-600 mb-0.5">군·구</label>
      <div class="border border-gray-300 rounded px-3 py-1 text-sm bg-gray-50 text-gray-800 font-medium pointer-events-none select-none">
        <?= esc($fixedGugunLabel ?? '현재 지자체') ?>
      </div>
    </div>
    <div>
      <label class="block text-xs text-gray-600 mb-0.5">읍·면·동</label>
      <select name="ds_zone_code" class="border border-gray-300 rounded px-2 py-1 text-sm min-w-[10rem]">
        <option value="">전체</option>
        <?php foreach (($zones ?? []) as $z): ?>
        <?php $zc = trim((string) ($z->zone_code ?? '')); ?>
        <option value="<?= esc($zc) ?>" <?= ($zoneFilter ?? '') === $zc ? 'selected' : '' ?>><?= esc($zc) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="block text-xs text-gray-600 mb-0.5">조회순서</label>
      <select name="order_by" class="border border-gray-300 rounded px-2 py-1 text-sm min-w-[8rem]">
        <option value="shop_no" <?= ($orderBy ?? 'shop_no') === 'shop_no' ? 'selected' : '' ?>>판매소 코드</option>
        <option value="name" <?= ($orderBy ?? '') === 'name' ? 'selected' : '' ?>>판매소명</option>
      </select>
    </div>
    <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">조회</button>
  </form>
</section>

<section class="mx-2 mt-2 mb-2">
  <form id="ds-bc-print-form" method="post" action="<?= mgmt_url('designated-shops/barcode/print') ?>" target="ds-bc-print-frame">
    <?= csrf_field() ?>
    <input type="hidden" name="zone_label" value="<?= esc(($zoneFilter ?? '') !== '' ? (string) $zoneFilter : '전체') ?>">
    <div class="mb-1 text-xs text-gray-600">
      <label class="inline-flex items-center gap-1 cursor-pointer"><input type="checkbox" id="ds-bc-check-all" class="ds-bc-check"> 전체선택</label>
      <span class="ml-3">선택 건수: <strong id="ds-bc-selected-count">0</strong></span>
    </div>
    <div class="overflow-auto">
      <table class="w-full text-[13px]">
        <thead>
          <tr class="text-left text-[11px] font-semibold text-gray-500 border-b border-gray-200">
            <th class="py-2.5 px-2 w-14 text-left">출력</th>
            <th class="py-2.5 px-2 w-36">판매소 코드</th>
            <th class="py-2.5 px-2">판매소명</th>
            <th class="py-2.5 px-2 w-24">대표자명</th>
            <th class="py-2.5 px-2 w-32">사업자번호</th>
            <th class="py-2.5 px-2">사업장 주소</th>
            <th class="py-2.5 px-2 w-16 text-left">상태</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach (($list ?? []) as $row): ?>
          <?php
            $st = (int) ($row->ds_state ?? 1);
            $stLabel = $st === 1 ? '사용' : '정지';
          ?>
          <tr class="border-b border-gray-200 last:border-0 hover:bg-gray-50">
            <td class="py-2.5 px-2 text-left"><input class="ds-bc-row-check ds-bc-check" type="checkbox" name="ds_idx[]" value="<?= (int) $row->ds_idx ?>"></td>
            <td class="py-2.5 px-2 font-mono text-gray-700"><?= esc((string) ($row->ds_shop_no ?? '')) ?></td>
            <td class="py-2.5 px-2 name-cell font-medium text-gray-900" title="<?= esc((string) ($row->ds_name ?? '')) ?>"><?= esc((string) ($row->ds_name ?? '')) ?></td>
            <td class="py-2.5 px-2 text-gray-600"><?= esc((string) ($row->ds_rep_name ?? '')) ?></td>
            <td class="py-2.5 px-2 font-mono text-gray-700"><?= esc((string) ($row->ds_biz_no ?? '')) ?></td>
            <td class="py-2.5 px-2 addr-cell text-gray-500" title="<?= esc((string) ($row->ds_addr ?? '')) ?>"><?= esc((string) ($row->ds_addr ?? '')) ?></td>
            <td class="py-2.5 px-2 text-left"><span class="inline-block px-2 py-0.5 rounded-full text-[11px] font-medium <?= $st === 1 ? 'bg-emerald-50 text-emerald-700' : 'bg-gray-100 text-gray-500' ?>"><?= esc($stLabel) ?></span></td>
          </tr>
          <?php endforeach; ?>
          <?php if (empty($list)): ?>
          <tr><td colspan="7" class="text-center text-gray-400 py-6">조회된 지정판매소가 없습니다.</td></tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </form>
  <iframe name="ds-bc-print-frame" class="hidden" style="display:none;width:0;height:0;border:0;" aria-hidden="true"></iframe>
</section>

<?php if (isset($pager)): ?>
<div class="mt-2 mb-2 mx-2 no-print"><?= $pager->links() ?></div>
<?php endif; ?>

<script>
(function () {
  var all = document.getElementById('ds-bc-check-all');
  var countEl = document.getElementById('ds-bc-selected-count');
  var printBtn = document.getElementById('ds-bc-print-btn');
  var printForm = document.getElementById('ds-bc-print-form');
  var rows = Array.prototype.slice.call(document.querySelectorAll('.ds-bc-row-check'));
  if (!all || !countEl || !rows.length) return;

  function refreshCount() {
    var n = rows.filter(function (el) { return el.checked; }).length;
    countEl.textContent = String(n);
    all.checked = n > 0 && n === rows.length;
    all.indeterminate = n > 0 && n < rows.length;
  }

  all.addEventListener('change', function () {
    rows.forEach(function (el) { el.checked = all.checked; });
    refreshCount();
  });
  rows.forEach(function (el) { el.addEventListener('change', refreshCount); });

  if (printBtn && printForm) {
    printBtn.addEventListener('click', function () {
      var selected = rows.filter(function (el) { return el.checked; }).length;
      if (selected < 1) {
        window.alert('출력할 지정판매소를 1개 이상 선택해 주세요.');
        return;
      }
      printForm.action = "<?= esc(mgmt_url('designated-shops/barcode/print')) ?>?autoprint=1";
      printForm.submit();
    });
  }

  refreshCount();
})();
</script>
