<?php
declare(strict_types=1);
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Auth;use App\Core\CodeGenerator;use App\Core\Database;
Auth::requireRole(['admin','vendor']);$id=(int)($_GET['id']??0);$params=[$id];$sql='SELECT p.*,v.shop_name FROM products p LEFT JOIN vendors v ON v.id=p.vendor_id WHERE p.id=?';
if(Auth::role()==='vendor'){$vendor=Database::safeFetchOne('SELECT id FROM vendors WHERE user_id=? LIMIT 1',[Auth::id()]);$sql.=' AND p.vendor_id=?';$params[]=(int)($vendor['id']??0);}$sql.=' LIMIT 1';$p=Database::safeFetchOne($sql,$params);
if(!$p){http_response_code(404);exit('Product not found or access denied.');}$payload=CodeGenerator::productPayload($p);
?><!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Print Product Label</title><link rel="stylesheet" href="<?= asset('css/bootstrap.min.css') ?>"><link rel="stylesheet" href="<?= asset('css/purchase-haat.css') ?>"></head><body class="p-4"><div class="barcode-print shadow"><div class="d-flex justify-content-between align-items-center"><img src="<?= asset('brand/logo.png') ?>" height="58" alt="logo"><button class="btn btn-primary" onclick="print()">Print</button></div><hr><h4><?= e($p['name']) ?></h4><p>SKU: <?= e($p['sku']) ?> | Product ID: <?= (int)$p['id'] ?> | Price: <?= money($p['sale_price']?:$p['regular_price']) ?> | Vendor: <?= e($p['shop_name']??'') ?> | Stock: <?= (int)$p['stock'] ?></p><div class="row g-4"><div class="col-md-7 text-center"><h6>Barcode</h6><?= CodeGenerator::barcodeSvg($p['barcode']?:($p['sku']?:('PH-'.$p['id'])),90) ?></div><div class="col-md-5 text-center qr-box"><h6>QR Payload</h6><?= CodeGenerator::qrSvg($payload,210) ?></div></div></div></body></html>
