jQuery(document).ready(function($) {
    // [awxct-build] console.log('✅ translation-mapping.js loaded');

    $('.awxct-active-translation-form').on('submit', function(e) {
        e.preventDefault();

        const $form = $(this);
        const selected = $form.find('input[name="active_translation"]:checked').val();
        const originalPostId = $form.find('input[name="original_post_id"]').val();
        const languageCode = $form.find('input[name="language_code"]').val();

        if (!selected) {
            alert('Please select a translation.');
            return;
        }

        $.post(awxctMappingData.ajax_url, {
            action: 'awxct_set_active_translation',
            translation_id: selected,
            original_post_id: originalPostId,
            language_code: languageCode,
            nonce: awxctMappingData.nonce || '' // add nonce if provided
        }, function(response) {
            if (response && response.success) {
                alert('✅ Saved successfully.');
                location.reload();
            } else {
                alert('❌ Error: ' + (response && response.data && response.data.message ? response.data.message : 'Unknown error'));
            }
        }).fail(function(xhr) {
            console.error('[AWX Mapping] AJAX error:', xhr.responseText || xhr.statusText);
            alert('❌ Request failed – see console for details.');
        });
    });
});
