<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel flex flex-wrap items-center justify-between gap-2">
  <span class="text-sm font-bold text-gray-700">일괄 입고</span>
  <div class="flex items-center gap-2">
    <a href="<?= base_url('bag/receiving/scanner') ?>" class="border border-gray-300 text-gray-700 px-3 py-1 rounded-sm text-sm hover:bg-gray-50">스캐너 입고</a>
    <a href="<?= base_url('bag/receiving/status') ?>" class="border border-gray-300 text-gray-700 px-3 py-1 rounded-sm text-sm hover:bg-gray-50">입고 현황</a>
  </div>
</section>

<?php /* 플래시 메시지는 레이아웃(embed/portal)에서 한 번만 표시한다 — 중복 출력 방지 */ ?>

<section class="p-2 bg-white border border-gray-300 rounded-lg mt-2">
  <form method="get" action="<?= base_url('bag/receiving/batch') ?>" class="flex flex-wrap items-center gap-2">
    <label class="text-sm text-gray-600">제작 업체</label>
    <select name="company_idx" class="border border-gray-300 rounded px-2 py-1 text-sm w-56">
      <option value="0">전 체</option>
      <?php foreach (($companies ?? []) as $company): ?>
      <option value="<?= (int) $company->cp_idx ?>" <?= (int) ($companyIdx ?? 0) === (int) $company->cp_idx ? 'selected' : '' ?>>
        <?= esc((string) ($company->cp_name ?? '')) ?>
      </option>
      <?php endforeach; ?>
    </select>
    <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">조회</button>
  </form>
</section>

<form action="<?= base_url('bag/receiving/batch/store') ?>" method="post" class="mt-2 space-y-2">
  <?= csrf_field() ?>
  <input type="hidden" name="company_idx" value="<?= (int) ($companyIdx ?? 0) ?>" />

  <section class="p-2 bg-white border border-gray-300 rounded-lg">
    <div class="grid grid-cols-1 xl:grid-cols-12 gap-2 items-end">
      <div class="xl:col-span-3 flex items-center gap-2">
        <label class="text-sm text-gray-600 shrink-0 w-28">인수자 (대행소)</label>
        <select name="br_receiver_ref" class="border border-gray-300 rounded px-2 py-1 text-sm w-full" required>
          <?php foreach (($receiverOptions ?? []) as $opt): ?>
          <option value="<?= esc((string) ($opt['ref'] ?? '')) ?>" <?= (string) ($receiverRef ?? '') === (string) ($opt['ref'] ?? '') ? 'selected' : '' ?>>
            <?= esc((string) ($opt['label'] ?? '')) ?>
          </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="xl:col-span-3 flex items-center gap-2">
        <label class="text-sm text-gray-600 shrink-0 w-28">인계자 (제작업체)</label>
        <select name="br_sender_idx" class="border border-gray-300 rounded px-2 py-1 text-sm w-full">
          <?php foreach (($senders ?? []) as $sender): ?>
          <option value="<?= (int) ($sender->mg_idx ?? 0) ?>" <?= (int) ($senderIdx ?? 0) === (int) ($sender->mg_idx ?? 0) ? 'selected' : '' ?>>
            <?= esc((string) ($sender->mg_name ?? '')) ?>
          </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="xl:col-span-2 flex items-center gap-2">
        <label class="text-sm text-gray-600 w-16">입고일</label>
        <input type="date" name="br_receive_date" value="<?= esc((string) old('br_receive_date', date('Y-m-d'))) ?>" class="border border-gray-300 rounded px-2 py-1 text-sm w-full" required />
      </div>
      <div class="xl:col-span-2">
        <button type="submit" class="w-full border border-blue-600 text-blue-700 px-2 py-1 rounded-sm text-sm hover:bg-blue-50">입고 처리</button>
      </div>
      <div class="xl:col-span-2 text-xs text-gray-500">체크한 LOT-봉투 행의 미입고량을 전부 입고 처리합니다.</div>
    </div>
  </section>

  <div class="border border-gray-300 rounded-lg p-4 overflow-auto bg-white">
    <table class="w-full data-table text-sm">
      <thead>
        <tr>
          <th class="w-12 text-center"><input type="checkbox" id="check-all" /></th>
          <th class="text-center">발주일자</th>
          <th>봉투종류</th>
          <th class="text-right">발주량(매)</th>
          <th class="text-right">미입고량(매)</th>
          <th>제작업체</th>
          <th class="text-center">LOT NO</th>
          <th class="text-center">발주NO</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($rows ?? []) as $row): ?>
        <tr>
          <td class="text-center">
            <input type="checkbox" name="selected_rows[]" value="<?= esc((string) ($row['row_key'] ?? '')) ?>" />
          </td>
          <td class="text-center"><?= esc((string) ($row['order_date'] ?? '')) ?></td>
          <td class="text-left pl-2"><?= esc((string) ($row['bag_name'] ?? '')) ?></td>
          <td class="text-right"><?= number_format((int) ($row['order_qty_sheet'] ?? 0)) ?></td>
          <td class="text-right text-blue-700 font-semibold"><?= number_format((int) ($row['pending_qty_sheet'] ?? 0)) ?></td>
          <td class="text-left pl-2"><?= esc((string) ($row['company_name'] ?? '')) ?></td>
          <td class="text-center font-mono"><?= esc((string) ($row['lot_no'] ?? '')) ?></td>
          <td class="text-center"><?= esc((string) ($row['order_no'] ?? '')) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($rows ?? [])): ?>
        <tr><td colspan="8" class="text-center text-gray-400 py-4">일괄 입고 가능한 미입고 행이 없습니다.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>

  <div class="flex items-center gap-2">
    <a href="<?= base_url('bag/purchase-inbound') ?>" class="bg-gray-200 text-gray-700 px-6 py-1.5 rounded-sm text-sm">취소</a>
  </div>
</form>

<script>
(() => {
  const all = document.getElementById('check-all');
  if (!all) return;
  all.addEventListener('change', () => {
    document.querySelectorAll('input[name="selected_rows[]"]').forEach((el) => {
      el.checked = all.checked;
    });
  });
})();
</script>
