<?= view('components/print_header', [
    'printTitle' => '재고 현황',
    'printDate' => (string) ($baseDate ?? date('Y-m-d')),
    'printExtraLines' => [
        '기준일자: ' . (string) ($baseDate ?? date('Y-m-d')),
    ],
]) ?>
<?php
$baseDate = (string) ($baseDate ?? date('Y-m-d'));
$agencyIdx = (int) ($agencyIdx ?? 0);
$rows = is_array($rows ?? null) ? $rows : [];
$subtotals = is_array($subtotals ?? null) ? $subtotals : [];
$grandTotals = is_array($grandTotals ?? null) ? $grandTotals : ['total' => 0, 'gugun' => 0, 'agency' => 0];
$agencyOptions = is_array($agencyOptions ?? null) ? $agencyOptions : [];
$subtotalByGroup = [];
foreach ($subtotals as $subtotal) {
    $group = (string) ($subtotal['group'] ?? '');
    if ($group !== '') {
        $subtotalByGroup[$group] = $subtotal;
    }
}
?>

<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel">
  <form method="get" class="flex flex-wrap items-end justify-between gap-2">
    <div class="flex flex-wrap items-end gap-2 text-sm">
      <label class="font-bold text-gray-700">기준일자</label>
      <input type="date" name="base_date" value="<?= esc($baseDate) ?>" class="border border-gray-300 rounded px-2 py-1 min-w-[10rem]">

      <label class="font-bold text-gray-700">대행소</label>
      <select name="agency_idx" class="border border-gray-300 rounded px-2 py-1 min-w-[12rem]">
        <option value="0">전체</option>
        <?php foreach ($agencyOptions as $agency): ?>
        <?php $idx = (int) ($agency->sa_idx ?? 0); ?>
        <option value="<?= esc((string) $idx) ?>" <?= $agencyIdx === $idx ? 'selected' : '' ?>>
          <?= esc((string) ($agency->sa_name ?? '')) ?>
        </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="flex items-center gap-2">
      <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">조회</button>
      <a href="<?= base_url('bag/inventory/export?' . http_build_query(['base_date' => $baseDate, 'agency_idx' => $agencyIdx])) ?>" class="no-print border border-btn-excel-border text-btn-excel-text px-3 py-1 rounded-sm text-sm hover:bg-green-50 transition">엑셀저장</a>
      <button type="button" onclick="window.print()" class="no-print border border-btn-print-border text-gray-600 px-3 py-1 rounded-sm text-sm hover:bg-gray-50 transition">인쇄</button>
    </div>
  </form>
</section>

<div class="mt-2 border border-gray-300 rounded-lg bg-white p-2 print:p-0 print:border-0">
  <div class="overflow-auto">
    <!-- 종류~계 사이 간격 축소 — 표 폭을 내용에 맞게 제한 (피드백 #18) -->
    <table class="w-full max-w-4xl data-table text-sm">
      <thead>
        <tr>
          <th class="w-36 text-center">품 목 구 분</th>
          <th class="text-left">봉투/스티커 종류</th>
          <th class="w-32 text-right">계</th>
          <th class="w-32 text-right">시군구 재고</th>
          <th class="w-32 text-right">대행소 재고</th>
        </tr>
      </thead>
      <tbody>
        <?php if ($rows !== []): ?>
          <?php
          $groupRowCount = [];
          foreach ($rows as $row) {
              $group = (string) ($row['group'] ?? '');
              if (! isset($groupRowCount[$group])) {
                  $groupRowCount[$group] = 0;
              }
              $groupRowCount[$group]++;
          }
          $printedGroupCount = [];
          ?>
          <?php foreach ($rows as $row): ?>
          <?php
              $group = (string) ($row['group'] ?? '');
              if (! isset($printedGroupCount[$group])) {
                  $printedGroupCount[$group] = 0;
              }
              $printedGroupCount[$group]++;
              $isFirst = $printedGroupCount[$group] === 1;
              $isLast = $printedGroupCount[$group] === (int) ($groupRowCount[$group] ?? 0);
          ?>
          <tr>
            <?php if ($isFirst): ?>
            <td class="text-center font-semibold bg-gray-50" rowspan="<?= esc((string) ($groupRowCount[$group] ?? 1)) ?>"><?= esc($group) ?></td>
            <?php endif; ?>
            <td class="pl-2"><?= esc((string) ($row['name'] ?? '')) ?></td>
            <td class="text-right pr-2"><?= number_format((int) ($row['total_qty'] ?? 0)) ?></td>
            <td class="text-right pr-2"><?= number_format((int) ($row['gugun_qty'] ?? 0)) ?></td>
            <td class="text-right pr-2"><?= number_format((int) ($row['agency_qty'] ?? 0)) ?></td>
          </tr>
          <?php if ($isLast && isset($subtotalByGroup[$group])): ?>
          <?php $s = $subtotalByGroup[$group]; ?>
          <tr class="bg-blue-50 font-semibold">
            <td class="text-center">소계</td>
            <td class="text-right pr-2"><?= number_format((int) ($s['total_qty'] ?? 0)) ?></td>
            <td class="text-right pr-2"><?= number_format((int) ($s['gugun_qty'] ?? 0)) ?></td>
            <td class="text-right pr-2"><?= number_format((int) ($s['agency_qty'] ?? 0)) ?></td>
          </tr>
          <?php endif; ?>
          <?php endforeach; ?>
        <?php else: ?>
          <tr><td colspan="5" class="text-center text-gray-400 py-4">조회 결과가 없습니다.</td></tr>
        <?php endif; ?>
      </tbody>
      <tfoot>
        <tr class="bg-gray-100 font-bold">
          <td class="text-center" colspan="2">합계</td>
          <td class="text-right pr-2"><?= number_format((int) ($grandTotals['total'] ?? 0)) ?></td>
          <td class="text-right pr-2"><?= number_format((int) ($grandTotals['gugun'] ?? 0)) ?></td>
          <td class="text-right pr-2"><?= number_format((int) ($grandTotals['agency'] ?? 0)) ?></td>
        </tr>
      </tfoot>
    </table>
  </div>

  <p class="mt-2 text-xs text-gray-500">
    ※ 기준일자까지 갱신된 재고를 집계합니다. 대행소 재고는 별도 재고 연계 전까지 0으로 표시됩니다.
  </p>
</div>
