/** * Certificate Actions JavaScript * * Handles the AJAX interactions for certificate viewing, emailing, and revocation. */ (function($) { 'use strict'; // Certificate Actions const CertificateActions = { /** * Initialize certificate actions */ init: function() { // View certificate $(document).on('click', '.hvac-view-certificate', this.viewCertificate); // Email certificate $(document).on('click', '.hvac-email-certificate', this.emailCertificate); // Revoke certificate $(document).on('click', '.hvac-revoke-certificate', this.revokeCertificate); // Close certificate modal $(document).on('click', '.hvac-modal-close, .hvac-modal-overlay', this.closeCertificateModal); }, /** * View certificate */ viewCertificate: function(e) { e.preventDefault(); const $button = $(this); const certificateId = $button.data('certificate-id'); // Disable button while processing $button.prop('disabled', true).addClass('hvac-loading'); // AJAX request $.ajax({ url: hvacCertificateData.ajaxUrl, type: 'POST', data: { action: 'hvac_get_certificate_url', certificate_id: certificateId, nonce: hvacCertificateData.viewNonce }, success: function(response) { $button.prop('disabled', false).removeClass('hvac-loading'); if (response.success && response.data.url) { // Show preview modal if it exists if ($('#hvac-certificate-modal').length > 0) { CertificateActions.showCertificateModal(response.data.url); } else { // Open in new tab window.open(response.data.url, '_blank'); } } else { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error(response.data.message || 'Failed to get certificate URL'); } else { alert(response.data.message || 'Error: Failed to get certificate URL'); } } }, error: function() { $button.prop('disabled', false).removeClass('hvac-loading'); // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error('Failed to connect to server'); } else { alert('Error: Failed to connect to server'); } } }); }, /** * Show certificate modal */ showCertificateModal: function(url) { // Set iframe source $('#hvac-certificate-preview').attr('src', url); // Show modal $('.hvac-modal-overlay').fadeIn(200); $('#hvac-certificate-modal').fadeIn(200); }, /** * Close certificate modal */ closeCertificateModal: function(e) { if (e.target === this || $(e.target).hasClass('hvac-modal-close')) { $('.hvac-modal-overlay').fadeOut(200); $('#hvac-certificate-modal').fadeOut(200); // Clear iframe src after fade setTimeout(function() { $('#hvac-certificate-preview').attr('src', ''); }, 200); } }, /** * Email certificate */ emailCertificate: function(e) { e.preventDefault(); const $button = $(this); const certificateId = $button.data('certificate-id'); // Confirm sending if (!confirm('Send certificate to attendee via email?')) { return; } // Disable button while processing $button.prop('disabled', true).addClass('hvac-loading'); // AJAX request $.ajax({ url: hvacCertificateData.ajaxUrl, type: 'POST', data: { action: 'hvac_email_certificate', certificate_id: certificateId, nonce: hvacCertificateData.emailNonce }, success: function(response) { $button.prop('disabled', false).removeClass('hvac-loading'); if (response.success) { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.success(response.data.message || 'Certificate sent successfully'); } else { alert(response.data.message || 'Certificate sent successfully'); } // Update UI to indicate certificate has been emailed const $row = $button.closest('tr'); $row.find('.hvac-certificate-emailed').text('Yes'); } else { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error(response.data.message || 'Failed to send certificate'); } else { alert(response.data.message || 'Error: Failed to send certificate'); } } }, error: function() { $button.prop('disabled', false).removeClass('hvac-loading'); // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error('Failed to connect to server'); } else { alert('Error: Failed to connect to server'); } } }); }, /** * Revoke certificate */ revokeCertificate: function(e) { e.preventDefault(); const $button = $(this); const certificateId = $button.data('certificate-id'); // Prompt for revocation reason const reason = prompt('Please enter a reason for revoking this certificate:', ''); // If canceled if (reason === null) { return; } // Disable button while processing $button.prop('disabled', true).addClass('hvac-loading'); // AJAX request $.ajax({ url: hvacCertificateData.ajaxUrl, type: 'POST', data: { action: 'hvac_revoke_certificate', certificate_id: certificateId, reason: reason, nonce: hvacCertificateData.revokeNonce }, success: function(response) { $button.prop('disabled', false).removeClass('hvac-loading'); if (response.success) { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.success(response.data.message || 'Certificate revoked successfully'); } else { alert(response.data.message || 'Certificate revoked successfully'); } // Update UI to indicate certificate has been revoked const $row = $button.closest('tr'); $row.find('.hvac-certificate-status').text('Revoked'); $row.find('.hvac-certificate-status').addClass('hvac-status-revoked'); $row.find('.hvac-certificate-revoked-date').text(response.data.revoked_date || 'Today'); // Hide revoke button, show unrevoke button if it exists $button.hide(); $row.find('.hvac-unrevoke-certificate').show(); } else { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error(response.data.message || 'Failed to revoke certificate'); } else { alert(response.data.message || 'Error: Failed to revoke certificate'); } } }, error: function() { $button.prop('disabled', false).removeClass('hvac-loading'); // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error('Failed to connect to server'); } else { alert('Error: Failed to connect to server'); } } }); } }; // Generate Certificates AJAX functionality const GenerateCertificates = { /** * Initialize generate certificates functionality */ init: function() { // Event selection $(document).on('change', '#event_id', this.loadAttendees); // Certificate generation (use event delegation for dynamic form) $(document).on('submit', '#generate-certificates-form', this.generateCertificates); // Helper buttons (use event delegation for dynamic buttons) $(document).on('click', '#select-all-attendees', this.selectAllAttendees); $(document).on('click', '#select-checked-in', this.selectCheckedInAttendees); $(document).on('click', '#deselect-all-attendees', this.deselectAllAttendees); // Certificate preview (use event delegation for dynamic buttons) $(document).on('click', '.hvac-preview-certificate', this.showCertificatePreview); $(document).on('click', '.hvac-modal-close', this.closeCertificatePreview); $(document).on('click', '#hvac-certificate-preview-modal', function(e) { if (e.target === this) { GenerateCertificates.closeCertificatePreview(e); } }); }, /** * Load attendees via AJAX when event is selected */ loadAttendees: function(e) { const eventId = $(this).val(); if (!eventId) { $('#step-select-attendees').hide(); return; } // Show loading $('#attendees-loading').show(); $('#attendees-content').hide(); $.ajax({ url: hvacCertificateData.ajaxUrl, type: 'POST', data: { action: 'hvac_get_event_attendees', event_id: eventId, nonce: hvacCertificateData.generateNonce }, success: function(response) { if (response.success) { GenerateCertificates.renderAttendees(response.data.attendees, response.data.event_title); $('#step-select-attendees').show(); } else { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error(response.data.message); } else { alert('Error: ' + response.data.message); } } }, error: function() { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error('Failed to load attendees. Please try again.'); } else { alert('Failed to load attendees. Please try again.'); } }, complete: function() { $('#attendees-loading').hide(); $('#attendees-content').show(); } }); }, /** * Render attendees table */ renderAttendees: function(attendees, eventTitle) { if (attendees.length === 0) { $('#attendees-table-container').html('
This event has no attendees.
'); return; } let tableHtml = `| Attendee | Status | Certificate | ||
|---|---|---|---|---|
| ${!attendee.has_certificate ? `` : '' } | ${attendee.holder_name} | ${attendee.holder_email} | ${statusText} | ${certificateStatus} |
${response.data.message}
Failed to generate certificates. Please try again.