jQuery(document).ready(function($) {
    $('#kaveh-ticket-form').on('submit', function(e) {
        e.preventDefault();

        var $form = $(this);
        var $messageDiv = $('#kaveh-ticket-message');
        
        // Create FormData object to handle file uploads
        var formData = new FormData(this);
        formData.append('action', 'kaveh_submit_ticket');
        formData.append('nonce', kavehTicketAjax.nonce);

        $messageDiv.removeClass('success error').html('');
        $form.find('input[type="submit"]').prop('disabled', true);

        $.ajax({
            url: kavehTicketAjax.ajaxurl,
            type: 'POST',
            data: formData,
            processData: false, // Don't process the data
            contentType: false, // Don't set content type
            success: function(response) {
                if (response.success) {
                    $messageDiv.addClass('success').html(response.data.message);
                    $form[0].reset(); // Clear the form
                } else {
                    $messageDiv.addClass('error').html(response.data.message);
                }
            },
            error: function(xhr, status, error) {
                $messageDiv.addClass('error').html('An error occurred: ' + error);
            },
            complete: function() {
                $form.find('input[type="submit"]').prop('disabled', false);
            }
        });
    });
});