<?php
declare(strict_types=1);
/**
 * 입금자 영수증 출력용 (독립 인쇄 화면).
 * @var object $order
 * @var list<object> $items
 * @var array|null $shop
 * @var string $lgName
 */
$nf = static fn ($n): string => number_format((int) $n);
$paid = (int) ($order->so_paid ?? 0) === 1;
$addr = $shop ? trim((string) ($shop['ds_addr'] ?? '') . ' ' . (string) ($shop['ds_addr_detail'] ?? '')) : '';
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>입금 영수증 #<?= (int) $order->so_idx ?></title>
<style>
  * { box-sizing: border-box; }
  body { font-family: 'Malgun Gothic', 'Noto Sans KR', sans-serif; color: #111; margin: 0; padding: 24px; }
  .receipt { max-width: 620px; margin: 0 auto; }
  h1 { text-align: center; font-size: 1.5rem; letter-spacing: 8px; margin: 0 0 4px; }
  .sub { text-align: center; color: #555; font-size: .85rem; margin-bottom: 18px; }
  .meta { width: 100%; border-collapse: collapse; margin-bottom: 14px; font-size: .85rem; }
  .meta th { text-align: left; width: 90px; color: #555; padding: 3px 6px; vertical-align: top; }
  .meta td { padding: 3px 6px; }
  table.items { width: 100%; border-collapse: collapse; font-size: .82rem; }
  table.items th, table.items td { border: 1px solid #bbb; padding: 5px 7px; }
  table.items th { background: #f1f3f5; }
  table.items td.num { text-align: right; }
  tfoot td { font-weight: bold; background: #fafafa; }
  .paidbox { margin-top: 16px; text-align: center; font-size: 1rem; font-weight: bold; }
  .paid { color: #c0392b; }
  .unpaid { color: #888; }
  .foot { margin-top: 26px; text-align: center; color: #666; font-size: .75rem; }
  .noprint { text-align: center; margin: 18px 0; }
  @media print { .noprint { display: none; } body { padding: 0; } }
</style>
</head>
<body>
  <div class="receipt">
    <h1>영 수 증</h1>
    <div class="sub"><?= esc($lgName) ?> 종량제 봉투 · 전화 접수 입금 영수증</div>

    <table class="meta">
      <tr><th>접수번호</th><td>#<?= (int) $order->so_idx ?></td><th>접수일</th><td><?= esc((string) ($order->so_order_date ?? '')) ?></td></tr>
      <tr><th>판매소</th><td><?= esc($shop['ds_name'] ?? (string) ($order->so_ds_name ?? '')) ?> <?= $shop ? '(' . esc((string) $shop['ds_shop_no']) . ')' : '' ?></td><th>배달일</th><td><?= esc((string) ($order->so_delivery_date ?? '')) ?></td></tr>
      <tr><th>담당자</th><td><?= esc($shop['ds_rep_name'] ?? '') ?></td><th>전화</th><td><?= esc($shop['ds_tel'] ?? '') ?></td></tr>
      <tr><th>주소</th><td colspan="3"><?= esc($addr) ?></td></tr>
      <tr><th>결제수단</th><td><?= esc((string) ($order->so_payment_type ?? '')) ?></td><th>입금</th><td><?= $paid ? '입금완료' : '미입금' ?></td></tr>
    </table>

    <table class="items">
      <thead>
        <tr><th>품목</th><th>단가</th><th>수량</th><th>금액</th></tr>
      </thead>
      <tbody>
        <?php $sumQty = 0; $sumAmt = 0; foreach (($items ?? []) as $it): ?>
        <?php $q = (int) ($it->soi_qty ?? 0); $a = (int) ($it->soi_amount ?? 0); $sumQty += $q; $sumAmt += $a; ?>
        <tr>
          <td><?= esc((string) ($it->soi_bag_code ?? '')) ?> <?= esc((string) ($it->soi_bag_name ?? '')) ?></td>
          <td class="num"><?= $nf($it->soi_unit_price ?? 0) ?></td>
          <td class="num"><?= $nf($q) ?></td>
          <td class="num"><?= $nf($a) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($items)): ?>
        <tr><td colspan="4" style="text-align:center;color:#999;">품목 내역이 없습니다.</td></tr>
        <?php endif; ?>
      </tbody>
      <tfoot>
        <tr><td>합계</td><td></td><td class="num"><?= $nf($sumQty) ?></td><td class="num"><?= $nf($sumAmt) ?></td></tr>
      </tfoot>
    </table>

    <div class="paidbox <?= $paid ? 'paid' : 'unpaid' ?>"><?= $paid ? '● 입금 완료' : '미입금' ?></div>

    <div class="foot">출력일: <?= date('Y-m-d H:i') ?></div>

    <div class="noprint">
      <button type="button" onclick="window.print()" style="padding:8px 18px;font-size:.9rem;cursor:pointer;">인쇄</button>
      <button type="button" onclick="window.close()" style="padding:8px 18px;font-size:.9rem;cursor:pointer;margin-left:6px;">닫기</button>
    </div>
  </div>
  <script>window.addEventListener('load', function () { setTimeout(function () { window.print(); }, 300); });</script>
</body>
</html>
