<?= view('components/print_header', ['printTitle' => '기타 입출고']) ?>
<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>
    <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>
</section>

<?php if (!($tableExists ?? false)): ?>
<div class="border border-orange-300 bg-orange-50 p-3 mt-2 text-sm text-orange-700">
  bag_misc_flow 테이블이 생성되지 않았습니다. <code>writable/database/bag_misc_flow_tables.sql</code>을 실행해 주세요.
</div>
<?php endif; ?>

<!-- 등록 폼 -->
<section class="p-2 bg-white border-b border-gray-200 no-print">
  <form method="POST" action="<?= mgmt_url('reports/misc-flow') ?>" class="flex flex-wrap items-center gap-2">
    <?= csrf_field() ?>
    <label class="text-sm text-gray-600">구분</label>
    <select name="bmf_type" class="border border-gray-300 rounded px-2 py-1 text-sm" required>
      <option value="in">입고</option>
      <option value="out">출고</option>
    </select>
    <label class="text-sm text-gray-600">봉투</label>
    <select name="bmf_bag_code" class="border border-gray-300 rounded px-2 py-1 text-sm" required>
      <option value="">선택</option>
      <?php foreach ($bagCodes as $bc): ?>
      <option value="<?= esc($bc->cd_code) ?>"><?= esc($bc->cd_code . ' - ' . $bc->cd_name) ?></option>
      <?php endforeach; ?>
    </select>
    <label class="text-sm text-gray-600">수량</label>
    <input type="number" name="bmf_qty" min="1" class="border border-gray-300 rounded px-2 py-1 text-sm w-24" required/>
    <label class="text-sm text-gray-600">일자</label>
    <input type="date" name="bmf_date" value="<?= date('Y-m-d') ?>" class="border border-gray-300 rounded px-2 py-1 text-sm" required/>
    <label class="text-sm text-gray-600">사유</label>
    <input type="text" name="bmf_reason" placeholder="입출고 사유" class="border border-gray-300 rounded px-2 py-1 text-sm w-48" required/>
    <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">등록</button>
  </form>
</section>

<!-- 조회 필터 -->
<section class="p-2 bg-white border-b border-gray-200 no-print">
  <form method="GET" action="<?= mgmt_url('reports/misc-flow') ?>" class="flex flex-wrap items-center gap-2">
    <label class="text-sm text-gray-600">시작일</label>
    <input type="date" name="start_date" value="<?= esc($startDate ?? '') ?>" class="border border-gray-300 rounded px-2 py-1 text-sm"/>
    <label class="text-sm text-gray-600">~</label>
    <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 rounded-sm text-sm">조회</button>
  </form>
</section>

<div class="border border-gray-300 overflow-auto mt-2">
  <table class="w-full data-table">
    <thead>
      <tr>
        <th>번호</th>
        <th>구분</th>
        <th>일자</th>
        <th>봉투코드</th>
        <th>봉투명</th>
        <th>수량</th>
        <th>사유</th>
        <th>등록일</th>
      </tr>
    </thead>
    <tbody class="text-right">
      <?php foreach ($result as $row): ?>
      <tr>
        <td class="text-center"><?= (int) $row->bmf_idx ?></td>
        <td class="text-center"><?= $row->bmf_type === 'in' ? '<span class="text-blue-600">입고</span>' : '<span class="text-red-600">출고</span>' ?></td>
        <td class="text-center"><?= esc($row->bmf_date) ?></td>
        <td class="text-center font-mono"><?= esc($row->bmf_bag_code) ?></td>
        <td class="text-left pl-2"><?= esc($row->bmf_bag_name) ?></td>
        <td><?= number_format((int) $row->bmf_qty) ?></td>
        <td class="text-left pl-2"><?= esc($row->bmf_reason) ?></td>
        <td class="text-center"><?= esc($row->bmf_regdate) ?></td>
      </tr>
      <?php endforeach; ?>
      <?php if (empty($result)): ?>
      <tr><td colspan="8" class="text-center text-gray-400 py-4">조회된 데이터가 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>
