// source --> https://www.spinfamily.fr/wp-content/plugins/msw-plugin/assets/js/MSW_plugin_ajax.js?ver=1.52 

jQuery(document).ready(function($) {
	
	var show_notice = function( html_element, $target ) {
		if ( ! $target ) {
			$target = $( '.woocommerce-notices-wrapper:first' ) || $( '.cart-empty' ).closest( '.woocommerce' ) || $( '.woocommerce-cart-form' );
		}
		$target.prepend( html_element );
	};
	
	//Vider totalement le panier
	$(document).on("click", "body form.woocommerce-cart-form #vider_panier", function(e){

		e.preventDefault();

		$('#MSW_overlay').css({'z-index':'9999','background-color':'rgba(0,0,0,0.5)','max-height': '2000px'});
		
		$.ajax( {
				type:     'POST',
				dataType: 'json',
				url:      MSW_plugin_nonce_URL.url,
				data: {
					action: "MSW_ajax_vider_panier",
					security : MSW_plugin_nonce_URL.nonce,
				},
				success: function( response ) {

					$('#MSW_overlay').css({'z-index':'3','background-color':'transparent','max-height': '0px'});
					
					if ( response.error == true ) show_notice('<ul class="woocommerce-error" role="alert"><li>Une erreur s\'est produite. Merci de rééessayer.</li></ul>');
					else if ( response.error == false )
					{
						$("[name='update_cart']").removeAttr('disabled');
						$("[name=\"update_cart\"]").trigger("click");
					}
				}
			} );
	});
	
	//Envoyer la wishlist par mail
	$(document).on("click", "form#yith-wcwl-form a.envoyer_wishlist", function(e){

		e.preventDefault();

		$('#MSW_overlay').css({'z-index':'9999','background-color':'rgba(0,0,0,0.5)','max-height': '2000px'});
		
		$.ajax( {
				type:     'POST',
				dataType: 'json',
				url:      MSW_plugin_nonce_URL.url,
				data: {
					action: "MSW_ajax_envoyer_mail_wishlist",
					security : MSW_plugin_nonce_URL.nonce,
				},
				success: function(response) {
					$('#MSW_overlay').css({'z-index':'3','background-color':'transparent','max-height': '0px'});
					
					if (response.error) show_notice('<ul class="woocommerce-error" role="alert"><li>'+response.error+'</li></ul>');
					else show_notice('<div class="woocommerce-message">Votre liste a bien été envoyée à l\'adresse '+response.mail+'</div>');
					$( 'form#yith-wcwl-form' ).unblock();
				}
			} );
	});
	
	//Appliquer un coupon avec AJAX page panier
	$( ".coupon button[name=apply_coupon]" ).click(function(e){

		e.preventDefault();
		
		$( '.woocommerce-cart-form , div.cart_totals ' ).block({
				message: null,
				overlayCSS: {
					background: '#fff',
					opacity: 0.7
				}
			});
		
		var coupon_code = $( '#coupon_code' ).val();

		$.ajax( {
				type:     'POST',
				dataType: 'html',
				url:      MSW_plugin_nonce_URL.url,
				data: {
					action: "MSW_ajax_apply_coupon",
					security : MSW_plugin_nonce_URL.nonce,
					coupon_code: coupon_code
				},
				success: function( response ) {
					$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
					show_notice( response );
					$( '.woocommerce-cart-form , div.cart_totals ' ).unblock();
					$( document.body ).trigger( 'applied_coupon', [ coupon_code ] );
				},
				complete: function() {
					$( '#coupon_code' ).val( '' );
					$("[name='update_cart']").prop("disabled", false);
					$("[name=\"update_cart\"]").trigger("click");	
				}
			} );
	});
});