/* A1-WooBay - eBay Tracking meta box (order edit screen, Pro)
 * Reads order ID / nonce from the button's data attributes - nothing here
 * needs server-side localization.
 */
jQuery(function($) {
	$('#a1wb-push-tracking-btn').on('click', function() {
		var btn      = $(this);
		var orderId  = btn.data('order-id');
		var nonce    = btn.data('nonce');
		var carrier  = $('#a1wb_tracking_carrier').val();
		var tracking = $('#a1wb_tracking_number').val().trim();
		var shipped  = $('#a1wb_shipped_date').val();
		var statusEl = $('#a1wb-tracking-status');

		if ( ! tracking ) {
			statusEl.css('color','#c00').text('Please enter a tracking number.');
			return;
		}

		btn.prop('disabled', true).text('Pushing...');
		statusEl.css('color','#777').text('');

		$.post(ajaxurl, {
			action:       'a1woobay_push_tracking',
			nonce:        nonce,
			order_id:     orderId,
			carrier:      carrier,
			tracking_num: tracking,
			shipped_date: shipped,
		}, function(res) {
			btn.prop('disabled', false).text('Push Tracking to eBay');
			if ( res.success ) {
				statusEl.css('color','#2a7a2a').text(res.data.message);
			} else {
				statusEl.css('color','#c00').text(res.data.message || 'Push failed.');
			}
		}).fail(function() {
			btn.prop('disabled', false).text('Push Tracking to eBay');
			statusEl.css('color','#c00').text('Request failed.');
		});
	});
});
