(function(){
    document.addEventListener('click',function(e){
        var row=e.target.closest('#tbl-orders tbody tr');
        if(!row)return;
        var id=row.cells[0].textContent;
        loadOrderDetail(id);
    });
    function loadOrderDetail(id){
        fetch('api/search.php?scope=orders&q='+id).then(r=>r.json()).then(d=>{
            if(d.success&&d.data.length){
                document.getElementById('order-list').style.display='none';
                document.getElementById('order-detail').style.display='block';
                document.getElementById('order-detail').dispatchEvent(new CustomEvent('load-order',{detail:d.data[0]}));
            }
        });
    }
    var search=document.getElementById('order-search');
    if(search){var t;search.addEventListener('input',function(){clearTimeout(t);t=setTimeout(function(){fetch('api/search.php?scope=orders&q='+encodeURIComponent(search.value)).then(r=>r.json()).then(d=>{void 0;});},300);});}
})();
