jQuery(document).ready(function($){
    // Store initial values
    var initialValues = {
        driver_image: $('#oddw_driver_image').val(),
        car_image: $('#oddw_driver_car_image').val(),
        car_type: $('#oddw_driver_car_type').val()
    };

    function checkForChanges() {
        var currentValues = {
            driver_image: $('#oddw_driver_image').val(),
            car_image: $('#oddw_driver_car_image').val(),
            car_type: $('#oddw_driver_car_type').val()
        };

        return JSON.stringify(initialValues) !== JSON.stringify(currentValues);
    }

    function updateSubmitButton() {
        var $submitBtn = $('input#submit');
        $submitBtn.prop('disabled', !checkForChanges());

        $(document).trigger('user-profile-update');
        $('#your-profile').trigger('change');
    }

    function oddwBindMediaUploader(btn, input, preview, removeBtn) {
        // Store initial value to track changes
        var initialValue = input.val();
        btn.on('click', function(e){
            e.preventDefault();
            var custom_uploader = wp.media({
                title: 'Select Image',
                button: { text: 'Use this image' },
                multiple: false,
                library: { type: 'image' }
            })
            .on('select', function() {
                var attachment = custom_uploader.state().get('selection').first().toJSON();
                
                // Update hidden input and preview
                input.val(attachment.id);
                preview.attr('src', attachment.url).show();
                removeBtn.show();

                updateSubmitButton();
                input.trigger('change');
                $('#your-profile').trigger('change').trigger('user-profile-update');
                $(document).trigger('user-profile-update');
            })
            .open();
        });

        removeBtn.on('click', function(e){
            e.preventDefault();
            
            // Update hidden input and preview
            input.val('');
            preview.hide();
            removeBtn.hide();

            updateSubmitButton();
            input.trigger('change');
            $('#your-profile').trigger('change').trigger('user-profile-update');
            $(document).trigger('user-profile-update');
        });

        // Monitor manual changes to the hidden input
        input.on('change', function() {
            updateSubmitButton();
        });
    }

    // Initialize media uploaders for both fields
    oddwBindMediaUploader(
        $('#oddw_driver_image').siblings('.oddw-upload-btn'),
        $('#oddw_driver_image'),
        $('#oddw_driver_image_preview'),
        $('#oddw_driver_image').siblings('.oddw-remove-btn')
    );

    oddwBindMediaUploader(
        $('#oddw_driver_car_image').siblings('.oddw-upload-btn'),
        $('#oddw_driver_car_image'),
        $('#oddw_driver_car_image_preview'),
        $('#oddw_driver_car_image').siblings('.oddw-remove-btn')
    );

    // Monitor form changes
    $('#your-profile').on('change', 'input, select, textarea', function() {
        updateSubmitButton();
    });

    // Initial button state
    $('#your-profile').find('input#submit').prop('disabled', true);
});