// One lookup form for shortcuts and saved order cards; no draft or chat ownership changes. var OrderLookup = { create(options){ const overlay=document.getElementById('order-lookup'); const form=document.getElementById('order-lookup-form'); const input=document.getElementById('order-reference'); const label=document.getElementById('order-reference-label'); const title=document.getElementById('order-lookup-title'); const submit=document.getElementById('order-lookup-submit'); const cancel=document.getElementById('order-lookup-cancel'); const copy=document.getElementById('order-reference-copy'); const error=document.getElementById('order-lookup-error'); let kind='inbound', opener=null, busy=false, copying=false, copyState='', version=0, timer=null; overlay.hidden=true; function valid(value){ return /^[A-Za-z0-9][A-Za-z0-9_-]{5,63}$/.test(value) && (value.match(/[0-9]/g) || []).length >= 3; } function status(message){ error.textContent=message || ''; error.hidden=!message; } function render(){ title.textContent=options.text(kind === 'inbound' ? 'lookupTitle' : 'lookupOrderTitle'); label.textContent=options.text(kind === 'inbound' ? 'order_inbound' : 'orderNo'); input.placeholder=options.text(kind === 'inbound' ? 'lookupPlaceholder' : 'lookupOrderPlaceholder'); submit.textContent=options.text(busy ? 'lookupSending' : 'lookupSubmit'); cancel.textContent=options.text('customerCancel'); copy.textContent=options.text(copyState || 'copy'); submit.disabled=busy; cancel.disabled=busy; input.disabled=busy; copy.disabled=busy || copying || !valid(input.value.trim()); } function clearTimer(){ if(timer !== null) clearTimeout(timer); timer=null; } function close(){ version+=1;clearTimer();overlay.hidden=true; busy=false;copying=false;copyState='';input.value='';status('');render(); if(opener && opener.isConnected) opener.focus({preventScroll:true}); opener=null; } function open(reference, referenceKind, source){ version+=1;clearTimer();kind=referenceKind === 'order' ? 'order' : 'inbound'; busy=false;copying=false;copyState=''; opener=source || document.activeElement;input.value=reference || '';status('');render();overlay.hidden=false; if(options.onOpen) options.onOpen(); input.focus({preventScroll:true}); } input.addEventListener('input',function(){ clearTimer();copyState='';status('');render(); }); copy.onclick=async function(){ const reference=input.value.trim(), token=version; if(busy || copying || !valid(reference)) return; copying=true;copyState='';status('');render(); const copied=await SupportUI.copyText(reference); if(token !== version) return; copying=false; if(input.value.trim() === reference){ copyState=copied ? 'copied' : 'copyFailed'; if(!copied) status(options.text('copyFailed')); timer=setTimeout(function(){copyState='';timer=null;render();},copied ? 1200 : 3000); } render(); }; form.addEventListener('submit',async function(event){ event.preventDefault(); if(busy) return; const reference=input.value.trim(), token=version; if(!valid(reference)){status(options.text('lookupInvalid'));input.focus({preventScroll:true});return;} const blocked=options.canQuery(); if(blocked){status(blocked);return;} busy=true;status('');render(); try{ const accepted=await options.query(reference,kind); if(token !== version) return; if(accepted) close();else status(options.text('lookupFailed')); }catch(error){if(token === version) status(options.text('lookupFailed'));} finally{if(token === version){busy=false;render();}} }); cancel.onclick=function(){if(!busy) close();}; overlay.addEventListener('click',function(event){if(event.target === overlay && !busy) close();}); overlay.addEventListener('keydown',function(event){ if(event.key === 'Escape' && !busy){event.preventDefault();close();return;} if(event.key === 'Tab'){ const controls=[input,copy,cancel,submit].filter(control=>!control.disabled); event.preventDefault(); if(controls.length){ const index=controls.indexOf(document.activeElement); controls[(index+(event.shiftKey ? -1 : 1)+controls.length)%controls.length].focus({preventScroll:true}); } } }); render(); return {open:open,cancel:close,render:render,viewport:function(height,top){ overlay.style.height=height || '';overlay.style.top=top || '';overlay.style.bottom=height ? 'auto' : ''; }}; } };