/**
 * Classic Checkout - EMT Payment Method Handler
 * Triggers checkout update when EMT is selected to recalculate discount
 */
jQuery(function($) {
	function setPayment(method) {
		$.post(wc_emt_params.ajax_url, {
			action: 'wc_emt_set_payment_method',
			nonce: wc_emt_params.nonce,
			payment_method: method
		}).done(function(res){
			console.log('[EMT] AJAX set_payment_method response', res);
			$('body').trigger('update_checkout');
		});
	}

	// Trigger when payment method changes
	$('input[name="payment_method"]').on('change', function() {
		var val = $(this).val();
		if (val === 'advanced_emt') {
			setPayment('advanced_emt');
		} else {
			$('body').trigger('update_checkout');
		}
	});

	// Also trigger on initial page load if EMT is already selected
	if ($('input[name="payment_method"]:checked').val() === 'advanced_emt') {
		setPayment('advanced_emt');
	}
});
