/**
 * JavaScript for Iron Security 2FA admin functionality
 */
(function($) {
    'use strict';

    // Handle 2FA setup form
    const setupForm = $('#iron-security-2fa-setup-form');
    if (setupForm.length) {
        setupForm.on('submit', function(e) {
            e.preventDefault();
            
            const code = $('#iron_security_2fa_setup_code').val().trim();
            const secret = $(this).find('input[name="secret"]').val();
            const nonce = $(this).find('#iron_security_2fa_setup_nonce').val();
            
            if (!code || code.length !== 6 || !/^\d+$/.test(code)) {
                $('#iron-security-2fa-setup-error').show();
                return;
            }
            
            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'iron_security_verify_2fa_setup',
                    code: code,
                    secret: secret,
                    nonce: nonce,
                    setup: 1
                },
                success: function(response) {
                    if (response.success) {
                        $('#iron-security-2fa-setup-error').hide();
                        $('#iron-security-2fa-setup-form').hide();
                        $('#iron-security-2fa-setup-success').show();
                    } else {
                        $('#iron-security-2fa-setup-error').show();
                        $('#iron-security-2fa-setup-error').text(response.data || ironSecurity2FA.strings.verifyError);
                    }
                },
                error: function() {
                    $('#iron-security-2fa-setup-error').show();
                }
            });
        });
    }

    // Handle 2FA settings form
    const settingsForm = $('#iron-security-2fa-settings-form');
    if (settingsForm.length) {
        settingsForm.on('submit', function(e) {
            e.preventDefault();
            
            const enabled = $('#iron_security_2fa_enabled').prop('checked') ? 1 : 0;
            const roles = [];
            $('input[name="roles[]"]:checked').each(function() {
                roles.push($(this).val());
            });
            const gracePeriod = $('#iron_security_2fa_grace_period').val();
            const rememberDevice = $('#iron_security_2fa_remember_device').val();
            const nonce = $('#iron_security_2fa_nonce').val();
            
            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'iron_security_save_2fa_settings',
                    enabled: enabled,
                    roles: roles,
                    grace_period: gracePeriod,
                    remember_device: rememberDevice,
                    nonce: nonce
                },
                success: function(response) {
                    if (response.success) {
                        alert(ironSecurity2FA.strings.saveSuccess);
                    } else {
                        alert(ironSecurity2FA.strings.saveError);
                    }
                },
                error: function() {
                    alert(ironSecurity2FA.strings.saveError);
                }
            });
        });
    }

})(jQuery); 