<?php
  // 발주기간: native month 입력은 로케일에 따라 Jan 등 영문 표기될 수 있어 YYYY-MM select + 한글 라벨 사용
  $bagOrderYmChoices = [];
  $bagOrderYmCenterY = (int) date('Y');
  for ($by = $bagOrderYmCenterY - 4; $by <= $bagOrderYmCenterY + 2; $by++) {
      for ($bm = 1; $bm <= 12; $bm++) {
          $bagOrderYmChoices[] = sprintf('%04d-%02d', $by, $bm);
      }
  }
  foreach ([(string) ($startMonth ?? ''), (string) ($endMonth ?? '')] as $ymExtra) {
      if (preg_match('/^\d{4}-\d{2}$/', $ymExtra) && ! in_array($ymExtra, $bagOrderYmChoices, true)) {
          $bagOrderYmChoices[] = $ymExtra;
      }
  }
  sort($bagOrderYmChoices);
  $bagOrderYmLabel = static function (string $ym): string {
      if (preg_match('/^(\d{4})-(\d{2})$/', $ym, $m)) {
          return $m[1] . '년 ' . (int) $m[2] . '월';
      }

      return $ym;
  };
?>
<?= view('components/print_header', ['printTitle' => '봉투 발주 현황']) ?>
<section class="no-print border-b border-gray-300 p-2 shrink-0 bg-control-panel">
  <div class="flex flex-wrap items-center justify-between gap-y-2">
    <div class="flex items-center gap-2">
      <a href="<?= mgmt_url('bag-orders/export') . '?' . http_build_query(array_filter(['start_month' => $startMonth ?? '', 'end_month' => $endMonth ?? '', 'company_idx' => $companyIdx ?? 0, 'bag_code' => $bagCode ?? '', 'receive_type' => $receiveType ?? ''])) ?>" 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 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>
      <a href="<?= mgmt_url('bag-orders/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 p-2 bg-white border-b border-gray-200">
  <!-- GBMS 발주현황: 발주기간은 [시작] ~ [끝] 한 줄 고정, 필터 블록은 가로 나열 후 좁으면 블록 단위로만 줄바꿈 -->
  <form method="GET" action="<?= mgmt_url('bag-orders') ?>" class="flex flex-wrap items-end gap-x-5 gap-y-3 w-full">
    <div class="flex flex-nowrap items-center gap-2 shrink-0">
      <label class="text-sm text-gray-600 whitespace-nowrap">발주 기간</label>
      <select name="start_month" class="border border-gray-300 rounded px-2 py-1 text-sm w-[11.5rem] max-w-[13rem] shrink-0">
        <?php foreach ($bagOrderYmChoices as $ym): ?>
        <option value="<?= esc($ym) ?>" <?= ($startMonth ?? date('Y-m')) === $ym ? 'selected' : '' ?>><?= esc($bagOrderYmLabel($ym)) ?></option>
        <?php endforeach; ?>
      </select>
      <span class="text-sm text-gray-500 select-none">~</span>
      <select name="end_month" class="border border-gray-300 rounded px-2 py-1 text-sm w-[11.5rem] max-w-[13rem] shrink-0">
        <?php foreach ($bagOrderYmChoices as $ym): ?>
        <option value="<?= esc($ym) ?>" <?= ($endMonth ?? date('Y-m')) === $ym ? 'selected' : '' ?>><?= esc($bagOrderYmLabel($ym)) ?></option>
        <?php endforeach; ?>
      </select>
    </div>

    <div class="flex flex-nowrap items-center gap-2 shrink-0">
      <label class="text-sm text-gray-600 whitespace-nowrap">제작 업체</label>
      <select name="company_idx" class="border border-gray-300 rounded px-2 py-1 text-sm w-[11rem] max-w-[14rem]">
        <option value="0">전 체</option>
        <?php foreach (($companyOptions ?? []) as $company): ?>
        <option value="<?= (int) $company->cp_idx ?>" <?= (int) ($companyIdx ?? 0) === (int) $company->cp_idx ? 'selected' : '' ?>>
          <?= esc((string) $company->cp_name) ?>
        </option>
        <?php endforeach; ?>
      </select>
    </div>

    <div class="flex flex-nowrap items-center gap-2 shrink-0">
      <label class="text-sm text-gray-600 whitespace-nowrap">품 명</label>
      <select name="bag_code" class="border border-gray-300 rounded px-2 py-1 text-sm w-[11rem] max-w-[16rem]">
        <option value="">전 체</option>
        <?php foreach (($bagCodeOptions ?? []) as $bag): ?>
        <option value="<?= esc((string) $bag->cd_code) ?>" <?= (string) ($bagCode ?? '') === (string) $bag->cd_code ? 'selected' : '' ?>>
          <?= esc((string) $bag->cd_name) ?>
        </option>
        <?php endforeach; ?>
      </select>
    </div>

    <div class="flex flex-nowrap items-center gap-2 shrink-0">
      <label class="text-sm text-gray-600 whitespace-nowrap">입고 구분</label>
      <select name="receive_type" class="border border-gray-300 rounded px-2 py-1 text-sm w-[8.5rem]">
        <option value="all" <?= ($receiveType ?? 'all') === 'all' ? 'selected' : '' ?>>전 체</option>
        <option value="received" <?= ($receiveType ?? 'all') === 'received' ? 'selected' : '' ?>>입고완료</option>
        <option value="pending" <?= ($receiveType ?? 'all') === 'pending' ? 'selected' : '' ?>>미입고</option>
      </select>
    </div>

    <div class="flex flex-nowrap items-center gap-2 shrink-0 sm:ml-auto">
      <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">조회</button>
      <a href="<?= mgmt_url('bag-orders') ?>" class="text-sm text-gray-500 hover:underline whitespace-nowrap">초기화</a>
    </div>
  </form>
