/**
 * 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 {
                        alert(response.data.message || 'Error: Failed to get certificate URL');
                    }
                },
                error: function() {
                    $button.prop('disabled', false).removeClass('hvac-loading');
                    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) {
                        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 {
                        alert(response.data.message || 'Error: Failed to send certificate');
                    }
                },
                error: function() {
                    $button.prop('disabled', false).removeClass('hvac-loading');
                    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) {
                        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 {
                        alert(response.data.message || 'Error: Failed to revoke certificate');
                    }
                },
                error: function() {
                    $button.prop('disabled', false).removeClass('hvac-loading');
                    alert('Error: Failed to connect to server');
                }
            });
        }
    };

    // Initialize when document is ready
    $(document).ready(function() {
        CertificateActions.init();
    });

})(jQuery);
EOFJS < /dev/null