document.addEventListener('DOMContentLoaded',()=>{const b=document.getElementById('outlookBtn');if(!b)return;b.addEventListener('click',async()=>{const box=document.getElementById('outlookResult');b.disabled=true;b.textContent='Preparing…';box.innerHTML='';try{const fd=new FormData();fd.append('order_id',b.dataset.order);const r=await fetch('outlook_token.php',{method:'POST',body:fd,credentials:'same-origin'});const d=await r.json();if(!r.ok||!d.ok)throw new Error(d.message||'Could not create code.');box.innerHTML=`<div class="alert alert-success mb-0"><strong>Outlook code:</strong><div style="font-size:28px;letter-spacing:.16em;font-weight:900;margin:8px 0">${d.token}</div>Open the RoomZ add-in in the Outlook reply, enter this code, and click Load prepared booking. It expires in ${d.expires_minutes} minutes and works once.</div>`;}catch(e){box.innerHTML=`<div class="alert alert-danger mb-0">${e.message}</div>`;}finally{b.disabled=false;b.textContent='Prepare Outlook Reply';}});});