<?php
/**
 * Displays review notice.
 *
 * @package SWPTLS
 */

// If direct access than exit the file.
defined( 'ABSPATH' ) || exit;

$rating = ECRE_ASSETS . '/icons/rating-mode.svg';

?>

<div class="ecre-ratting-banner">
	<div class="banner-content" data-value="hide_notice">
		<span class="ecre-ratting-open"></span>
		<div class="ecre-influencer-image">
			<img class="ecre-image-icon" src="<?php echo esc_url( $rating ); ?>" alt="">
		</div>
		<div class="ecre-influencer-wrapper">
			<h3 class="rating-heading"><?php esc_html_e( 'Seems like ', 'echo-rewards' ); ?><span class="echo-rewards-plugin-title"><?php esc_html_e( 'EchoRewards', 'echo-rewards' ); ?></span><?php esc_html_e( ' is bringing you value 🥳', 'echo-rewards' ); ?></h3>

			<p class="review-notice"><?php esc_html_e( "Hi there! You've been using EchoRewards for a while. Would you consider leaving us a 😍 5-star review? Your feedback will help us to develop better features and spread the word.", 'echo-rewards' ); ?></p>
			<div class="link-wrapper notice-actions">
				<h3><?php esc_attr_e( 'Please Rate Us:', 'echo-rewards' ); ?></h3>
				<div class="bottom-fields">
					<div class="rating-container">
						<span class="ratting-yellow-icon 1"></span>
						<span class="ratting-yellow-icon 2"></span>
						<span class="ratting-yellow-icon 3"></span>
						<span class="ratting-yellow-icon 4"></span>
						<span class="ratting-yellow-icon 5"></span>
					</div> 
					<a href="#" class="hide_notice already-did-hook" data-value="hide_notice"><?php esc_html_e( 'I already did it', 'echo-rewards' ); ?></a>
				</div>
			</div>
		</div>
	</div>
	<!-- style="display: none;" -->
	<div id="popup1" class="ratting_popup-container" style="display: none;"> 
		<div class="ratting_popup-content">
			<a href="#" target="_blank" class="close ratting_close_button cross_to_close">&times;</a>
			<div class="ratting_first_section2">
				<div class="ratting_popup_wrap">
					<h4><?php esc_html_e( 'Would you like to be remind in the future?', 'echo-rewards' ); ?></h4>
				</div>
				<div class="ratting_select-wrapper">
					<span class="remind-title"><?php esc_html_e( 'Remind Me After:', 'echo-rewards' ); ?></span>
					<div class="custom-select">
						<span class="selected-option" data-value="7"><?php esc_html_e( '7 Days', 'echo-rewards' ); ?>
						</span>
						<ul class="options-list">
							<li data-value="7"><?php esc_html_e( '7 Days', 'echo-rewards' ); ?></li>
							<li data-value="14"><?php esc_html_e( '14 Days', 'echo-rewards' ); ?></li>
							<li class="remind-me-text" data-value="hide_notice"><?php esc_html_e( 'Remind me never', 'echo-rewards' ); ?></li>
						</ul>
					</div>

					<div class="promo_close_btn_submit">
						<button class="ratting_custom-button ratting_submit_button2 promo_close_btn"><?php esc_html_e( 'Ok', 'echo-rewards' ); ?></button>
					</div>
				</div>
			</div>
		</div>
	</div>

</div>


<script>

