jQuery(document).ready(function() {
// Why you peaking at the code bro?

// Save Listing
jQuery('body').on("click", '.savelistingbutton', function(){
	var response;
	var listingId = this.getAttribute('data-listingid');
	var userId = this.getAttribute('data-userid');

	jQuery.ajax({
		type: "POST",
		url: "/wp-admin/admin-ajax.php",
		async: false,
		data: {
			action: 'user_save_listing_ajax',
			listingId: listingId,
			userId: userId,
		},
		success: function (data) {
			console.log(data);
			response = data;
		}
		
	});
	if(response == '1'){
		jQuery(this).html("<img class='heartsaveicon' src='https://www.homesforsalehermosabeach.com/wp-content/uploads/2023/09/heart-icon-25x23-1.png' />");
		jQuery(this).removeAttr('class');
		jQuery(this).addClass('favoritelisting');
		jQuery(this).addClass('saveasfavoritelink');	
	}
	else if(response == '0'){
		alert("There was an error. Please try again later. Error 1003");
	}
	else if(response == '3'){
		// Something happened bro.
	}
	return false;
});
// End

// Remove favorite from listing page
jQuery('body').on("click", '.favoritelisting', function(){
	var response;
	var listingId = this.getAttribute('data-listingid');
	var userId = this.getAttribute('data-userid');

	jQuery.ajax({
		type: "POST",
		url: "/wp-admin/admin-ajax.php",
		async: false,
		data: {
			action: 'user_remove_favorite_listing_ajax',
			listingId: listingId,
			userId: userId,
		},
		success: function (data) {
			console.log(data);
			response = data;
		}
		
	});
	if(response == '1'){
		jQuery(this).html("<img class='heartsaveicon' src='https://www.southbayrentalagent.com/wp-content/uploads/2023/09/heart-icon-gray-25x23-1.png' />");
		jQuery(this).removeAttr('class');
		jQuery(this).addClass('saveasfavoritelink');
		jQuery(this).addClass('savelistingbutton'); 
	}
	else if(response == '0'){
		alert("There was an error. Please try again later. Error 1002");
	}
	else if(response == '3'){
		// Something happened bro.
	}
	return false;
});
// End

// Remove listing as favorite from my favorites
jQuery('.removefavoritelistingbutton').click(function () {
	if(confirm("Are you sure you want to remove this listing as a favorite?")){
		var response;
		var listingId = this.getAttribute('data-listingid');
		var userId = this.getAttribute('data-userid');
		var userLikeId = this.getAttribute('data-userfavid')
		
		jQuery.ajax({
			type: "POST",
			url: "/wp-admin/admin-ajax.php",
			async: false,
			data: {
				action: 'user_remove_favorite_listing_ajax',
				listingId: listingId,
				userId: userId,
				userLikeId: userLikeId
			},
			success: function (data) {
				console.log(data);
				response = data;
			}
		});
		if(response == '1'){
			jQuery(this).closest('.listing-column').hide();
		}
		else if(response == '0'){
			alert("There was a problem removing this listing. Try again later.");
		}
		else{
			alert(response);
		}
		return false;
	}
	return false;
});
// End

});