(()=>{'use strict';const $=s=>document.querySelector(s);let token=localStorage.getItem('lz_raast_agent_token')||'',selected=null;
function toast(t){const x=$('#toast');x.textContent=t;x.classList.add('show');clearTimeout(toast.t);toast.t=setTimeout(()=>x.classList.remove('show'),1800)}
async function api(url,opt={}){const h={...(opt.headers||{})};if(token)h['X-Raast-Agent-Token']=token;if(opt.body)h['Content-Type']='application/json';const r=await fetch(url,{cache:'no-store',...opt,headers:h});const j=await r.json().catch(()=>({}));if(!r.ok||!j.ok)throw new Error(j.error||'Request failed');return j}
function money(v){return Number(v||0).toLocaleString(undefined,{minimumFractionDigits:2,maximumFractionDigits:2})}
function esc(v){return String(v??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#039;'}[c]))}
async function load(){const j=await api('/api/raast_agent_queue.php?ts='+Date.now());$('#login').classList.add('hide');$('#app').classList.remove('hide');$('#whoCode').textContent=j.agent.agent_code;$('#whoName').textContent=j.agent.display_name;$('#shareAgentCode').textContent=j.agent.agent_code;const q=j.queue||[];$('#queue').innerHTML=q.length?q.map(r=>`<article class="item"><div><b>Withdrawal #${r.withdrawal_id}</b><small>Player ${esc(r.public_id)} • ${esc(r.method)}</small><small>Destination: ${esc(r.destination)}</small><small>Verify: ${esc(r.verification_code)}</small></div><strong>PKR ${money(r.amount)}</strong><div class="actions">${r.assignment_status==='assigned'?`<button class="ack" data-ack="${r.assignment_id}">ACKNOWLEDGE</button>`:''}<button data-paid="${r.assignment_id}">MARK PAID</button></div></article>`).join(''):'<div class="notice">No assigned withdrawals.</div>';document.querySelectorAll('[data-ack]').forEach(b=>b.onclick=()=>action(Number(b.dataset.ack),'acknowledge'));document.querySelectorAll('[data-paid]').forEach(b=>b.onclick=()=>{selected=Number(b.dataset.paid);$('#modal').classList.remove('hide')})}
async function action(id,action,data={}){try{await api('/api/raast_agent_action.php',{method:'POST',body:JSON.stringify({assignment_id:id,action,...data})});toast('Updated');await load()}catch(e){toast(e.message)}}
$('#loginBtn').onclick=async()=>{try{const r=await fetch('/api/raast_agent_login.php',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({agent_code:$('#agentCode').value.trim(),secret:$('#secret').value})});const j=await r.json();if(!r.ok||!j.ok)throw new Error(j.error||'Login failed');token=j.session_token;localStorage.setItem('lz_raast_agent_token',token);await load()}catch(e){$('#error').textContent=e.message}};
$('#refresh').onclick=()=>token&&load().catch(e=>toast(e.message));
$('#generateCode').onclick=async()=>{
  try{
    const j=await api('/api/raast_agent_generate_code.php',{
      method:'POST',
      body:JSON.stringify({ttl_minutes:30})
    });
    $('#shareAgentCode').textContent=j.code.agent_code;
    $('#shareConfirmCode').textContent=j.code.confirmation_code;
    $('#codeExpiry').textContent='Valid for '+j.code.expires_in_minutes+' minutes • one use only';
    $('#generatedCode').classList.remove('hide');
    toast('Confirmation code generated');
  }catch(e){toast(e.message)}
};
$('#copyAgentDetails').onclick=async()=>{
  const a=$('#shareAgentCode').textContent.trim();
  const c=$('#shareConfirmCode').textContent.trim();
  try{
    await navigator.clipboard.writeText('LuckZoo Raast Agent ID: '+a+'\nConfirmation Code: '+c);
    toast('Agent details copied');
  }catch(e){toast('Copy failed')}
};
$('#logout').onclick=()=>{localStorage.removeItem('lz_raast_agent_token');location.reload()};$('#cancel').onclick=()=>$('#modal').classList.add('hide');$('#paid').onclick=async()=>{await action(selected,'paid',{payout_channel:$('#channel').value.trim(),payout_reference:$('#reference').value.trim(),note:$('#note').value.trim()});$('#modal').classList.add('hide')};if(token)load().catch(()=>{localStorage.removeItem('lz_raast_agent_token');token=''})})();