/* A1-WooBay - Settings page
 * Handles: "Import Orders Now" button (Pro), "Sync Now" button (free + Pro).
 */
(function($){
	$('#a1wb-import-orders-btn').on('click', function(){
		var $btn = $(this), $msg = $('#a1wb-import-orders-msg');
		$btn.prop('disabled', true);
		$msg.css('color','#555').text(a1wbSettings.importingText);
		$.post(ajaxurl, {
			action: 'a1woobay_import_orders',
			nonce:  a1wbSettings.importOrdersNonce
		}, function(r){
			if (r.success) {
				$msg.css('color','#2a9d2a').text(r.data.message);
			} else {
				$msg.css('color','#dc3232').text(r.data || 'Error');
			}
			$btn.prop('disabled', false);
		}).fail(function(){
			$msg.css('color','#dc3232').text('Request failed.');
			$btn.prop('disabled', false);
		});
	});

	$('#a1wb-sync-now-btn').on('click', function(){
		var $btn = $(this), $msg = $('#a1wb-sync-now-msg');
		$btn.prop('disabled', true);
		$msg.text(a1wbSettings.syncingText);
		$.post(ajaxurl, {
			action: 'a1woobay_manual_sync',
			nonce:  a1wbSettings.manualSyncNonce
		}, function(r){
			if (r.success) {
				$msg.css('color','#2a9d2a').text(r.data.message);
			} else {
				$msg.css('color','#dc3232').text(r.data || 'Error');
			}
			$btn.prop('disabled', false);
		}).fail(function(){
			$msg.css('color','#dc3232').text('Request failed.');
			$btn.prop('disabled', false);
		});
	});
}(jQuery));
