<?= view('components/print_header', ['printTitle' => 'LOT 수불 조회']) ?>
<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">LOT 수불 조회</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="<?= mgmt_url('reports/lot-flow') ?>" class="flex flex-wrap items-center gap-2">
    <label class="text-sm text-gray-600">LOT 번호</label>
    <input type="text" name="lot_no" value="<?= esc($lotNo ?? '') ?>" placeholder="LOT-YYYYMMDD-XXXXXX" class="border border-gray-300 rounded px-2 py-1 text-sm w-64"/>
    <button type="submit" class="bg-btn-search text-white px-4 py-1 rounded-sm text-sm">조회</button>
  </form>
</section>

<?php if ($lotNo !== '' && $order): ?>
<!-- 발주 정보 -->
<div class="border border-gray-300 p-3 mt-2 bg-gray-50">
  <h3 class="text-sm font-bold text-gray-700 mb-2">발주 정보</h3>
  <div class="grid grid-cols-2 md:grid-cols-4 gap-2 text-sm">
    <div><span class="text-gray-500">LOT번호:</span> <span class="font-mono"><?= esc($order->bo_lot_no) ?></span></div>
    <div><span class="text-gray-500">발주일:</span> <?= esc($order->bo_order_date) ?></div>
    <div><span class="text-gray-500">상태:</span>
      <?php $statusMap = ['normal' => '정상', 'cancelled' => '취소', 'deleted' => '삭제']; ?>
      <?= esc($statusMap[$order->bo_status] ?? $order->bo_status) ?>
    </div>
    <div><span class="text-gray-500">등록일:</span> <?= esc($order->bo_regdate) ?></div>
  </div>
</div>

<!-- 발주 품목 -->
<h3 class="text-sm font-bold text-gray-700 mt-3 mb-1">발주 품목</h3>
<div class="border border-gray-300 overflow-auto">
  <table class="w-full data-table">
    <thead>
      <tr>
        <th>봉투코드</th>
        <th>봉투명</th>
        <th>발주수량(박스)</th>
        <th>발주수량(매)</th>
        <th>단가</th>
        <th>금액</th>
      </tr>
    </thead>
    <tbody class="text-right">
      <?php foreach ($items as $item): ?>
      <tr>
        <td class="text-center font-mono"><?= esc($item->boi_bag_code) ?></td>
        <td class="text-left pl-2"><?= esc($item->boi_bag_name) ?></td>
        <td><?= number_format((int) $item->boi_qty_box) ?></td>
        <td><?= number_format((int) $item->boi_qty_sheet) ?></td>
        <td><?= number_format((int) $item->boi_unit_price) ?></td>
        <td><?= number_format((int) $item->boi_amount) ?></td>
      </tr>
      <?php endforeach; ?>
      <?php if (empty($items)): ?>
      <tr><td colspan="6" class="text-center text-gray-400 py-4">품목이 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<!-- 입고 내역 -->
<h3 class="text-sm font-bold text-gray-700 mt-3 mb-1">입고 내역</h3>
<div class="border border-gray-300 overflow-auto">
  <table class="w-full data-table">
    <thead>
      <tr>
        <th>입고일</th>
        <th>봉투코드</th>
        <th>봉투명</th>
        <th>입고수량(박스)</th>
        <th>입고수량(매)</th>
        <th>납품자</th>
      </tr>
    </thead>
    <tbody class="text-right">
      <?php foreach ($receivings as $recv): ?>
      <tr>
        <td class="text-center"><?= esc($recv->br_receive_date) ?></td>
        <td class="text-center font-mono"><?= esc($recv->br_bag_code) ?></td>
        <td class="text-left pl-2"><?= esc($recv->br_bag_name) ?></td>
        <td><?= number_format((int) $recv->br_qty_box) ?></td>
        <td><?= number_format((int) $recv->br_qty_sheet) ?></td>
        <td class="text-left pl-2"><?= esc($recv->br_sender_name ?? '') ?></td>
      </tr>
      <?php endforeach; ?>
      <?php if (empty($receivings)): ?>
      <tr><td colspan="6" class="text-center text-gray-400 py-4">입고 내역이 없습니다.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<?php elseif ($lotNo !== '' && !$order): ?>
<div class="border border-gray-300 p-4 mt-2 text-center text-gray-400">해당 LOT 번호의 발주를 찾을 수 없습니다.</div>
<?php else: ?>
<div class="border border-gray-300 p-4 mt-2 text-center text-gray-400">LOT 번호를 입력하고 조회해 주세요.</div>
<?php endif; ?>
