<?= view('components/print_header', ['printTitle' => '봉투 입고 현황']) ?>
<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel no-print">
  <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">
      <a href="<?= base_url('bag/receiving/status/export') . '?' . http_build_query(array_filter(['start_date' => $startDate ?? '', 'end_date' => $endDate ?? '', 'company_idx' => $companyIdx ?? 0, 'bag_code' => $bagCode ?? '', 'receive_type' => $receiveType ?? ''])) ?>" class="border border-btn-excel-border text-btn-excel-text px-3 py-1 rounded-sm text-sm hover:bg-green-50">엑셀저장</a>
      <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">인쇄</button>
      <a href="<?= base_url('bag/receiving/scanner') ?>" class="border border-gray-300 text-gray-700 px-3 py-1 rounded-sm text-sm hover:bg-gray-50">입고 처리</a>
    </div>
  </div>
</section>

<section class="p-2 bg-white border-b border-gray-200 no-print">
  <form method="get" action="<?= base_url('bag/receiving/status') ?>" class="flex flex-wrap items-center gap-2">
    <label class="text-sm text-gray-600">입고 기간</label>
    <input type="date" name="start_date" value="<?= esc((string) ($startDate ?? '')) ?>" class="border border-gray-300 rounded px-2 py-1 text-sm" />
    <span class="text-sm text-gray-500">~</span>
    <input type="date" name="end_date" value="<?= esc((string) ($endDate ?? '')) ?>" class="border border-gray-300 rounded px-2 py-1 text-sm" />

    <label class="text-sm text-gray-600 ml-2">제작 업체</label>
    <select name="company_idx" class="border border-gray-300 rounded px-2 py-1 text-sm w-52">
      <option value="0">전 체</option>
      <?php foreach (($companies ?? []) 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>

    <label class="text-sm text-gray-600">품명</label>
    <select name="bag_code" class="border border-gray-300 rounded px-2 py-1 text-sm w-44">
      <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>

    <label class="text-sm text-gray-600">입고 구분</label>
    <select name="receive_type" class="border border-gray-300 rounded px-2 py-1 text-sm w-36">
      <option value="all" <?= ($receiveType ?? 'all') === 'all' ? 'selected' : '' ?>>전 체</option>
      <option value="completed" <?= ($receiveType ?? 'all') === 'completed' ? 'selected' : '' ?>>완료</option>
      <option value="pending" <?= ($receiveType ?? 'all') === 'pending' ? 'selected' : '' ?>>미완료</option>
    </select>

    <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">조회</button>
    <a href="<?= base_url('bag/receiving/status') ?>" class="text-sm text-gray-500 hover:underline">초기화</a>
  </form>
</section>

<div class="border border-gray-300 rounded-lg p-4 overflow-auto mt-2 receiving-status-print-wrap">
  <table class="w-full data-table text-sm receiving-status-print-table">
    <thead>
      <tr>
        <th class="text-center">입고일자</th>
        <th>품명</th>
        <th class="text-right">입고수량</th>
        <th class="text-center">발주일자</th>
        <th class="text-right">발주수량</th>
        <th class="text-center">발주번호</th>
        <th>제작업체</th>
        <th class="text-center">입고여부</th>
        <th>입고처</th>
        <th>비고</th>
      </tr>
    </thead>
    <tbody>
      <?php
        $prevDate = null;
        $runningSum = 0;
      ?>
      <?php foreach (($rows ?? []) as $i => $row): ?>
      <?php
        $d = (string) ($row['display_date'] ?? '');
        if ($prevDate !== null && $d !== $prevDate):
      ?>
      <tr class="bg-gray-50 font-semibold">
        <td colspan="2" class="text-center">소 계</td>
        <td class="text-right"><?= number_format($runningSum) ?></td>
        <td colspan="7"></td>
      </tr>
      <?php
          $runningSum = 0;
        endif;
        $runningSum += (int) ($row['received_qty_sheet'] ?? 0);
        $prevDate = $d;
      ?>
      <tr>
        <td class="text-center"><?= esc($d) ?></td>
        <td class="text-left pl-2"><?= esc((string) ($row['bag_name'] ?? '')) ?></td>
        <td class="text-right"><?= number_format((int) ($row['received_qty_sheet'] ?? 0)) ?></td>
        <td class="text-center"><?= esc((string) ($row['order_date'] ?? '')) ?></td>
        <td class="text-right"><?= number_format((int) ($row['order_qty_sheet'] ?? 0)) ?></td>
        <td class="text-center"><?= esc((string) ($row['order_no'] ?? '')) ?></td>
        <td class="text-left pl-2"><?= esc((string) ($row['company_name'] ?? '')) ?></td>
        <td class="text-center <?= ($row['receive_status_label'] ?? '') === '완료' ? 'text-red-600 font-bold' : 'text-blue-600 font-bold' ?>"><?= esc((string) ($row['receive_status_label'] ?? '')) ?></td>
        <td class="text-left pl-2"><?= esc((string) ($row['agency_name'] ?? '')) ?></td>
        <td></td>
      </tr>
      <?php endforeach; ?>

      <?php if (! empty($rows ?? [])): ?>
      <tr class="bg-gray-50 font-semibold">
        <td colspan="2" class="text-center">소 계</td>
        <td class="text-right"><?= number_format($runningSum) ?></td>
        <td colspan="7"></td>
      </tr>
      <?php endif; ?>

      <?php if (empty($rows ?? [])): ?>
      <tr><td colspan="10" class="text-center text-gray-400 py-4">조회 결과가 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
    <tfoot>
      <tr class="bg-gray-100 font-bold">
        <td colspan="2" class="text-center">합 계</td>
        <td class="text-right"><?= number_format((int) ($grandTotalReceive ?? 0)) ?></td>
        <td colspan="7"></td>
      </tr>
    </tfoot>
  </table>
</div>

<style>
@media print {
  .receiving-status-print-wrap {
    overflow: visible !important;
    border: none !important;
  }
  .receiving-status-print-table th,
  .receiving-status-print-table td {
    white-space: nowrap !important;
    font-size: 10px !important;
    padding: 2px 3px !important;
  }
}
</style>
