<?php
/**
 * Vote Card Template for Pixelio Cart Abandon Recovery More
 *
 * This file displays a voting card allowing users to vote for
 * the next integration to be added to the plugin.
 *
 * PHP version 8.x
 *
 * @package   PixelioCartAbandonRecoveryMore
 * @author    Pixelio
 * @license   GPL-2.0-or-later
 * @link      https://www.pixelio.com/
 */

use PixelioCartAbandonRecoveryMore\Admin\Views\Reusable\Pixelio_Cart_Abandon_Recovery_More_Admin_Loader_Spinner;
use PixelioCartAbandonRecoveryMore\Admin\Views\Reusable\Pixelio_Cart_Abandon_Recovery_More_Header;
use PixelioCartAbandonRecoveryMore\Admin\Views\Reusable\Pixelio_Cart_Abandon_Recovery_More_Overlay;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

if (!defined('ABSPATH')) exit;
$pixelio_util = new Pixelio_Cart_Abandon_Recovery_More_Util(new Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper());
$pixelio_cart_abandon_recovery_more_header = new Pixelio_Cart_Abandon_Recovery_More_Header($pixelio_util);
$pixelio_cart_abandon_recovery_more_header->pixelio_cart_abandon_recovery_more_get_views();
$pixelio_cart_abandon_recovery_more_spinner = new Pixelio_Cart_Abandon_Recovery_More_Admin_Loader_Spinner(__("Vote is ongoing. Please wait", "rivio-ai-cart-recovery-marketing-email-automation"));
$pixelio_cart_abandon_recovery_more_spinner->pixelio_cart_abandon_recovery_more_get_views();
?>


<div class="pixelio-cart-abandon-recovery-more-vote-card pixelio-cart-abandon-recovery-more-card <?php echo get_option('pixelio_api_key', '') !== '' ? 'pixelio-cart-abandon-recovery-more-vote-card-with-api-key' : ''; ?>">
	<?php $pixelio_cart_abandon_recovery_more_overlay = new Pixelio_Cart_Abandon_Recovery_More_Overlay();
	$pixelio_cart_abandon_recovery_more_overlay->pixelio_cart_abandon_recovery_more_get_views(); ?>
	<?php if (get_option('pixelio_api_key', '') != ""): ?>
		<h2><?php esc_html_e('Vote for the future integration', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></h2>

		<div class="pixelio-cart-abandon-recovery-more-integration-options">
			<?php
			$pixelio_cart_abandon_recovery_more_api_key = esc_html(get_option('pixelio_api_key', ''));
			$pixelio_cart_abandon_recovery_more_response = $pixelio_util->pixelio_cart_abandon_recovery_more_call_api($pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'available_vote_for_next_integration?userId=' . rawurlencode(get_current_user_id()), 'GET');

			if (false === $pixelio_cart_abandon_recovery_more_response) {
				die();
			}
			$pixelio_cart_abandon_recovery_more_data = $pixelio_cart_abandon_recovery_more_response;

			$pixelio_cart_abandon_recovery_more_already_voted = false;
			if (isset($pixelio_cart_abandon_recovery_more_data) && !is_wp_error($pixelio_cart_abandon_recovery_more_data) && isset($pixelio_cart_abandon_recovery_more_data['success']) && true === $pixelio_cart_abandon_recovery_more_data['success']) {


				foreach ($pixelio_cart_abandon_recovery_more_data['integrations'] as $pixelio_cart_abandon_recovery_more_already_item) {
					if (!empty($pixelio_cart_abandon_recovery_more_already_item['alreadyVoted'])) {
						$pixelio_cart_abandon_recovery_more_already_voted = true;
						break;
					}
				}

				if ($pixelio_cart_abandon_recovery_more_already_voted) { ?>

					<p class="pixelio-cart-abandon-recovery-more-already-voted"><?php esc_html_e('Thank you for your vote and see you soon on the next integrations vote.', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></p>
				<?php } else {
					foreach ($pixelio_cart_abandon_recovery_more_data['integrations'] as $pixelio_cart_abandon_recovery_more_integration) {
						echo wp_kses("<button type='button' class='pixelio-cart-abandon-recovery-more-integration-option' data-value='" . esc_attr($pixelio_cart_abandon_recovery_more_integration['name']) . "'>" . esc_attr($pixelio_cart_abandon_recovery_more_integration['name']) . '</button>', Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_cart_abandon_recovery_more_allowed_html);
					}
					?>
					<div class="pixelio-cart-abandon-recovery-more-other-input-container"
						 style="display:none; margin-top:10px;">
						<input type="text" class="pixelio-cart-abandon-recovery-more-other-input"
							   placeholder="<?php esc_attr_e('Precise your choice', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>"/>
					</div>

					<button
						class="pixelio-cart-abandon-recovery-more-submit-vote"><?php esc_attr_e('Submit', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></button>
					<?php
				}

			} else {
				$pixelio_cart_abandon_recovery_more_already_voted = true;
				esc_html_e('Impossible to retrieve the intégration, try it soon.', 'rivio-ai-cart-recovery-marketing-email-automation');
			}


			?>

		</div>

	<?php endif; ?>
</div>
