(function($) {
    'use strict';

    $(document).ready(function() {
        // Toggle password visibility
        $('.password-toggle').on('click', function() {
            var targetId = $(this).data('target');
            var passwordField = $('#' + targetId);
            
            if (passwordField.attr('type') === 'password') {
                passwordField.attr('type', 'text');
                $(this).text('🔒');
            } else {
                passwordField.attr('type', 'password');
                $(this).text('👁️');
            }
        });
        
        // Registration form submission
        $('#subscription-system-register-form').on('submit', function(e) {
            e.preventDefault();
            
            var form = $(this);
            var messageContainer = form.closest('.subscription-system-form-container').find('.subscription-system-message-container');
            
            // Clear previous messages
            messageContainer.empty();
            
            // Disable submit button
            form.find('button[type="submit"]').prop('disabled', true).text('Processing...');
            
            // Collect form data
            var formData = new FormData(form[0]);
            formData.append('action', 'subs_sys_register');
            formData.append('security', subs_sys_ajax.register_nonce);
            
            // Send AJAX request
            $.ajax({
                url: subs_sys_ajax.ajax_url,
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Register');
                    
                    // Display message
                    var messageClass = response.success ? 'success' : 'error';
                    messageContainer.html('<div class="subscription-system-message ' + messageClass + '">' + response.message + '</div>');
                    
                    // If registration was successful, redirect after a delay
                    if (response.success && response.redirect) {
                        setTimeout(function() {
                            window.location.href = response.redirect;
                        }, 2000);
                    }
                },
                error: function() {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Register');
                    
                    // Display error message
                    messageContainer.html('<div class="subscription-system-message error">An error occurred. Please try again later.</div>');
                }
            });
        });
        
        // Login form submission
        $('#subscription-system-login-form').on('submit', function(e) {
            e.preventDefault();
            
            var form = $(this);
            var messageContainer = form.closest('.subscription-system-form-container').find('.subscription-system-message-container');
            
            // Clear previous messages
            messageContainer.empty();
            
            // Disable submit button
            form.find('button[type="submit"]').prop('disabled', true).text('Processing...');
            
            // Collect form data
            var formData = new FormData(form[0]);
            formData.append('action', 'subs_sys_login');
            formData.append('security', subs_sys_ajax.login_nonce);
            
            // Send AJAX request
            $.ajax({
                url: subs_sys_ajax.ajax_url,
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Login');
                    
                    // Display message
                    var messageClass = response.success ? 'success' : 'error';
                    messageContainer.html('<div class="subscription-system-message ' + messageClass + '">' + response.message + '</div>');
                    
                    // If login was successful, redirect after a delay
                    if (response.success && response.redirect) {
                        setTimeout(function() {
                            window.location.href = response.redirect;
                        }, 2000);
                    }
                },
                error: function() {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Login');
                    
                    // Display error message
                    messageContainer.html('<div class="subscription-system-message error">An error occurred. Please try again later.</div>');
                }
            });
        });
        
        // Forgot Password form submission
        $('#subscription-system-forgot-password-form').on('submit', function(e) {
            e.preventDefault();
            
            var form = $(this);
            var messageContainer = form.closest('.subscription-system-form-container').find('.subscription-system-message-container');
            
            // Clear previous messages
            messageContainer.empty();
            
            // Disable submit button
            form.find('button[type="submit"]').prop('disabled', true).text('Processing...');
            
            // Collect form data
            var formData = new FormData(form[0]);
            formData.append('action', 'subs_sys_forgot_password');
            formData.append('security', subs_sys_ajax.forgot_password_nonce);
            
            // Send AJAX request
            $.ajax({
                url: subs_sys_ajax.ajax_url,
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Get New Password');
                    
                    // Display message
                    var messageClass = response.success ? 'success' : 'error';
                    messageContainer.html('<div class="subscription-system-message ' + messageClass + '">' + response.message + '</div>');
                    
                    // If successful, clear the form
                    if (response.success) {
                        form[0].reset();
                    }
                },
                error: function() {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Get New Password');
                    
                    // Display error message
                    messageContainer.html('<div class="subscription-system-message error">An error occurred. Please try again later.</div>');
                }
            });
        });
        
        // Reset Password form submission
        $('#subscription-system-reset-password-form').on('submit', function(e) {
            e.preventDefault();
            
            var form = $(this);
            var messageContainer = form.closest('.subscription-system-form-container').find('.subscription-system-message-container');
            
            // Clear previous messages
            messageContainer.empty();
            
            // Validate passwords match
            var pass1 = form.find('#pass1').val();
            var pass2 = form.find('#pass2').val();
            
            if (pass1 !== pass2) {
                messageContainer.html('<div class="subscription-system-message error">Passwords do not match.</div>');
                return;
            }
            
            // Disable submit button
            form.find('button[type="submit"]').prop('disabled', true).text('Processing...');
            
            // Collect form data
            var formData = new FormData(form[0]);
            formData.append('action', 'subs_sys_reset_password');
            formData.append('security', subs_sys_ajax.reset_password_nonce);
            
            // Send AJAX request
            $.ajax({
                url: subs_sys_ajax.ajax_url,
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Reset Password');
                    
                    // Display message
                    var messageClass = response.success ? 'success' : 'error';
                    messageContainer.html('<div class="subscription-system-message ' + messageClass + '">' + response.message + '</div>');
                    
                    // If reset was successful, redirect after a delay
                    if (response.success && response.redirect) {
                        setTimeout(function() {
                            window.location.href = response.redirect;
                        }, 2000);
                    }
                },
                error: function() {
                    // Re-enable submit button
                    form.find('button[type="submit"]').prop('disabled', false).text('Reset Password');
                    
                    // Display error message
                    messageContainer.html('<div class="subscription-system-message error">An error occurred. Please try again later.</div>');
                }
            });
        });
    });

})(jQuery); 