<?= 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>
<section class="p-2 bg-white border-b border-gray-200">
  <form method="GET" action="<?= base_url('admin/reports/returns') ?>" 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>
      </tr>
    </thead>
    <tbody class="text-right">
      <?php
        $totalQty = 0; $totalAmt = 0;
        $typeMap = ['return' => '반품', 'cancel' => '취소/파기'];
        foreach ($result as $row):
          $totalQty += (int) $row->qty;
          $totalAmt += (int) $row->amount;
      ?>
      <tr>
        <td class="text-center"><?= esc($row->bs_sale_date) ?></td>
        <td class="text-left pl-2"><?= esc($row->bs_ds_name) ?></td>
        <td class="text-center font-mono"><?= esc($row->bs_bag_code) ?></td>
        <td class="text-left pl-2"><?= esc($row->bs_bag_name) ?></td>
        <td class="text-center"><?= esc($typeMap[$row->bs_type] ?? $row->bs_type) ?></td>
        <td><?= number_format((int) $row->qty) ?></td>
        <td><?= number_format((int) $row->amount) ?></td>
      </tr>
      <?php endforeach; ?>
      <?php if (empty($result)): ?>
      <tr><td colspan="7" class="text-center text-gray-400 py-4">조회된 데이터가 없습니다.</td></tr>
      <?php else: ?>
      <tr class="font-bold bg-gray-100">
        <td colspan="5" class="text-center">합계</td>
        <td><?= number_format($totalQty) ?></td>
        <td><?= number_format($totalAmt) ?></td>
      </tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>
