<h4>Email Settings — Button Loading Fix</h4><ul><li><strong>Fixed:</strong> Edit and View template buttons showed 'Loading record' indefinitely. Two causes: (1) When controller returned <code>null</code> (no template found for the given ID), the JS <code>if(res)</code> was false so <code>swal.close()</code> never ran. Moved <code>swal.close()</code> before the <code>if(res)</code> check. Added else branch showing 'Template not found' warning. (2) No AJAX <code>error</code> handler — if controller returned PHP errors (HTML instead of JSON), jQuery's <code>dataType:'json'</code> failed to parse and went to error callback which didn't exist. Added error handlers with SweetAlert to all 4 AJAX calls.</li><li><strong>Fixed:</strong> Undefined array key warnings in LeadsController — <code>$r['wtid']</code>, <code>$r['tempid']</code>, <code>$r['id']</code>, <code>$r['tid']</code> accessed without empty() check. Changed to <code>\!empty()</code> guards with null initialization.</li><li><strong>Fixed:</strong> <code>getEmailTemplateById</code> now checks <code>$r['tid']</code> (what the JS sends) as well as <code>$r['id']</code> (fallback).</li><li><strong>Fixed:</strong> Undefined <code>data-wtid</code>/<code>data-tempid</code> — JS reads both but buttons only set one. Added <code>|| ''</code> default to prevent undefined being sent as POST param.</li></ul><p><strong>Endpoints Tested:</strong> getAllEmailTemplateByWebsite (7 templates returned), getEmailTemplateWebsiteById (clean JSON), getEmailTemplateById (clean JSON). All 0 PHP errors.</p><p><strong>Files:</strong> platform/settings/emails/emails.php, controller/leads/LeadsController.php</p>