<?= view('components/print_header', ['printTitle' => '봉투 단가 관리']) ?>
<style>
@media print {
  .no-print { display: none !important; }
}
</style>
<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>
    <div class="flex items-center gap-2 no-print">
      <button onclick="window.print()" class="border border-btn-print-border text-gray-600 px-3 py-1 rounded-sm text-sm hover:bg-gray-50 transition">인쇄</button>
      <a href="<?= mgmt_url('bag-prices/create') ?>" class="bg-btn-search text-white px-4 py-1.5 rounded-sm flex items-center gap-1 text-sm shadow hover:opacity-90 transition border border-transparent">단가 등록</a>
    </div>
  </div>
</section>
<section class="no-print border border-gray-200 rounded-lg bg-white p-3 mt-2">
  <form method="get" action="<?= mgmt_url('bag-prices') ?>" class="flex flex-wrap items-end gap-3" autocomplete="off">
    <div class="flex flex-col gap-0.5">
      <label class="text-xs text-gray-500">봉투구분</label>
      <select name="bag_kind_e" class="border border-gray-300 rounded px-2 py-1.5 text-sm min-w-[9rem]">
        <option value="">전체</option>
        <?php foreach ($bag_kind_options ?? [] as $cd): ?>
        <option value="<?= esc($cd->cd_code) ?>" <?= (string) ($cd->cd_code ?? '') === (string) ($bag_kind_e ?? '') ? 'selected' : '' ?>>
          <?= esc($cd->cd_name) ?>
        </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="flex flex-col gap-0.5">
      <label class="text-xs text-gray-500">봉투코드</label>
      <select name="bag_code" class="border border-gray-300 rounded px-2 py-1.5 text-sm min-w-[11rem]">
        <option value="">전체</option>
        <?php foreach ($bag_codes ?? [] as $cd): ?>
        <option value="<?= esc($cd->cd_code) ?>" <?= (string) ($cd->cd_code ?? '') === (string) ($bag_code ?? '') ? 'selected' : '' ?>>
          <?= esc($cd->cd_code) ?> — <?= esc($cd->cd_name) ?>
        </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="flex flex-col gap-0.5">
      <label class="text-xs text-gray-500">조회 기간 (적용기간 겹침)</label>
      <?php
      $sp = $startParts ?? ['y' => '', 'm' => '', 'd' => ''];
      $ep = $endParts ?? ['y' => '', 'm' => '', 'd' => ''];
      $ymin = (int) ($dateYearMin ?? ((int) date('Y') - 12));
      $ymax = (int) ($dateYearMax ?? ((int) date('Y') + 2));
      ?>
      <div class="flex flex-wrap items-center gap-1">
        <span class="text-xs text-gray-500 mr-0.5">시작</span>
        <select name="start_y" class="border border-gray-300 rounded px-1.5 py-1.5 text-sm min-w-[4.5rem]">
          <option value="">연도</option>
          <?php for ($yy = $ymin; $yy <= $ymax; $yy++): ?>
          <option value="<?= $yy ?>" <?= (string) ($sp['y'] ?? '') === (string) $yy ? 'selected' : '' ?>><?= $yy ?></option>
          <?php endfor; ?>
        </select>
        <select name="start_m" class="border border-gray-300 rounded px-1.5 py-1.5 text-sm min-w-[3.75rem]">
          <option value="">월</option>
          <?php for ($mi = 1; $mi <= 12; $mi++): ?>
          <option value="<?= $mi ?>" <?= isset($sp['m']) && (int) $sp['m'] === $mi ? 'selected' : '' ?>><?= $mi ?>월</option>
          <?php endfor; ?>
        </select>
        <select name="start_d" class="border border-gray-300 rounded px-1.5 py-1.5 text-sm min-w-[3.75rem]">
          <option value="">일</option>
          <?php for ($di = 1; $di <= 31; $di++): ?>
          <option value="<?= $di ?>" <?= isset($sp['d']) && (int) $sp['d'] === $di ? 'selected' : '' ?>><?= $di ?>일</option>
          <?php endfor; ?>
        </select>
        <span class="text-sm text-gray-500 mx-0.5">~</span>
        <span class="text-xs text-gray-500 mr-0.5">종료</span>
        <select name="end_y" class="border border-gray-300 rounded px-1.5 py-1.5 text-sm min-w-[4.5rem]">
          <option value="">연도</option>
          <?php for ($yy = $ymin; $yy <= $ymax; $yy++): ?>
          <option value="<?= $yy ?>" <?= (string) ($ep['y'] ?? '') === (string) $yy ? 'selected' : '' ?>><?= $yy ?></option>
          <?php endfor; ?>
        </select>
        <select name="end_m" class="border border-gray-300 rounded px-1.5 py-1.5 text-sm min-w-[3.75rem]">
          <option value="">월</option>
          <?php for ($mi = 1; $mi <= 12; $mi++): ?>
          <option value="<?= $mi ?>" <?= isset($ep['m']) && (int) $ep['m'] === $mi ? 'selected' : '' ?>><?= $mi ?>월</option>
          <?php endfor; ?>
        </select>
        <select name="end_d" class="border border-gray-300 rounded px-1.5 py-1.5 text-sm min-w-[3.75rem]">
          <option value="">일</option>
          <?php for ($di = 1; $di <= 31; $di++): ?>
          <option value="<?= $di ?>" <?= isset($ep['d']) && (int) $ep['d'] === $di ? 'selected' : '' ?>><?= $di ?>일</option>
          <?php endfor; ?>
        </select>
      </div>
    </div>
    <div class="flex items-center gap-2 pb-0.5">
      <button type="submit" class="bg-btn-search text-white px-4 py-1.5 rounded-sm text-sm">조회</button>
      <a href="<?= mgmt_url('bag-prices') ?>" class="text-sm text-gray-500 hover:underline">초기화</a>
      <button type="button" onclick="window.print()" class="border border-gray-300 text-gray-700 px-3 py-1.5 rounded-sm text-sm hover:bg-gray-50">인쇄</button>
    </div>
  </form>