jQuery(document).ready(function($) {
	/**
	 * Custom select
	 */
	$(document).ready(function() {

		function adjustButtonMargin() {
			var isOpen = $('.ecre-ratting-banner .custom-select').hasClass('open');
			if (isOpen) {
				var isVisible = $('.ecre-ratting-banner .options-list').is(':visible');
				if (isVisible) {
					$('.ecre-ratting-banner .ratting_custom-button').css('margin-top', '185px');
				} else {
					$('.ecre-ratting-banner .ratting_custom-button').css('margin-top', 'unset');
				}
			} else {
				$('.ecre-ratting-banner .ratting_custom-button').css('margin-top', 'unset');
			}
		}

		$('.ecre-ratting-banner .selected-option').click(function() {
			$('.ecre-ratting-banner .options-list').toggle();
			$('.ecre-ratting-banner .custom-select').toggleClass('open');
			adjustButtonMargin();
		});

		$('.ecre-ratting-banner .options-list li').click(function() {
			var selectedValue = $(this).attr('data-value');

			// Update the data-value attribute of the selected-option span
			$('.ecre-ratting-banner .selected-option').attr('data-value', selectedValue);
			// Update the text content of the selected-option span
			$('.ecre-ratting-banner .selected-option').text($(this).text());
			$('.ecre-ratting-banner .options-list').hide();
			$('.ecre-ratting-banner .custom-select').removeClass('open');
			$('.ecre-ratting-banner .ratting_custom-button').css('margin-top', 'unset');

		});

		// Close the dropdown when clicking outside of it
		$(document).click(function(event) {
			if (!$(event.target).closest('.ecre-ratting-banner .custom-select').length) {
				$('.ecre-ratting-banner .options-list').hide();
				$('.ecre-ratting-banner .custom-select').removeClass('open');
				adjustButtonMargin();
			}
		});

		// Handle adjustment when the options-list is toggled
		$('.ecre-ratting-banner .options-list').on('toggle', function() {
			adjustButtonMargin();
		});
	});


	// I already did button .
	$('.ecre-ratting-banner .hide_notice').click(e => {
		e.preventDefault();
		$.ajax({
			type: "POST",
			url: "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>",
			data: {
				action: 'ecre_notice_action',
				nonce: '<?php echo esc_attr( wp_create_nonce( 'ecre_notices_nonce' ) ); ?>',
				info: {
					type: 'hide_notice'
				},
				actionType: 'review_notice'
			},
			success: response => {
				if (response.data.response_type === 'success') {
					$('.ecre-ratting-banner').slideUp();
				}
			}
		});
		
	})

	/* Open popup when click the X icon*/
	$('.ecre-ratting-banner .ecre-ratting-open').click(function(e) {
		// Prevent the event from reaching the document.
		e.stopPropagation(); 
		$('.ecre-ratting-banner #popup1').show();
	});

	// Close the popup when clicking outside of ratting_popup-content.
	$(document).on('click', function(event) {
		if (!$(event.target).closest('.ecre-ratting-banner .ratting_popup-content').length && !$(event.target).is('.cross_to_close')) {
			// Check if the click is outside the popup content and not on the close button
			$('.ecre-ratting-banner #popup1').hide();
		}
	});

	// Close the popup when clicking on the close button.
	$('.ecre-ratting-banner .cross_to_close').click(function(e) {
		e.preventDefault();
		$('#popup1').hide();
	});

	// When click in popup ok or X icon. 
	$('.ecre-ratting-banner .ratting_popup-container .promo_close_btn').click(e => {
		e.preventDefault();
		// Get the selected option value from the dropdown.
		dataValue = $('.ecre-ratting-banner .selected-option').attr('data-value');

		// Perform AJAX request.
		$.ajax({
			type: "POST",
			url: "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>",
			data: {
				action: 'ecre_notice_action',
				nonce: '<?php echo esc_attr( wp_create_nonce( 'ecre_notices_nonce' ) ); ?>',
				info: {
					type: 'reminder',
					value: dataValue
				},
				actionType: 'review_notice'
			},
			success: response => {
				if (response.data.response_type === 'success') {
					$('.ecre-ratting-banner').slideUp();
				}
			}
		});
	});


	// Ratting 
	$('.ecre-ratting-banner .ratting-yellow-icon').hover(
		function() {
			// Remove existing orange and gray classes
			$('.ecre-ratting-banner .ratting-yellow-icon').removeClass('ratting-orange-icon ratting-gray-icon');
			
			// Get the index of the hovered star
			var currentIndex = $(this).index() + 1;

			// Add orange class to stars up to the hovered one
			$('.ecre-ratting-banner .ratting-yellow-icon:lt(' + currentIndex + ')').addClass('ratting-orange-icon');

			// Add gray class to the remaining stars
			$('.ecre-ratting-banner .ratting-yellow-icon:gt(' + (currentIndex - 1) + ')').addClass('ratting-gray-icon');
		},
		function() {
			// Remove all hover-related classes and set to yellow
			$('.ecre-ratting-banner .ratting-yellow-icon').removeClass('ratting-orange-icon ratting-gray-icon').addClass('ratting-yellow-icon');
		}
	);

	// Add click event to stars
	$('.ecre-ratting-banner .ratting-yellow-icon').click(function() {
		// Add 'rated' class to the clicked stars
		$(this).addClass('rated');
	});

	// Click event for the rating container
	$('.ecre-ratting-banner .rating-container').click(function() {
		var orangeIcons = $('.ecre-ratting-banner .ratting-orange-icon').length;

		if (orangeIcons === 5) {
			window.open('https://cutt.ly/YewdDtai', '_blank');
		} else {
			window.open('https://wordpress.org/support/plugin/echo-rewards/', '_blank');
		}
	});

});
</script>
