<?php
$_ph = ['printTitle' => '봉투 단가 조회', 'printExtraLines' => $printExtraLines ?? []];
if (($printLgName ?? '') !== '') {
    $_ph['printLgName'] = $printLgName;
}
echo view('components/print_header', $_ph);
unset($_ph);
?>
<style>
@media print {
  .no-print { display: none !important; }
}
</style>
<div class="space-y-4">
  <p class="text-sm text-gray-600 no-print">
    <a href="<?= base_url('bag/basic-info') ?>" class="text-blue-600 hover:underline">&larr; 기본정보관리</a>
    <span class="mx-2 text-gray-300">|</span>
    <a href="<?= base_url('bag/packaging-units') ?>" class="text-blue-600 hover:underline">포장 단위</a>
    <span class="mx-2 text-gray-300">|</span>
    <a href="<?= base_url('bag/bag-prices') ?>" class="text-blue-600 hover:underline">단가 관리(CRUD)</a>
  </p>

  <?php if (($lgIdx ?? null) === null): ?>
  <p class="text-sm text-amber-800 bg-amber-50 border border-amber-200 rounded p-3">작업 지자체가 선택되지 않았습니다. 관리자는 지자체 선택 후 단가를 조회할 수 있습니다.</p>
  <?php endif; ?>

  <section class="no-print border border-gray-200 rounded-lg bg-white p-3">
    <form method="post" action="<?= site_url('bag/prices') ?>" class="flex flex-wrap items-end gap-3" autocomplete="off">
      <?= csrf_field() ?>
      <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]" title="시작 연도">
            <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]" title="시작 월">
            <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]" title="시작 일">
            <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]" title="종료 연도">
            <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]" title="종료 월">
            <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]" title="종료 일">
            <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="<?= base_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>

  <section>
    <div class="flex items-center justify-between mb-2 border-b pb-1">
      <h3 class="text-base font-bold text-gray-700">봉투 단가</h3>
    </div>
    <div class="overflow-auto">
    <table class="w-full text-[13px]">
      <thead>
        <tr class="text-left text-[11px] font-semibold text-gray-500 border-b border-gray-200">
          <th class="py-2.5 px-2 w-16 text-left">번호</th>
          <th class="py-2.5 px-2">봉투코드</th>
          <th class="py-2.5 px-2">봉투명</th>
          <th class="py-2.5 px-2 text-left">발주단가</th>
          <th class="py-2.5 px-2 text-left">도매가</th>
          <th class="py-2.5 px-2 text-left">소비자가</th>
          <th class="py-2.5 px-2 text-left">적용시작</th>
          <th class="py-2.5 px-2 text-left">적용종료</th>
          <th class="py-2.5 px-2 text-left">상태</th>
        </tr>
      </thead>
      <tbody>
      <?php if (! empty($bagPrices)): ?>
        <?php foreach ($bagPrices as $row): ?>
        <tr class="border-b border-gray-200 last:border-0 hover:bg-gray-50">
          <td class="py-2.5 px-2 text-left text-gray-500"><?= esc($row->bp_idx) ?></td>
          <td class="py-2.5 px-2 font-mono text-gray-700"><?= esc($row->bp_bag_code) ?></td>
          <td class="py-2.5 px-2 font-medium text-gray-900"><?= esc($row->bp_bag_name ?? '') ?></td>
          <td class="py-2.5 px-2 text-left text-gray-600"><?= number_format((float) ($row->bp_order_price ?? 0)) ?></td>
          <td class="py-2.5 px-2 text-left text-gray-600"><?= number_format((float) ($row->bp_wholesale ?? 0)) ?></td>
          <td class="py-2.5 px-2 text-left text-gray-600"><?= number_format((float) ($row->bp_consumer ?? 0)) ?></td>
          <td class="py-2.5 px-2 text-left text-gray-500 text-[12px]"><?= esc($row->bp_start_date ?? '') ?></td>
          <td class="py-2.5 px-2 text-left text-gray-500 text-[12px]"><?= ($row->bp_end_date ?? '') !== '' && $row->bp_end_date !== null ? esc((string) $row->bp_end_date) : '현재' ?></td>
          <td class="py-2.5 px-2 text-left">
            <?php if ((int) ($row->bp_state ?? 1) === 1): ?>
            <span class="inline-block px-2 py-0.5 rounded-full text-[11px] font-medium bg-emerald-50 text-emerald-700">사용</span>
            <?php else: ?>
            <span class="inline-block px-2 py-0.5 rounded-full text-[11px] font-medium bg-gray-100 text-gray-500">만료</span>
            <?php endif; ?>
          </td>
        </tr>
        <?php endforeach; ?>
      <?php else: ?>
        <tr><td colspan="9" class="text-center text-gray-400 py-6">등록된 단가 정보가 없습니다.</td></tr>
      <?php endif; ?>
      </tbody>
    </table>
    </div>
    <?php if (! empty($pager)): ?>
    <div class="mt-3 no-print"><?= $pager->links() ?></div>
    <?php endif; ?>
  </section>
</div>
