// reCAPTCHA v3 implementation
function onSubmit(token) {
    // This function will be called when reCAPTCHA is verified
    // The form will be submitted automatically by reCAPTCHA
}

$(document).ready(function () {
    // Disable submit button by default
    $('#send_message').prop('disabled', false);
    
    // Hide error message initially
    $('#recaptcha-error').hide();

    // Remove error class on click
    $('#name, #email, #message, input[name="date"], #time').on('click', function() {
        $(this).removeClass('error_input');
    });

    // Form submission handler
    $('#contact_form').on('submit', function(e) {
        e.preventDefault();
        
        var error = false;
        var name = $('#name').val();
        var email = $('#email').val();
        var date = $('input[name="date"]').val();
        var time = $('#time').val();
        var message = $('#message').val();
        
        // Reset error states
        $('.error_input').removeClass('error_input');
        $('#recaptcha-error').hide();
        
        // Basic validation
        if (name.length === 0) {
            error = true;
            $('#name').addClass('error_input');
        }

        if (email.length === 0 || email.indexOf('@') === -1) {
            error = true;
            $('#email').addClass('error_input');
        }

        if (date.length === 0) {
            error = true;
            $('input[name="date"]').addClass('error_input');
        }

        if (time.length === 0) {
            error = true;
            $('#time').addClass('error_input');
        }

        if (message.length === 0) {
            error = true;
            $('#message').addClass('error_input');
        }
        
        if (error) {
            return false;
        }
        
        // Show loading state
        var $submitBtn = $('#send_message');
        $submitBtn.prop('disabled', true).val('Verifying...');
        
        // Execute reCAPTCHA
        grecaptcha.ready(function() {
            grecaptcha.execute('6LeH5kcsAAAAAGFkQp_Z_iyeUxcuTRAdX2v7jJeo', {action: 'submit'}).then(function(token) {
                // Add token to form
                var form = document.getElementById('contact_form');
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = 'g-recaptcha-response';
                input.value = token;
                form.appendChild(input);
                
                // Now submit the form
                $submitBtn.val('Sending...');
                var formData = $("#contact_form").serialize();
                
                // Submit form
                $.ajax({
            type: "POST",
            url: "visit.php",
            data: formData,
            success: function(response) {
                try {
                    var result = typeof response === 'string' ? JSON.parse(response) : response;
                    
                    if (result.success) {
                        // Hide the form and show success message
                        $('#contact_form').hide();
                        $('#success_message').fadeIn(500);
                        if (result.name) {
                            $('#user_name').text(result.name);
                        }
                    } else {
                        // Show error message
                        if (result.message) {
                            $('#recaptcha-error').text(result.message).show();
                        }
                        // Re-enable the submit button
                        $submitBtn.prop('disabled', false).val('Request a Visit');
                    }
                } catch (e) {
                    console.error('Error:', e, response);
                    $('#recaptcha-error').text('An error occurred. Please try again.').show();
                    // Re-enable the submit button
                    $submitBtn.prop('disabled', false).val('Request a Visit');
                }
            },
            error: function(xhr, status, error) {
                console.error('Error:', error);
                $('#recaptcha-error').text('Failed to submit form. Please try again.').show();
                $submitBtn.prop('disabled', false).val('Request a Visit');
            }
                });
            });
        });
        
        return false;
    });
});
