import {
	PixelioCartAbandonRecoveryMoreLoaderSpinner
} from "../components/pixelio_cart_abandon_recovery_more_loader_spinner";
import {
	PixelioCartAbandonRecoveryMoreUpdate
} from "../components/pixelio_cart_abandon_recovery_more_update_popup";
jQuery(function($) {
	var loadSpinner = new PixelioCartAbandonRecoveryMoreLoaderSpinner();

	var already_vote = $(".pixelio-cart-abandon-recovery-more-already-voted").length>0;
	if (already_vote) {
		$('.pixelio-cart-abandon-recovery-more-submit-vote').hide();
	}

	$('.pixelio-cart-abandon-recovery-more-integration-option').on('click', function () {
		$('.pixelio-cart-abandon-recovery-more-integration-option').removeClass('selected');
		$('.pixelio-other-input-container').hide();
		$('.pixelio-cart-abandon-recovery-more-other-input').val('');
		$(this).toggleClass('selected');
	});

	$('.pixelio-cart-abandon-recovery-more-submit-vote').on('click', function () {
		loadSpinner.showPixelioLoader();
		var selected = $('.pixelio-cart-abandon-recovery-more-integration-option.selected').data('value');

		if (!selected) {
			loadSpinner.hidePixelioLoader();
			alert(pixelioVoteData.alert_choose_integration);
			return;
		}

		$.ajax({
			url: pixelioVoteData.vote_endpoint,
			method: 'POST',
			contentType: 'application/json',
			data: JSON.stringify({
				name: selected,
				userId: pixelioVoteData.user_id
			}),
			headers: {
				'x-api-key': pixelioVoteData.api_key
			},
			success: function(response) {
				if (response.success) {
					alert(pixelioVoteData.alert_success);
					location.reload();
				} else {
					alert(pixelioVoteData.alert_error);
				}
				loadSpinner.hidePixelioLoader();
			},
			error: function() {
				alert(pixelioVoteData.alert_error);
				loadSpinner.hidePixelioLoader();
			}
		});

	});

});
