From 06260ed192e2daffc1f61397f76d13abbb2d074b Mon Sep 17 00:00:00 2001 From: Myron Blair Date: Tue, 23 Jun 2026 16:35:11 +0000 Subject: [PATCH] fix: move loadCustomerEmails to module scope, wire into switchCTab, add emails to tab loop; drop redundant ?: [] in customer-emails.php --- admin/api/customer-emails.php | 2 +- admin/customers.php | 77 ++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/admin/api/customer-emails.php b/admin/api/customer-emails.php index f5a773a..3c041c7 100644 --- a/admin/api/customer-emails.php +++ b/admin/api/customer-emails.php @@ -24,4 +24,4 @@ $emails = db()->fetchAll( ['cid' => $customerId] ); -echo json_encode(['emails' => $emails ?: []]); +echo json_encode(['emails' => $emails]); diff --git a/admin/customers.php b/admin/customers.php index 3a63900..c9433a4 100644 --- a/admin/customers.php +++ b/admin/customers.php @@ -517,8 +517,45 @@ function toggleCOrd(id){ var _cCustId = null; 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 = ''+e.status+''; + var opened = e.opened == '1' ? '✓'+(e.open_count > 1 ? ' ('+e.open_count+')' : '')+'' : ''; + html += '' + + ''+e.subject+'' + + ''+badge+'' + + ''+opened+'' + + ''+e.sent_at+'' + + ''; + }); + document.getElementById('cEmailBody').innerHTML = html; + }).catch(function() { + document.getElementById('cEmailLoading').innerHTML = '
Failed to load emails
'; + }); +} + function switchCTab(tab) { - ['details','orders'].forEach(function(t) { + ['details','orders','emails'].forEach(function(t) { var tabBtn = document.getElementById('ctab-' + t); var panel = document.getElementById('cpanel-' + t); if (tabBtn) { @@ -528,6 +565,7 @@ function switchCTab(tab) { if (panel) panel.style.display = t === tab ? '' : 'none'; }); if (tab === 'orders' && _cCustId && !_cLoaded) loadCOrders(_cCustId); + if (tab === 'emails' && _cCustId) loadCustomerEmails(_cCustId); } @@ -535,43 +573,6 @@ function openCustomerModal(customer = null) { const isEdit = !!customer; _cCustId = isEdit ? customer.customer_id : null; _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=''+e.status+''; - var opened=e.opened=='1'?'✓'+(e.open_count>1?' ('+e.open_count+')':'')+'':''; - html+='' - +''+e.subject+'' - +''+badge+'' - +''+opened+'' - +''+e.sent_at+'' - +''; - }); - document.getElementById('cEmailBody').innerHTML=html; - }).catch(function(){ - document.getElementById('cEmailLoading').innerHTML='
Failed to load emails
'; - }); - } document.getElementById('customerModalTitle').textContent = isEdit ? 'Edit Customer' : 'Add Customer'; document.getElementById('customerSubmitBtn').textContent = isEdit ? 'Update Customer' : 'Add Customer';