(function(){
  function api(path){var base=(window.PH_BASE||'/').replace(/\/$/,'');return base+'/'+path.replace(/^\//,'');}
  function notify(message,kind){var box=document.getElementById('phToastContainer');if(!box){alert(message);return;}var el=document.createElement('div');el.className='toast align-items-center text-bg-'+(kind||'primary')+' border-0';el.innerHTML='<div class="d-flex"><div class="toast-body"></div><button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button></div>';el.querySelector('.toast-body').textContent=message;box.appendChild(el);var toast=bootstrap.Toast.getOrCreateInstance(el,{delay:2500});el.addEventListener('hidden.bs.toast',function(){el.remove();});toast.show();}
  function post(path,data){data=data||{};data.csrf_token=window.PH_CSRF||'';return fetch(api(path),{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','X-CSRF-Token':window.PH_CSRF||''},body:new URLSearchParams(data)}).then(function(r){return r.json().then(function(j){if(!r.ok&&!j.message)j.message='Request failed.';return j;});});}
  window.phAddCart=function(productId,qty,variantId){post('/api/cart.php',{action:'add',product_id:productId,qty:qty||1,variant_id:variantId||''}).then(function(res){if(res.ok){var c=document.getElementById('cartCount');if(c)c.textContent=res.summary.count;notify(res.message||'Added to cart.','success');}else notify(res.message||'Could not add product.','danger');}).catch(function(){notify('Network error.','danger');});};
  window.phToggleList=function(type,productId,button,reload){post('/api/'+type+'.php',{action:reload?'remove':'toggle',product_id:productId}).then(function(res){notify(res.message||'Updated.',res.ok?'success':'danger');if(res.ok&&reload)location.reload();if(res.ok&&button&&!reload)button.classList.toggle('active',!!res.added);});};
  window.phOrderStatus=function(orderId,status){post('/admin/ajax/order-status.php',{order_id:orderId,status:status}).then(function(res){if(res.ok)location.reload();else notify(res.message||'Update failed.','danger');});};
  document.addEventListener('DOMContentLoaded',function(){
    var box=document.querySelector('[data-dashboard-stats]');if(box)fetch(api('/admin/ajax/stats.php')).then(function(r){return r.json();}).then(function(res){if(!res.ok)return;Object.keys(res.stats).forEach(function(k){var el=document.querySelector('[data-stat="'+k+'"]');if(el)el.textContent=res.stats[k];});}).catch(function(){});
    var input=document.getElementById('globalProductSearch'),results=document.getElementById('globalSearchResults'),timer;
    if(input&&results){input.addEventListener('input',function(){clearTimeout(timer);var q=input.value.trim();if(q.length<2){results.classList.add('d-none');results.innerHTML='';return;}timer=setTimeout(function(){fetch(api('/api/product-search.php?q='+encodeURIComponent(q))).then(function(r){return r.json();}).then(function(res){results.innerHTML='';(res.results||[]).slice(0,8).forEach(function(p){var a=document.createElement('a');a.className='list-group-item list-group-item-action';a.href=api('/product/'+p.slug);a.textContent=p.name+' — '+(p.sku||'');results.appendChild(a);});results.classList.toggle('d-none',!results.children.length);});},250);});document.addEventListener('click',function(e){if(!results.contains(e.target)&&e.target!==input)results.classList.add('d-none');});}
  });
})();
