<?php
/**
 * Exit Popup / Email Capture Template for Pixelio Cart Abandon Recovery More plugin.
 *
 * One merged template for both modes:
 * 1. Exit popup with discount / coupon.
 * 2. Email capture popup after add-to-cart.
 *
 * PHP version 8.0+
 *
 * @package PixelioCartAbandonRecoveryMore
 */

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_is_capture_email = isset( $pixelio_cart_abandon_recovery_more_capture_email );
$pixelio_is_edit          = isset( $this->edit ) && $this->edit;
$pixelio_has_gdpr         = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( 'pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox' );
$pixelio_has_promo_code   = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( 'pixelio-cart-abandon-recovery-more-use-promo-code-checkbox' );

$pixelio_discount_option_name = $pixelio_is_capture_email
	? 'pixelio-cart-abandon-recovery-more-use-promo-code-percent'
	: 'pixelio-cart-abandon-recovery-more-exit-popup-discount';

$pixelio_discount_value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_get_input_content( $pixelio_discount_option_name, false );
$pixelio_discount_value = ( '' !== $pixelio_discount_value && 0 !== $pixelio_discount_value ) ? $pixelio_discount_value : '15';

$pixelio_container_classes = [
	'pixelio-cart-abandon-recovery-more-pop-up-container',
];

if ( $pixelio_is_capture_email ) {
	$pixelio_container_classes[] = 'pixelio-cart-abandon-recovery-more-pop-up-container-email-capture';
}

if ( is_front_page() ) {
	$pixelio_container_classes[] = 'pixelio-cart-abandon-recovery-more-pop-up-container-front-page';
}

if ( is_checkout() || is_cart() ) {
	$pixelio_container_classes[] = 'pixelio-cart-abandon-recovery-more-is-cart-or-checkout-page';
}

$pixelio_form_classes = [
	'pixelio-cart-abandon-recovery-more-popup-form-common',
];

if ( $pixelio_is_capture_email ) {
	$pixelio_form_classes[] = 'pixelio-cart-abandon-recovery-more-pop-up-form';
} else {
	$pixelio_form_classes[] = 'pixelio-cart-abandon-recovery-more-exit-popup-form';
}

$pixelio_email_input_id    = $pixelio_is_capture_email ? 'pixelio-cart-abandon-recovery-more-pop-up-email' : 'pixelio-cart-abandon-recovery-more-email';
$pixelio_email_input_name  = $pixelio_is_capture_email ? 'pixelio-cart-abandon-recovery-more-pop-up-email' : 'pixelio-cart-abandon-recovery-more-email';
$pixelio_email_placeholder = $pixelio_is_capture_email ? __( 'Enter your email', 'rivio-ai-cart-recovery-marketing-email-automation' ) : 'exemple@domaine.com';

$pixelio_rgpd_input_id   = $pixelio_is_capture_email ? 'pixelio-cart-abandon-recovery-more-pop-up-rgpd' : 'pixelio-abandon-recovery-more-rgpd';
$pixelio_rgpd_input_name = $pixelio_is_capture_email ? 'pixelio-cart-abandon-recovery-more-pop-up-rgpd' : 'pixelio-abandon-recovery-more-rgpd';
$pixelio_rgpd_text       = $pixelio_is_capture_email
	? __( 'I agree to give the ability to follow my abandon cart in order to recovered it later & to subscribe to the newsletters', 'rivio-ai-cart-recovery-marketing-email-automation' )
	: __( 'I agree to the processing of my data and sign up for newsletters.', 'rivio-ai-cart-recovery-marketing-email-automation' );
?>

