(()=>{const boot=()=>{const input=document.getElementById('pImages'),modal=document.getElementById('prodModal');if(!input||!modal||!window.DTProducts||typeof api!=='function')return false;const originalOpen=window.DTProducts.openProduct;window.DTProducts.openProduct=(p)=>{modal.dataset.product=p?.id||'';return originalOpen(p)};input.onchange=async()=>{const id=modal.dataset.product,files=[...input.files||[]];if(!id||!files.length)return;if(files.length>8){alert('Maximum 8 images per upload.');input.value='';return}try{await api('/v1/admin/context');const fd=new FormData();files.forEach(f=>fd.append('files',f));const r=await fetch('https://api.dosatoppings.in/v1/admin/catalog/products/'+encodeURIComponent(id)+'/images',{method:'POST',headers:{Authorization:'Bearer '+state.access},body:fd});const text=await r.text();let d={};try{d=JSON.parse(text)}catch{}if(!r.ok)throw new Error(d.error||text||'Image upload failed');const status=document.getElementById('pStatus');if(status){status.textContent=`${(d.images||[]).length} image${(d.images||[]).length===1?'':'s'} uploaded.`;status.className='status'}const gallery=document.getElementById('pGallery');if(gallery)gallery.innerHTML='';window.DTProducts.openProduct({id,name:document.getElementById('pName')?.value||'',image_url:document.getElementById('pImage')?.value||'',slug:document.getElementById('pSlug')?.value||'',category_id:document.getElementById('pCategory')?.value||'',short_description:document.getElementById('pShort')?.value||'',description:document.getElementById('pDesc')?.value||'',is_published:document.getElementById('pPublished')?.checked});}catch(e){const s=document.getElementById('pStatus');if(s){s.textContent=e.message||'Image upload failed';s.className='status error'}}finally{input.value=''}};return true};let n=0;const timer=setInterval(()=>{if(boot()||++n>40)clearInterval(timer)},250)})();