</section>

<div class="bag-order-print-wrap border border-gray-300 rounded-lg p-4 overflow-auto mt-2">
  <table class="bag-order-print-table w-full data-table">
    <thead>
      <tr>
        <th class="w-32 text-center">발주일자</th>
        <th class="min-w-[10rem] text-left">제작 업체</th>
        <th class="min-w-[12rem] text-left">품 명</th>
        <th class="w-28 text-right">발주 수량</th>
        <th class="w-28 text-right">입고 수량</th>
        <th class="w-28 text-right">미입고수량</th>
        <th class="w-32 text-right">발주 금액</th>
        <th class="min-w-[9rem] text-left">입고처</th>
        <th class="min-w-[8rem] text-left">비 고</th>
      </tr>
    </thead>
    <tbody class="text-right">
      <?php $printedGroup = []; ?>
      <?php foreach (($rows ?? []) as $row): ?>
      <?php if (! empty($row['is_subtotal'])): ?>
      <tr class="bg-gray-50 font-semibold">
        <td colspan="3" class="text-center"><?= esc((string) ($row['label'] ?? '소계')) ?></td>
        <td><?= number_format((int) ($row['order_qty'] ?? 0)) ?></td>
        <td><?= number_format((int) ($row['received_qty'] ?? 0)) ?></td>
        <td><?= number_format((int) ($row['pending_qty'] ?? 0)) ?></td>
        <td><?= number_format((float) ($row['amount'] ?? 0)) ?></td>
        <td></td>
        <td></td>
      </tr>
      <?php continue; ?>
      <?php endif; ?>

      <?php
        $boIdx = (int) ($row['bo_idx'] ?? 0);
        $showGroup = ! isset($printedGroup[$boIdx]);
        $rowspan = (int) (($groupRows[$boIdx] ?? 1));
        if ($showGroup) {
            $printedGroup[$boIdx] = true;
        }
      ?>
      <tr>
        <?php if ($showGroup): ?>
        <td class="text-center align-top" rowspan="<?= $rowspan ?>"><?= esc((string) ($row['order_date'] ?? '')) ?></td>
        <td class="text-left pl-2 align-top" rowspan="<?= $rowspan ?>"><?= esc((string) ($row['company_name'] ?? '')) ?></td>
        <?php endif; ?>
        <td class="text-left pl-2"><?= esc((string) ($row['bag_name'] ?? '')) ?></td>
        <td><?= number_format((int) ($row['order_qty'] ?? 0)) ?></td>
        <td><?= number_format((int) ($row['received_qty'] ?? 0)) ?></td>
        <td><?= number_format((int) ($row['pending_qty'] ?? 0)) ?></td>
        <td><?= number_format((float) ($row['amount'] ?? 0)) ?></td>
        <td class="text-left pl-2"><?= esc((string) ($row['agency_name'] ?? '')) ?></td>
        <td></td>
      </tr>
      <?php endforeach; ?>

      <?php if (empty($rows ?? [])): ?>
      <tr><td colspan="9" class="text-center text-gray-400 py-6">조회 조건에 해당하는 발주 내역이 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
    <tfoot>
      <tr class="bg-gray-100 font-bold">
        <td colspan="3" class="text-center">총계</td>
        <td class="text-right"><?= number_format((int) ($grandTotals['order_qty'] ?? 0)) ?></td>
        <td class="text-right"><?= number_format((int) ($grandTotals['received_qty'] ?? 0)) ?></td>
        <td class="text-right"><?= number_format((int) ($grandTotals['pending_qty'] ?? 0)) ?></td>
        <td class="text-right"><?= number_format((float) ($grandTotals['amount'] ?? 0)) ?></td>
        <td></td>
        <td></td>
      </tr>
    </tfoot>
  </table>
</div>

<style>
@media print {
  #debug-icon,
  #debug-bar,
  #debug-bar-contents,
  #debug-toolbar,
  .debug-toolbar,
  .ci-debug-toolbar,
  [id^='debug-bar-'],
  [id^='debug-icon'],
  [class*='debug-toolbar'] {
    display: none !important;
    visibility: hidden !important;
  }

  .bag-order-print-wrap {
    overflow: visible !important;
    border: none !important;
    margin-top: 0 !important;
  }

  .bag-order-print-table {
    width: 100% !important;
    table-layout: auto !important;
  }

  .bag-order-print-table th,
  .bag-order-print-table td {
    white-space: nowrap !important;
    word-break: keep-all !important;
    overflow-wrap: normal !important;
    font-size: 10px !important;
    padding: 2px 3px !important;
  }
}
</style>
