<?php
$inspection = is_array($inspection ?? null) ? $inspection : [];
$items = is_array($items ?? null) ? $items : [];
$status = (string) ($inspection['bis_status'] ?? '');
?>
<div class="space-y-3">
  <section class="border border-gray-300 rounded-lg bg-white p-3">
    <div class="flex flex-wrap items-center justify-between gap-2">
      <div class="text-sm text-gray-700">
        <span class="font-bold">실사번호:</span> <?= esc((string) ($inspection['bis_idx'] ?? '')) ?>
        <span class="mx-2">|</span>
        <span class="font-bold">작업일자:</span> <?= esc((string) ($inspection['bis_work_date'] ?? '')) ?>
        <span class="mx-2">|</span>
        <span class="font-bold">상태:</span> <?= esc($status) ?>
      </div>
      <a href="<?= base_url('bag/inventory/inspection-select') ?>" class="text-sm text-blue-700 hover:underline">실사 선별 조회로 돌아가기</a>
    </div>
  </section>

  <section class="border border-gray-300 rounded-lg bg-white p-3">
    <form method="post" action="<?= base_url('bag/inventory/inspection/' . (int) ($inspection['bis_idx'] ?? 0) . '/save') ?>" class="space-y-3">
      <?= csrf_field() ?>
      <div class="overflow-auto">
        <table class="w-full data-table text-sm">
          <thead>
            <tr>
              <th class="w-14 text-center">번호</th>
              <th class="text-left">품목명</th>
              <th class="w-24 text-right">전산수량</th>
              <th class="w-24 text-right">실사수량</th>
              <th class="w-24 text-right">차이수량</th>
              <th class="w-20 text-center">반영여부</th>
            </tr>
          </thead>
          <tbody>
            <?php if ($items !== []): ?>
              <?php foreach ($items as $i => $row): ?>
              <?php
                $itemId = (int) ($row['bisi_idx'] ?? 0);
                $systemQty = (int) ($row['bisi_system_qty'] ?? 0);
                $actualQty = $row['bisi_actual_qty'];
                $actualQty = $actualQty === null ? '' : (string) ((int) $actualQty);
                $diffQty = (int) ($row['bisi_diff_qty'] ?? 0);
              ?>
              <tr>
                <td class="text-center"><?= $i + 1 ?></td>
                <td class="pl-2"><?= esc((string) ($row['bisi_bag_name'] ?? '')) ?> <span class="text-xs text-gray-400">(<?= esc((string) ($row['bisi_bag_code'] ?? '')) ?>)</span></td>
                <td class="text-right pr-2"><?= number_format($systemQty) ?></td>
                <td class="text-right pr-2">
                  <input type="number" min="0" name="actual_qty[<?= esc((string) $itemId, 'attr') ?>]" value="<?= esc($actualQty) ?>" class="border border-gray-300 rounded px-1 py-0.5 w-24 text-right">
                </td>
                <td class="text-right pr-2 <?= $diffQty === 0 ? '' : ($diffQty > 0 ? 'text-blue-700' : 'text-red-700') ?>"><?= number_format($diffQty) ?></td>
                <td class="text-center"><?= ((string) ($row['bisi_apply_yn'] ?? 'N')) === 'Y' ? 'Y' : 'N' ?></td>
              </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr><td colspan="6" class="text-center text-gray-400 py-4">실사 품목이 없습니다.</td></tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
      <div class="flex justify-end gap-2">
        <button type="submit" class="bg-btn-search text-white px-5 py-1.5 rounded-sm text-sm">실사 저장</button>
      </div>
    </form>
  </section>

  <section class="border border-gray-300 rounded-lg bg-white p-3">
    <form method="post" action="<?= base_url('bag/inventory/inspection/' . (int) ($inspection['bis_idx'] ?? 0) . '/apply') ?>" id="inspection-apply-form">
      <?= csrf_field() ?>
      <div class="flex items-center justify-between">
        <p class="text-sm text-gray-600">실사 저장 후 확정 시 차이수량이 현재 재고에 반영됩니다.</p>
        <button type="submit" class="bg-green-600 text-white px-5 py-1.5 rounded-sm text-sm">실사 확정(재고 반영)</button>
      </div>
    </form>
  </section>
</div>

<script>
(() => {
  const form = document.getElementById('inspection-apply-form');
  if (!form) return;
  form.addEventListener('submit', (event) => {
    const ok = window.confirm('실사 결과를 재고에 반영하시겠습니까?');
    if (!ok) {
      event.preventDefault();
    }
  });
})();
</script>