<div class="<?php echo esc_attr( implode( ' ', $pixelio_container_classes ) ); ?>">
	<a class="pixelio-cart-abandon-recovery-more-pop-up-close">
		<ion-icon name="close">X</ion-icon>
	</a>

	<div class="pixelio-cart-abandon-recovery-pop-up-body">
		<div class="pixelio-cart-abandon-recovery-more-pop-up-header">
			<div class="pixelio-cart-abandon-recovery-more-pop-up-header-title-container">
				<h2 class="pixelio-cart-abandon-recovery-more-pop-up-header-title" <?php echo $pixelio_is_edit ? 'contenteditable="true"' : ''; ?>>
					<?php if ( $pixelio_is_capture_email ) : ?>
						<?php esc_html_e( 'Hey !', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
					<?php else : ?>
						<span class="pixelio-cart-abandon-recovery-more-exit-pop-up-discount"><?php echo esc_html( $pixelio_discount_value ); ?>%</span>
						<?php esc_html_e( 'OFF JUST FOR YOU', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
					<?php endif; ?>
				</h2>

				<?php if ( $pixelio_is_capture_email ) : ?>
					<h4 class="pixelio-cart-abandon-recovery-more-pop-up-header-title-action" <?php echo $pixelio_is_edit ? 'contenteditable="true"' : ''; ?>>
						<?php if ( $pixelio_has_promo_code ) : ?>
							<?php echo esc_html( $pixelio_discount_value ); ?>% <?php esc_html_e( 'OFF JUST FOR YOU', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
						<?php else : ?>
							<?php esc_html_e( 'Don\'t lose your cart!', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
						<?php endif; ?>
					</h4>
				<?php else : ?>
					<p class="pixelio-cart-abandon-recovery-more-pop-up-header-title-action" <?php echo $pixelio_is_edit ? 'contenteditable="true"' : ''; ?>>
						<?php esc_html_e( 'If you complete your order in..', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
					</p>
				<?php endif; ?>
			</div>
		</div>

		<div class="pixelio-cart-abandon-recovery-more-pop-up-middle <?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ) : ''; ?>">
			<div class="pixelio-cart-abandon-recovery-more-pop-up-timer"><p class="pixelio-cart-abandon-recovery-more-pop-up-timer-content">1</p></div>
			<div class="pixelio-cart-abandon-recovery-more-pop-up-timer"><p class="pixelio-cart-abandon-recovery-more-pop-up-timer-content">5</p></div>
			<div class="pixelio-cart-abandon-recovery-more-pop-up-timer-dot"><p class="pixelio-cart-abandon-recovery-more-pop-up-timer-content">:</p></div>
			<div class="pixelio-cart-abandon-recovery-more-pop-up-timer"><p class="pixelio-cart-abandon-recovery-more-pop-up-timer-content">0</p></div>
			<div class="pixelio-cart-abandon-recovery-more-pop-up-timer"><p class="pixelio-cart-abandon-recovery-more-pop-up-timer-content">0</p></div>
		</div>

		<input type="hidden" class="pixelio-cart-abandon-recovery-more-newsletters-submit-background-color" name="pixelio-cart-abandon-recovery-more-newsletters-submit-background-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-newsletters-submit-background-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-newsletters-submit-text-color" name="pixelio-cart-abandon-recovery-more-newsletters-submit-text-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-newsletters-submit-text-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-newsletters-submit-hover-color" name="pixelio-cart-abandon-recovery-more-newsletters-submit-hover-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-newsletters-submit-hover-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup-text-color" name="pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup-text-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup-text-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color" name="pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color" name="pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-hover-color" name="pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-hover-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-hover-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-timer-content-text-color" name="pixelio-cart-abandon-recovery-more-pop-up-timer-content-text-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-timer-content-text-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-header-title-text-color" name="pixelio-cart-abandon-recovery-more-pop-up-header-title-text-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-header-title-text-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-header-title-action-text-color" name="pixelio-cart-abandon-recovery-more-pop-up-header-title-action-text-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-header-title-action-text-color' ) ); ?>">
		<input type="hidden" class="pixelio-cart-abandon-recovery-more-pop-up-header-background-color" name="pixelio-cart-abandon-recovery-more-pop-up-header-background-color" value="<?php echo esc_attr( get_option( 'pixelio-cart-abandon-recovery-more-pop-up-header-background-color' ) ); ?>">

		<div class="pixelio-cart-abandon-recovery-more-pop-up-footer">
			<?php if ( $pixelio_has_promo_code ) : ?>
				<div class="pixelio-cart-abandon-recovery-more-preview-container-exit-popup pixelio-cart-abandon-recovery-more-hide-it coupon-container">

					<p>
						<?php esc_html_e(
							'Thank you for your subscription. Here is your promo code:',
							'rivio-ai-cart-recovery-marketing-email-automation'
						); ?>
					</p>

					<span class="pixelio-cart-abandon-recovery-more-coupon-code-text">
			PROMO-XXXXXX
		</span>

					<button
						class="pixelio-cart-abandon-recovery-more-copy-coupon-btn"
						type="button">

						<?php esc_html_e(
							'Copy',
							'rivio-ai-cart-recovery-marketing-email-automation'
						); ?>

					</button>

					<p class="pixelio-cart-abandon-recovery-more-coupon-expiration">
						<?php esc_html_e(
							'Hurry, it\'s only valid for 15 minutes',
							'rivio-ai-cart-recovery-marketing-email-automation'
						); ?>
					</p>

				</div>
			<?php endif; ?>

			<div class="<?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-pop-up-form-container' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-hide-div-if-form-showed' ); ?>">
				<form action="/" method="post" class="<?php echo esc_attr( implode( ' ', $pixelio_form_classes ) ); ?>">
					<div class="pixelio-cart-abandon-recovery-more-popup-field-group <?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-pop-up-personal-email' ) : ''; ?>">
						<label for="<?php echo esc_attr( $pixelio_email_input_id ); ?>" class="pixelio-cart-abandon-recovery-more-popup-label pixelio-cart-abandon-recovery-more-input-label">
							<?php echo $pixelio_is_capture_email ? esc_html__( 'Your email', 'rivio-ai-cart-recovery-marketing-email-automation' ) : esc_html__( 'Email:', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
						</label>

						<input
							type="email"
							id="<?php echo esc_attr( $pixelio_email_input_id ); ?>"
							name="<?php echo esc_attr( $pixelio_email_input_name ); ?>"
							class="pixelio-cart-abandon-recovery-more-popup-input <?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-pop-up-email pixelio-cart-abandon-recovery-more-input-text' ) : ''; ?>"
							placeholder="<?php echo esc_attr( $pixelio_email_placeholder ); ?>"
							autocomplete="email"
							required
						>

						<?php wp_nonce_field( 'pixelio-cart-abandon-recovery-more-pop-up-rgpd_nonce', 'pixelio-cart-abandon-recovery-more-pop-up-rgpd_nonce' ); ?>

						<?php if ( $pixelio_has_gdpr ) : ?>
							<label for="<?php echo esc_attr( $pixelio_rgpd_input_id ); ?>" class="pixelio-cart-abandon-recovery-more-popup-rgpd-label <?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-pop-up-rgpd-label' ) : ''; ?>">
								<input
									type="checkbox"
									id="<?php echo esc_attr( $pixelio_rgpd_input_id ); ?>"
									name="<?php echo esc_attr( $pixelio_rgpd_input_name ); ?>"
									class="pixelio-cart-abandon-recovery-more-popup-rgpd <?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-pop-up-rgpd' ) : ''; ?>"
									required
								>
								<span><?php echo esc_html( $pixelio_rgpd_text ); ?></span>
							</label>
						<?php endif; ?>
					</div>

					<?php if ( $pixelio_is_capture_email ) : ?>
						<input
							type="submit"
							class="pixelio-cart-abandon-recovery-more-popup-submit pixelio-cart-abandon-recovery-more-gradient pixelio-cart-abandon-recovery-more-pixelio-style-button pixelio-cart-abandon-recovery-more-newsletters-submit"
							value="<?php echo esc_attr( $pixelio_has_promo_code ? sprintf( __( 'GET MY %s%%', 'rivio-ai-cart-recovery-marketing-email-automation' ), $pixelio_discount_value ) : __( 'SAVE MY CART', 'rivio-ai-cart-recovery-marketing-email-automation' ) ); ?>"
						>
					<?php else : ?>
						<button
							type="submit"
							class="pixelio-cart-abandon-recovery-more-popup-submit pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup pixelio-cart-abandon-recovery-more-gradient pixelio-cart-abandon-recovery-more-pixelio-style-button"
							<?php echo $pixelio_is_edit ? 'contenteditable="true"' : ''; ?>
						>
							<?php esc_html_e( 'GET MY ', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
							<span class="pixelio-cart-abandon-recovery-more-exit-pop-up-discount" <?php echo $pixelio_is_edit ? 'contenteditable="true"' : ''; ?>><?php echo esc_html( $pixelio_discount_value ); ?>%</span>
							<?php esc_html_e( ' OFF ', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
						</button>
					<?php endif; ?>

					<?php if ( $pixelio_is_edit ) : ?>
						<div class="<?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-newsletters-submit-button-state' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-pop-up-yes-button-state' ); ?> pixelio-cart-abandon-recovery-more-hide-it">
							<button type="button" class="<?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-newsletters-submit-yes-background' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-popup-yes-background' ); ?> pixelio-cart-abandon-recovery-more-gradient pixelio-cart-abandon-recovery-more-pixelio-style-button"><?php esc_attr_e( 'Background', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></button>
							<button type="button" class="<?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-newsletters-submit-yes-text' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-popup-yes-text' ); ?> pixelio-cart-abandon-recovery-more-gradient pixelio-cart-abandon-recovery-more-pixelio-style-button"><?php esc_attr_e( 'Text', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></button>
							<button type="button" class="<?php echo $pixelio_is_capture_email ? esc_attr( 'pixelio-cart-abandon-recovery-more-newsletters-submit-hover' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-popup-yes-hover' ); ?> pixelio-cart-abandon-recovery-more-gradient pixelio-cart-abandon-recovery-more-pixelio-style-button"><?php esc_attr_e( 'Hover', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></button>
						</div>
					<?php endif; ?>
				</form>

					<a class="pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup" <?php echo $pixelio_is_edit ? 'contenteditable="true"' : ''; ?>>
						<?php esc_attr_e( "No, thanks. I'll rather pay full price", 'rivio-ai-cart-recovery-marketing-email-automation' ); ?>
					</a>
			</div>
		</div>
	</div>
</div>
