<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\PosService;
PosService::requireAccess();
$id = (int)($_GET['id'] ?? 0);
$data = $id ? PosService::receipt($id) : null;
if (!$data) { http_response_code(404); echo 'Receipt not found'; exit; }
$sale = $data['sale']; $items = $data['items']; $payments = $data['payments'];
?>
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Receipt <?= e($sale['sale_no']) ?></title><link rel="stylesheet" href="<?= asset('css/pos.css') ?>"></head><body class="receipt-body">
<div class="receipt-80">
  <div class="text-center"><img src="<?= asset('brand/logo.png') ?>" style="max-width:160px" alt="logo"><h2><?= e($sale['shop_name'] ?? BRAND_NAME) ?></h2><p>POS Receipt<br><?= e($sale['sale_no']) ?><br><?= e($sale['created_at']) ?></p></div>
  <hr><p>Cashier: <?= e($sale['cashier_name'] ?? '') ?></p>
  <table class="receipt-table"><tbody><?php foreach($items as $item): ?><tr><td><?= e($item['product_name']) ?><br><small><?= e($item['sku']) ?> x <?= e($item['qty']) ?></small></td><td class="text-right"><?= money($item['line_total']) ?></td></tr><?php endforeach; ?></tbody></table>
  <hr><div class="receipt-row"><span>Subtotal</span><strong><?= money($sale['subtotal']) ?></strong></div><div class="receipt-row"><span>Discount</span><strong><?= money($sale['discount_amount']) ?></strong></div><div class="receipt-row"><span>VAT/TAX</span><strong><?= money($sale['tax_amount']) ?></strong></div><div class="receipt-row grand"><span>Total</span><strong><?= money($sale['total_amount']) ?></strong></div><div class="receipt-row"><span>Paid</span><strong><?= money($sale['paid_amount']) ?></strong></div><div class="receipt-row"><span>Change</span><strong><?= money($sale['change_amount']) ?></strong></div>
  <hr><p>Payment: <?php foreach($payments as $p){ echo e($p['payment_method']).' '.money($p['amount']).' '; } ?></p><div class="receipt-barcode">*<?= e($sale['sale_no']) ?>*</div><p class="text-center">Thank you for shopping with Purchase Haat.</p>
</div>
<script>window.addEventListener('load',function(){if(new URLSearchParams(location.search).get('print')==='1')window.print();});</script>
</body></html>