</section>
<div class="border border-gray-300 rounded-lg p-4 overflow-auto mt-2">
  <table class="w-full data-table">
    <thead>
      <tr>
        <th class="w-16 text-center">번호</th>
        <th class="text-center">봉투코드</th>
        <th class="text-left">봉투명</th>
        <th class="text-right">발주단가</th>
        <th class="text-right">도매가</th>
        <th class="text-right">소비자가</th>
        <th class="text-center">적용시작</th>
        <th class="text-center">적용종료</th>
        <th class="w-20 text-center">상태</th>
        <th class="w-36 no-print text-center">작업</th>
      </tr>
    </thead>
    <tbody class="text-right">
      <?php
        $startNo = 1;
      if (isset($pager) && method_exists($pager, 'getCurrentPage') && method_exists($pager, 'getPerPage')) {
          $currentPage = (int) $pager->getCurrentPage();
          $perPage = (int) $pager->getPerPage();
          $startNo = (($currentPage > 0 ? $currentPage : 1) - 1) * ($perPage > 0 ? $perPage : 20) + 1;
      }
      ?>
      <?php foreach (($list ?? []) as $idx => $row): ?>
      <tr>
        <td class="text-center"><?= (int) $startNo + (int) $idx ?></td>
        <td class="text-center font-mono"><?= esc($row->bp_bag_code) ?></td>
        <td class="text-left pl-2"><?= esc($row->bp_bag_name) ?></td>
        <td><?= number_format((float) $row->bp_order_price) ?></td>
        <td><?= number_format((float) $row->bp_wholesale) ?></td>
        <td><?= number_format((float) $row->bp_consumer) ?></td>
        <td class="text-center"><?= esc($row->bp_start_date) ?></td>
        <td class="text-center"><?= esc($row->bp_end_date ?? '현재') ?></td>
        <td class="text-center"><?= (int) $row->bp_state === 1 ? '사용' : '미사용' ?></td>
        <td class="text-center no-print">
          <a href="<?= mgmt_url('bag-prices/history/' . (int) $row->bp_idx) ?>" class="text-green-600 hover:underline text-sm mr-1">이력</a>
          <a href="<?= mgmt_url('bag-prices/edit/' . (int) $row->bp_idx) ?>" class="text-blue-600 hover:underline text-sm mr-1">수정</a>
          <form action="<?= mgmt_url('bag-prices/delete/' . (int) $row->bp_idx) ?>" method="POST" class="inline" onsubmit="return confirm('삭제하시겠습니까?');">
            <?= csrf_field() ?>
            <button type="submit" class="text-red-600 hover:underline text-sm">삭제</button>
          </form>
        </td>
      </tr>
      <?php endforeach; ?>
      <?php if (empty($list)): ?>
      <tr><td colspan="10" class="text-center text-gray-400 py-4">등록된 단가가 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>
<?php if (isset($pager)): ?><div class="mt-3 no-print"><?= $pager->links() ?></div><?php endif; ?>
