<?php $sym = setting('currency_symbol','Rs.'); ?>
<div class="container-fluid py-3">
  <div class="card shadow-sm"><div class="card-header d-flex justify-content-between align-items-center"><strong>Barcode Label Sheet</strong><button type="button" class="btn btn-primary btn-sm" id="printSelected"><i class="bi bi-printer"></i> Print Selected</button></div>
  <div class="card-body">
    <form method="get" class="row g-2 mb-3"><div class="col-md-5"><input class="form-control" name="search" value="<?=e($search)?>" placeholder="Search product or code"></div><div class="col-md-4"><select class="form-select" name="category"><option value="0">All categories</option><?php foreach($categories as $c):?><option value="<?=$c['id']?>" <?=((int)$catId===(int)$c['id'])?'selected':''?>><?=e($c['category_name'])?></option><?php endforeach;?></select></div><div class="col-md-3"><button class="btn btn-outline-primary w-100">Filter</button></div></form>
    <div class="table-responsive"><table class="table table-sm align-middle"><thead><tr><th><input type="checkbox" id="all"></th><th>Code</th><th>Product</th><th>Barcode</th><th class="text-end">Price</th></tr></thead><tbody><?php foreach($products as $p):?><tr><td><input class="pick" type="checkbox" value="<?=$p['id']?>"></td><td><?=e($p['product_code'])?></td><td><?=e($p['product_name'])?></td><td><code><?=e($p['barcode'])?></code></td><td class="text-end"><?=e($sym)?> <?=number_format((float)$p['selling_price'],2)?></td></tr><?php endforeach;?><?php if(!$products):?><tr><td colspan="5" class="text-center text-muted py-4">No products found.</td></tr><?php endif;?></tbody></table></div>
  </div></div>
</div>
<script>
document.getElementById('all')?.addEventListener('change',e=>document.querySelectorAll('.pick').forEach(x=>x.checked=e.target.checked));
document.getElementById('printSelected')?.addEventListener('click',()=>{const ids=[...document.querySelectorAll('.pick:checked')].map(x=>x.value);if(!ids.length){alert('Select at least one product.');return;}window.open('<?=APP_URL?>/products/barcode-labels/print?ids='+encodeURIComponent(ids.join(',')),'_blank');});
</script>
