document.addEventListener('DOMContentLoaded',()=>{const base=window.APP_BASE||'',ids={BTC:'bitcoin',ETH:'ethereum',USDT:'tether'},form=document.getElementById('withdrawForm'),asset=document.getElementById('withdrawCrypto'),amount=document.getElementById('withdrawUsdAmount'),estimate=document.getElementById('hiddenCryptoAmount'),notice=document.getElementById('conversionNote'),box=document.getElementById('withdrawAlertContainer');const message=(text,good=false)=>box.innerHTML=`<div class="notice" style="${good?'background:#ecfdf5;border-color:#a7f3d0;color:#047857':'background:#fef2f2;border-color:#fecaca;color:#b91c1c'}">${text}</div>`;fetch(`${base}/get_balance.php`).then(r=>r.json()).then(d=>{if(d.success)document.getElementById('userUsdBalance').textContent=Number(d.balance).toFixed(2)}).catch(()=>{});async function quote(){estimate.value='';const value=Number(amount.value),id=ids[asset.value];if(!value||!id)return;notice.textContent='Fetching a current GBP conversion estimate…';try{const r=await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=gbp`),d=await r.json();estimate.value=(value/d[id].gbp).toFixed(8);notice.textContent=`Estimated ${asset.value} amount based on a current market quote.`}catch{notice.textContent='Unable to load a market quote. Please try again.'}}asset.addEventListener('change',quote);amount.addEventListener('input',quote);form.addEventListener('submit',async e=>{e.preventDefault();if(!estimate.value){message('A current conversion estimate is required before submission.');return}const btn=document.getElementById('previewButton');btn.disabled=true;btn.textContent='Submitting…';try{const r=await fetch(`${base}/submit-withdrawal.php`,{method:'POST',body:new FormData(form)}),d=await r.json();message(d.message,!!d.success);if(d.success)form.reset()}catch{message('Unable to submit the withdrawal request. Please try again.')}finally{btn.disabled=false;btn.textContent='Submit withdrawal request'}})});