(function(){
function escH(s){return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');}
function setCard(id,value,cls){
var el=document.getElementById(id);
if(!el)return;
var v=el.querySelector('.hc-value');
v.textContent=value;
v.className='hc-value '+cls;
}
window.emailsRefreshStatus=function(){
fetch('/api/email/status.php?action=status')
.then(function(r){return r.json();})
.then(function(d){
setCard('hc-smtp',d.smtp_port_up?'UP':'DOWN',d.smtp_port_up?'ok':'err');
setCard('hc-wg',d.wg_tunnel_up?'LIVE':'DOWN',d.wg_tunnel_up?'ok':'warn');
setCard('hc-dkim',d.dkim_selector+'._domainkey','ok');
setCard('hc-sent',d.stats.last_24h||'0','ok');
setCard('hc-failed',d.stats.failed||'0',parseInt(d.stats.failed)>0?'err':'ok');
setCard('hc-etransfer',d.pending_etransfers||'0',parseInt(d.pending_etransfers)>0?'warn':'ok');
if(d.imap_scanner!==undefined){
setCard('hc-imap',d.imap_scanner?'ACTIVE':'IDLE',d.imap_scanner?'ok':'warn');
}else{
setCard('hc-imap','N/A','warn');
}
document.getElementById('email-config-bar').innerHTML=
'<div class="ecb-item"><div class="ecb-icon"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><path d="M22 6l-10 7L2 6"/></svg></div><div class="ecb-text"><span class="ecb-label">SMTP Host</span><span class="ecb-value">'+escH(d.smtp_host)+'</span></div></div>'+
'<div class="ecb-item"><div class="ecb-icon"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg></div><div class="ecb-text"><span class="ecb-label">From Address</span><span class="ecb-value">'+escH(d.smtp_from)+'</span></div></div>'+
'<div class="ecb-item"><div class="ecb-icon"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 12h-4l-3 9L9 3l-3 9H2"/></svg></div><div class="ecb-text"><span class="ecb-label">Relay Protocol</span><span class="ecb-value">'+escH(d.relay_method)+'</span></div></div>'+
'<div class="ecb-item"><div class="ecb-icon"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg></div><div class="ecb-text"><span class="ecb-label">DKIM Authority</span><span class="ecb-value">'+escH(d.dkim_selector)+'._domainkey.'+escH(d.dkim_domain)+'</span></div></div>'+
'<div class="ecb-item"><div class="ecb-icon"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg></div><div class="ecb-text"><span class="ecb-label">Origin Node</span><span class="ecb-value">'+escH(d.email_node_ip)+' (WG '+escH(d.wg_ip)+')</span></div></div>'+
'<div class="ecb-item" style="margin-left:auto"><div class="ecb-text" style="align-items:flex-end"><span class="ecb-label">Last Synchronized</span><span class="ecb-value" style="color:var(--text-dim);font-weight:400">'+escH(d.checked_at)+'</span></div></div>';
}).catch(function(){});
};
window.emailsLoadLog=function(offset){
offset=offset||0;
var status=document.getElementById('email-log-filter').value;
var url='/api/email/status.php?action=log&limit=25&offset='+offset+(status?'&status='+status:'');
document.getElementById('email-log-body').innerHTML='<tr><td colspan="7" class="loading-row">Loading...</td></tr>';
fetch(url).then(function(r){return r.json();}).then(function(d){
if(!d.rows||!d.rows.length){
document.getElementById('email-log-body').innerHTML='<tr><td colspan="7" class="loading-row">No emails yet.</td></tr>';
return;
}
document.getElementById('email-log-body').innerHTML=d.rows.map(function(r){
var badge='<span class="badge badge-'+r.status+'">'+escH(r.status)+'</span>';
var relay='<span class="badge badge-'+(r.relay_method==='wireguard'?'wg':'public')+'">'+escH(r.relay_method)+'</span>';
return '<tr><td>'+escH(r.created_at)+'</td><td>'+escH(r.sent_to)+'</td><td>'+escH(r.subject)+'</td>'+
'<td>'+(r.template||'--')+'</td><td>'+relay+'</td><td>'+badge+'</td>'+
'<td>'+(r.status==='failed'?'<button class="btn-ghost btn-xs" data-email-action="resend" data-id="'+r.id+'">Retry</button>':'')+'</td></tr>';
}).join('');
var pg=document.getElementById('email-log-pagination');
pg.innerHTML='Showing '+(offset+1)+'--'+Math.min(offset+25,d.total)+' of '+d.total;
if(offset>0)pg.innerHTML='<button class="btn-ghost btn-xs" data-email-action="prev-page" data-offset="'+(offset-25)+'">Prev</button> '+pg.innerHTML;
if(offset+25<d.total)pg.innerHTML+=' <button class="btn-ghost btn-xs" data-email-action="next-page" data-offset="'+(offset+25)+'">Next</button>';
});
};
window.emailsLoadEtransfer=function(){
fetch('/api/email/status.php?action=etransfer')
.then(function(r){return r.json();})
.then(function(d){
if(!d.rows||!d.rows.length){
document.getElementById('etransfer-body').innerHTML='<tr><td colspan="8" class="loading-row">No e-transfers found.</td></tr>';
return;
}
document.getElementById('etransfer-body').innerHTML=d.rows.map(function(r){
var badge='<span class="badge badge-'+r.status+'">'+escH(r.status)+'</span>';
return '<tr><td>#'+r.order_id+'</td>'+
'<td>$'+parseFloat(r.amount).toFixed(2)+' '+escH(r.currency)+'</td>'+
'<td><code>'+escH(r.reference_code||'--')+'</code></td>'+
'<td>'+escH(r.sender_name||'--')+'</td>'+
'<td>'+escH(r.customer_email||'--')+'</td>'+
'<td>'+badge+'</td>'+
'<td>'+(r.created_at||'--')+'</td>'+
'<td>'+(r.confirmed_at||'--')+'</td></tr>';
}).join('');
});
};
function showTestModal(){
document.getElementById('email-test-modal').style.display='flex';
document.getElementById('test-result').innerHTML='';
}
function hideTestModal(){
document.getElementById('email-test-modal').style.display='none';
}
function sendTest(){
var btn=document.getElementById('test-send-btn');
btn.disabled=true;btn.textContent='Sending...';
var fd=new FormData();
fd.append('action','test');
fd.append('to',document.getElementById('test-to').value);
fd.append('subject',document.getElementById('test-subject').value);
var cm=document.querySelector('meta[name="csrf-token"]');if(cm)fd.append('_csrf',cm.content);
fetch('/api/email/status.php',{method:'POST',body:fd})
.then(function(r){return r.json();})
.then(function(d){
btn.disabled=false;btn.textContent='Send';
var el=document.getElementById('test-result');
if(d.success){
el.innerHTML='<div style="color:#00ff87;font-size:13px">Sent successfully -- '+escH(d.response||'')+'</div>';
emailsLoadLog();
}else{
el.innerHTML='<div style="color:#ef4444;font-size:13px">Failed -- '+escH(d.error||d.response||'Unknown error')+'</div>';
}
});
}
function resendEmail(id){
var fd=new FormData();fd.append('action','resend');fd.append('id',id);var cm=document.querySelector('meta[name="csrf-token"]');if(cm)fd.append('_csrf',cm.content);
fetch('/api/email/status.php',{method:'POST',body:fd}).then(function(){emailsLoadLog();});
}
document.addEventListener('click',function(e){
var el=e.target.closest('[data-email-action]');
if(!el)return;
var action=el.dataset.emailAction;
if(action==='refresh')emailsRefreshStatus();
else if(action==='test-modal')showTestModal();
else if(action==='resend')resendEmail(el.dataset.id);
else if(action==='prev-page'||action==='next-page')emailsLoadLog(parseInt(el.dataset.offset));
});
var closeBtn=document.getElementById('email-test-close-btn');
if(closeBtn)closeBtn.addEventListener('click',hideTestModal);
var cancelBtn=document.getElementById('email-test-cancel-btn');
if(cancelBtn)cancelBtn.addEventListener('click',hideTestModal);
var sendBtn=document.getElementById('test-send-btn');
if(sendBtn)sendBtn.addEventListener('click',sendTest);
var logFilter=document.getElementById('email-log-filter');
if(logFilter)logFilter.addEventListener('change',function(){emailsLoadLog();});
emailsRefreshStatus();
emailsLoadLog();
emailsLoadEtransfer();
setInterval(emailsRefreshStatus,60000);
})();
