<div class="space-y-1">
  <form method="get" class="flex items-center gap-3 text-sm mb-3">
    <label class="font-bold text-gray-700">조회기간</label>
    <input type="date" name="start_date" value="<?= esc($startDate ?? '') ?>" class="border border-gray-300 rounded px-2 py-1 text-sm"/>
    <span>~</span>
    <input type="date" name="end_date" value="<?= esc($endDate ?? '') ?>" class="border border-gray-300 rounded px-2 py-1 text-sm"/>
    <button type="submit" class="bg-btn-search text-white px-4 py-1.5 rounded-sm text-sm">조회</button>
    <a href="<?= base_url('bag/flow') ?>" class="text-sm text-gray-500 hover:text-gray-700">초기화</a>
  </form>

  <!-- 수불 요약 -->
  <table class="data-table">
    <thead>
      <tr>
        <th rowspan="2">봉투코드</th>
        <th rowspan="2">봉투명</th>
        <th rowspan="2">현재재고</th>
        <th colspan="2">입고</th>
        <th colspan="2">출고</th>
      </tr>
      <tr>
        <th>입고수량</th><th>반품수량</th>
        <th>판매수량</th><th>불출수량</th>
      </tr>
    </thead>
    <tbody>
    <?php
    // 봉투코드별 수불 집계
    $summary = [];
    // 재고
    foreach ($inventory as $inv) {
        $code = $inv->bi_bag_code ?? '';
        if (! isset($summary[$code])) {
            $summary[$code] = ['name' => $inv->bi_bag_name ?? '', 'stock' => 0, 'recv' => 0, 'return' => 0, 'sale' => 0, 'issue' => 0];
        }
        $summary[$code]['stock'] += (int)($inv->bi_qty_sheet ?? 0);
    }
    // 입고
    foreach ($receiving as $r) {
        $code = $r->br_bag_code ?? '';
        if (! isset($summary[$code])) {
            $summary[$code] = ['name' => $r->br_bag_name ?? '', 'stock' => 0, 'recv' => 0, 'return' => 0, 'sale' => 0, 'issue' => 0];
        }
        $summary[$code]['recv'] += (int)($r->br_qty_sheet ?? 0);
    }
    // 판매/반품
    foreach ($sales as $s) {
        $code = $s->bs_bag_code ?? '';
        if (! isset($summary[$code])) {
            $summary[$code] = ['name' => $s->bs_bag_name ?? '', 'stock' => 0, 'recv' => 0, 'return' => 0, 'sale' => 0, 'issue' => 0];
        }
        $type = $s->bs_type ?? 'sale';
        if ($type === 'return') {
            $summary[$code]['return'] += (int)($s->bs_qty ?? 0);
        } else {
            $summary[$code]['sale'] += (int)($s->bs_qty ?? 0);
        }
    }
    // 불출
    foreach ($issues as $iss) {
        $code = $iss->bi2_bag_code ?? '';
        if (! isset($summary[$code])) {
            $summary[$code] = ['name' => $iss->bi2_bag_name ?? '', 'stock' => 0, 'recv' => 0, 'return' => 0, 'sale' => 0, 'issue' => 0];
        }
        if (($iss->bi2_status ?? 'normal') === 'normal') {
            $summary[$code]['issue'] += (int)($iss->bi2_qty ?? 0);
        }
    }
    ksort($summary);
    ?>
    <?php if (! empty($summary)): ?>
      <?php $idx = 0; foreach ($summary as $code => $s): $idx++; ?>
      <tr>
        <td class="text-center"><?= esc($code) ?></td>
        <td><?= esc($s['name']) ?></td>
        <td class="text-right"><?= number_format($s['stock']) ?></td>
        <td class="text-right"><?= number_format($s['recv']) ?></td>
        <td class="text-right"><?= number_format($s['return']) ?></td>
        <td class="text-right"><?= number_format($s['sale']) ?></td>
        <td class="text-right"><?= number_format($s['issue']) ?></td>
      </tr>
      <?php endforeach; ?>
    <?php else: ?>
      <tr><td colspan="7" class="text-center text-gray-400 py-4">수불 데이터가 없습니다.</td></tr>
    <?php endif; ?>
    </tbody>
  </table>
</div>
