/**
 * Public JavaScript for the Subscription System plugin
 */
(function($) {
    'use strict';

    $(document).ready(function() {
        // Handle subscription form submission
        $('.ss-subscription-form').on('submit', function(e) {
            e.preventDefault();
            
            var form = $(this);
            var submitButton = form.find('.ss-form-submit');
            var messageContainer = form.find('.ss-message');
            
            // Clear previous messages
            messageContainer.removeClass('success error').empty().hide();
            
            // Get form data
            var planId = form.find('select[name="plan_id"]').val() || form.find('input[name="plan_id"]').val();
            var email = form.find('input[name="email"]').val();
            
            // Validate form
            if (!planId) {
                messageContainer.addClass('error').text('Please select a subscription plan.').show();
                return;
            }
            
            if (!email) {
                messageContainer.addClass('error').text('Please enter your email address.').show();
                return;
            }
            
            // Show loading state
            submitButton.prop('disabled', true).text(ss_vars.i18n.processing);
            
            // Send AJAX request
            $.ajax({
                url: ss_vars.ajax_url,
                type: 'POST',
                data: {
                    action: 'ss_subscribe',
                    plan_id: planId,
                    email: email,
                    nonce: ss_vars.nonce
                },
                success: function(response) {
                    if (response.success) {
                        messageContainer.addClass('success').text(response.data.message).show();
                        
                        // Reset form
                        form[0].reset();
                        
                        // Redirect if URL provided
                        if (response.data.redirect) {
                            setTimeout(function() {
                                window.location.href = response.data.redirect;
                            }, 2000);
                        }
                    } else {
                        messageContainer.addClass('error').text(ss_vars.i18n.error + ' ' + response.data.message).show();
                    }
                    
                    // Reset button
                    submitButton.prop('disabled', false).text(form.data('button-text'));
                },
                error: function() {
                    messageContainer.addClass('error').text(ss_vars.i18n.error + ' An unexpected error occurred.').show();
                    submitButton.prop('disabled', false).text(form.data('button-text'));
                }
            });
        });
        
        // Handle cancel subscription
        $('.ss-cancel-subscription').on('click', function(e) {
            e.preventDefault();
            
            if (confirm('Are you sure you want to cancel this subscription?')) {
                var subscriptionId = $(this).data('id');
                
                // Send AJAX request
                $.ajax({
                    url: ss_vars.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'manage_subscription',
                        subscription_id: subscriptionId,
                        nonce: ss_vars.nonce,
                        ss_action: 'cancel'
                    },
                    success: function(response) {
                        if (response.success) {
                            alert(response.data.message);
                            window.location.reload();
                        } else {
                            alert('Error: ' + (response.data.message || 'An error occurred'));
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('AJAX Error:', status, error);
                        alert('An unexpected error occurred. Please try again.');
                    }
                });
            }
        });
        
        // Handle pause subscription
        $('.ss-pause-subscription').on('click', function(e) {
            e.preventDefault();
            
            if (confirm('Are you sure you want to pause this subscription?')) {
                var subscriptionId = $(this).data('id');
                
                // Send AJAX request
                $.ajax({
                    url: ss_vars.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'manage_subscription',
                        subscription_id: subscriptionId,
                        nonce: ss_vars.nonce,
                        ss_action: 'pause'
                    },
                    success: function(response) {
                        if (response.success) {
                            alert(response.data.message);
                            window.location.reload();
                        } else {
                            alert('Error: ' + (response.data.message || 'An error occurred'));
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('AJAX Error:', status, error);
                        alert('An unexpected error occurred. Please try again.');
                    }
                });
            }
        });
        
        // Handle resume subscription
        $('.ss-resume-subscription').on('click', function(e) {
            e.preventDefault();
            
            if (confirm('Are you sure you want to resume this subscription?')) {
                var subscriptionId = $(this).data('id');
                
                // Send AJAX request
                $.ajax({
                    url: ss_vars.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'manage_subscription',
                        subscription_id: subscriptionId,
                        ss_action: 'resume',
                        nonce: ss_vars.nonce
                    },
                    success: function(response) {
                        if (response.success) {
                            alert(response.data.message);
                            window.location.reload();
                        } else {
                            alert('Error: ' + (response.data.message || 'An error occurred'));
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('AJAX Error:', status, error);
                        alert('An unexpected error occurred. Please try again.');
                    }
                });
            }
        });
    });
})(jQuery); 