<!-- Notification popup for completed/cancelled orders -->
<div id="orderPopup" style="display:none;position:fixed;right:20px;bottom:20px;z-index:99999;max-width:340px;background:#fff;border-radius:14px;box-shadow:0 20px 40px rgba(0,0,0,.25);overflow:hidden;font-family:'Hind Siliguri',sans-serif;animation:slideUp .3s ease">
  <div id="popupBar" style="padding:10px 14px;color:#fff;font-weight:700;display:flex;justify-content:space-between;align-items:center">
    <span id="popupTitle"></span>
    <span style="cursor:pointer;font-size:20px;line-height:1" onclick="closePopup()">&times;</span>
  </div>
  <div style="padding:14px">
    <p id="popupBody" style="margin:0 0 12px;color:#334155;font-size:14px"></p>
    <a id="popupLink" href="#" style="display:inline-block;padding:8px 14px;background:#6366f1;color:#fff;border-radius:8px;text-decoration:none;font-weight:600;font-size:13px">বিস্তারিত দেখুন →</a>
  </div>
</div>
<style>@keyframes slideUp{from{transform:translateY(20px);opacity:0}to{transform:translateY(0);opacity:1}}</style>
<script>
(function(){
  var lastShown = null;
  async function poll(){
    try{
      const r = await fetch('<?=defined("BASE_URL")?BASE_URL:""?>/user/notifications.php', {credentials:'same-origin'});
      const j = await r.json();
      if(j.items && j.items.length){
        const it = j.items[0];
        if(lastShown === it.id) return;
        lastShown = it.id;
        showPopup(it);
      }
    }catch(e){}
  }
  function showPopup(it){
    const isCancel = it.status === 'cancelled';
    document.getElementById('popupBar').style.background = isCancel ? 'linear-gradient(90deg,#dc2626,#b91c1c)' : 'linear-gradient(90deg,#10b981,#059669)';
    document.getElementById('popupTitle').textContent = isCancel ? '❌ অর্ডার বাতিল' : '✅ অর্ডার সম্পাদন';
    document.getElementById('popupBody').textContent = isCancel
      ? ('আপনার অর্ডার #' + it.id + ' বাতিল করা হয়েছে' + (it.cancel_reason ? (' — ' + it.cancel_reason) : ''))
      : ('আপনার অর্ডার #' + it.id + ' সম্পাদন সম্পন্ন হয়েছে। ডাউনলোড / কপি করুন।');
    document.getElementById('popupLink').href = '<?=defined("BASE_URL")?BASE_URL:""?>/user/order_view.php?id=' + it.id;
    document.getElementById('orderPopup').style.display = 'block';
    // mark seen so it doesn't re-appear
    fetch('<?=defined("BASE_URL")?BASE_URL:""?>/user/notifications.php?mark=' + it.id, {credentials:'same-origin'}).catch(()=>{});
  }
  window.closePopup = function(){ document.getElementById('orderPopup').style.display='none'; };
  poll();
  setInterval(poll, 15000);
})();
</script>
</div></div></body></html>
