fix: move loadCustomerEmails to module scope, wire into switchCTab, add emails to tab loop; drop redundant ?: [] in customer-emails.php

This commit is contained in:
2026-06-23 16:35:11 +00:00
parent 145397ab81
commit 06260ed192
2 changed files with 40 additions and 39 deletions
+1 -1
View File
@@ -24,4 +24,4 @@ $emails = db()->fetchAll(
['cid' => $customerId] ['cid' => $customerId]
); );
echo json_encode(['emails' => $emails ?: []]); echo json_encode(['emails' => $emails]);
+39 -38
View File
@@ -517,8 +517,45 @@ function toggleCOrd(id){
var _cCustId = null; var _cCustId = null;
var _cLoaded = false; var _cLoaded = false;
function loadCustomerEmails(customerId) {
if (!customerId) return;
document.getElementById('cEmailLoading').style.display = 'block';
document.getElementById('cEmailContent').style.display = 'none';
var colors = {"sent":"#3B82F6","delivered":"#10B981","bounced":"#EF4444","failed":"#EF4444","unknown":"#9CA3AF"};
fetch('/admin/api/customer-emails.php?customer_id=' + encodeURIComponent(customerId))
.then(r => r.json()).then(function(data) {
document.getElementById('cEmailLoading').style.display = 'none';
document.getElementById('cEmailContent').style.display = 'block';
var emails = data.emails || [];
document.getElementById('cEmailBadge').textContent = emails.length;
document.getElementById('cEmailViewAll').href = '/admin/email-log.php?customer=' + encodeURIComponent(customerId);
if (!emails.length) {
document.getElementById('cEmailEmpty').style.display = 'block';
document.getElementById('cEmailTable').style.display = 'none';
return;
}
document.getElementById('cEmailEmpty').style.display = 'none';
document.getElementById('cEmailTable').style.display = 'table';
var html = '';
emails.forEach(function(e) {
var color = colors[e.status] || '#9CA3AF';
var badge = '<span style="background:'+color+';color:white;padding:2px 7px;border-radius:3px;font-size:.75rem;">'+e.status+'</span>';
var opened = e.opened == '1' ? '<span style="color:#10B981">✓'+(e.open_count > 1 ? ' ('+e.open_count+')' : '')+'</span>' : '<span style="color:#9CA3AF">—</span>';
html += '<tr style="border-bottom:1px solid var(--color-border)">'
+ '<td style="padding:.5rem;max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="'+e.subject+'">'+e.subject+'</td>'
+ '<td style="padding:.5rem">'+badge+'</td>'
+ '<td style="padding:.5rem">'+opened+'</td>'
+ '<td style="padding:.5rem;white-space:nowrap;color:#666">'+e.sent_at+'</td>'
+ '</tr>';
});
document.getElementById('cEmailBody').innerHTML = html;
}).catch(function() {
document.getElementById('cEmailLoading').innerHTML = '<div style="color:var(--color-error)"><i class="fas fa-exclamation-circle"></i> Failed to load emails</div>';
});
}
function switchCTab(tab) { function switchCTab(tab) {
['details','orders'].forEach(function(t) { ['details','orders','emails'].forEach(function(t) {
var tabBtn = document.getElementById('ctab-' + t); var tabBtn = document.getElementById('ctab-' + t);
var panel = document.getElementById('cpanel-' + t); var panel = document.getElementById('cpanel-' + t);
if (tabBtn) { if (tabBtn) {
@@ -528,6 +565,7 @@ function switchCTab(tab) {
if (panel) panel.style.display = t === tab ? '' : 'none'; if (panel) panel.style.display = t === tab ? '' : 'none';
}); });
if (tab === 'orders' && _cCustId && !_cLoaded) loadCOrders(_cCustId); if (tab === 'orders' && _cCustId && !_cLoaded) loadCOrders(_cCustId);
if (tab === 'emails' && _cCustId) loadCustomerEmails(_cCustId);
} }
@@ -535,43 +573,6 @@ function openCustomerModal(customer = null) {
const isEdit = !!customer; const isEdit = !!customer;
_cCustId = isEdit ? customer.customer_id : null; _cCustId = isEdit ? customer.customer_id : null;
_cLoaded = false; _cLoaded = false;
// Email history loader
function loadCustomerEmails(customerId){
if(!customerId) return;
document.getElementById('cEmailLoading').style.display='block';
document.getElementById('cEmailContent').style.display='none';
fetch('/admin/api/customer-emails.php?customer_id='+encodeURIComponent(customerId))
.then(r=>r.json()).then(function(data){
document.getElementById('cEmailLoading').style.display='none';
document.getElementById('cEmailContent').style.display='block';
var emails=data.emails||[];
document.getElementById('cEmailBadge').textContent=emails.length;
document.getElementById('cEmailViewAll').href='/admin/email-log.php?customer='+encodeURIComponent(customerId);
if(!emails.length){
document.getElementById('cEmailEmpty').style.display='block';
document.getElementById('cEmailTable').style.display='none';
return;
}
document.getElementById('cEmailEmpty').style.display='none';
document.getElementById('cEmailTable').style.display='table';
var colors={"sent":"#3B82F6","delivered":"#10B981","bounced":"#EF4444","failed":"#EF4444","unknown":"#9CA3AF"};
var html='';
emails.forEach(function(e){
var color=colors[e.status]||'#9CA3AF';
var badge='<span style="background:'+color+';color:white;padding:2px 7px;border-radius:3px;font-size:.75rem;">'+e.status+'</span>';
var opened=e.opened=='1'?'<span style="color:#10B981">✓'+(e.open_count>1?' ('+e.open_count+')':'')+'</span>':'<span style="color:#9CA3AF">—</span>';
html+='<tr style="border-bottom:1px solid var(--color-border)">'
+'<td style="padding:.5rem;max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="'+e.subject+'">'+e.subject+'</td>'
+'<td style="padding:.5rem">'+badge+'</td>'
+'<td style="padding:.5rem">'+opened+'</td>'
+'<td style="padding:.5rem;white-space:nowrap;color:#666">'+e.sent_at+'</td>'
+'</tr>';
});
document.getElementById('cEmailBody').innerHTML=html;
}).catch(function(){
document.getElementById('cEmailLoading').innerHTML='<div style="color:var(--color-error)"><i class="fas fa-exclamation-circle"></i> Failed to load emails</div>';
});
}
document.getElementById('customerModalTitle').textContent = isEdit ? 'Edit Customer' : 'Add Customer'; document.getElementById('customerModalTitle').textContent = isEdit ? 'Edit Customer' : 'Add Customer';
document.getElementById('customerSubmitBtn').textContent = isEdit ? 'Update Customer' : 'Add Customer'; document.getElementById('customerSubmitBtn').textContent = isEdit ? 'Update Customer' : 'Add Customer';