var ScannerEngine={active:false,init:function(){this.active=true;document.addEventListener("keydown",this.handleScan.bind(this));},buffer:"",timer:null,handleScan:function(e){if(!this.active)return;clearTimeout(this.timer);if(e.key==="Enter"&&this.buffer.length>=6){this.processCode(this.buffer);this.buffer="";}else if(e.key.length===1){this.buffer+=e.key;}this.timer=setTimeout(function(){this.buffer="";}.bind(this),100);},processCode:function(code){fetch("api/search.php?scope=orders&q="+encodeURIComponent(code)).then(r=>r.json()).then(d=>{if(d.success&&d.data.length){document.dispatchEvent(new CustomEvent("order-scanned",{detail:d.data[0]}));}else{alert("No order found for: "+code);}});},stop:function(){this.active=false;}};
