<?php
/**
 * The inline upsell widget's frontend template - add-ons, upgrades, bundle/tier progress, and summary totals.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/public/partials
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'nudgesells_render_product_title' ) ) {
	
	/**
	 * Nudgesells render product title.
	 *
	 * @param mixed $label
	 * @param mixed $permalink
	 * @param mixed $link_mode
	 * @param int $product_id
	 * @param string $extra_class
	 *
	 * @return void
	 * @since 1.0.0
	 */
	function nudgesells_render_product_title( $label, $permalink, $link_mode, $product_id = 0, $extra_class = '' ) {
		if ( 'none' === $link_mode || ! $permalink ) {
			echo esc_html( $label );
			return;
		}
		$class = 'nudgesells-product-link';
		if ( 'quick_view' === $link_mode ) {
			$class .= ' nudgesells-quick-view';
			if ( $extra_class ) {
				$class .= ' ' . $extra_class;
			}
		}
		printf(
			'<a href="%1$s" class="%2$s" target="_blank" rel="noopener noreferrer" data-product_id="%3$d">%4$s</a>',
			esc_url( $permalink ),
			esc_attr( $class ),
			absint( $product_id ),
			esc_html( $label )
		);
	}
}

if ( ! function_exists( 'nudgesells_render_addon_price' ) ) {

	/**
	 * Nudgesells render addon price.
	 *
	 * @param array $addon
	 *
	 * @return void
	 * @since 1.0.0
	 */
	function nudgesells_render_addon_price( $addon ) {
		?>
		<span class="nudgesells-addon-price">
			<?php if ( ! $addon['is_variable'] ) : ?>
				<span class="nudgesells-addon-price-discounted"><?php echo wp_kses_post( $addon['price_display_discounted'] ); ?></span>
				<?php if ( $addon['price_discounted'] < $addon['price_original'] ) : ?>
					<del class="nudgesells-addon-price-original"><?php echo wp_kses_post( $addon['price_display_original'] ); ?></del>
				<?php endif; ?>
				<?php if ( $addon['savings_label'] ) : ?>
					<span class="nudgesells-savings-badge"><?php echo wp_kses_post( $addon['savings_label'] ); ?></span>
				<?php endif; ?>
			<?php else : ?>
				<?php
				/* translators: %s: lowest variation price, e.g. "From $19.00" */
				echo wp_kses_post( $addon['price_display_discounted'] ? sprintf( esc_html__( 'From %s', 'nudgesells' ), $addon['price_display_discounted'] ) : '' );
				?>
			<?php endif; ?>
		</span>
		<?php
	}
}

if ( ! function_exists( 'nudgesells_render_addon_variations' ) ) {

	/**
	 * Nudgesells render addon variations.
	 *
	 * @param array $addon
	 *
	 * @return void
	 * @since 1.0.0
	 */
	function nudgesells_render_addon_variations( $addon ) {
		if ( empty( $addon['is_variable'] ) || empty( $addon['variations'] ) ) {
			return;
		}
		$addon_product = wc_get_product( $addon['product_id'] );
		?>
		<span class="nudgesells-addon-variations">
			<?php foreach ( $addon['variation_attributes'] as $attribute_name => $options ) : ?>
				<span class="nudgesells-variation-attribute-group">
					<label class="nudgesells-variation-attribute-label" for="nudgesells-addon-<?php echo esc_attr( $addon['product_id'] . '-' . sanitize_title( $attribute_name ) ); ?>"><?php echo esc_html( wc_attribute_label( $attribute_name, $addon_product ) ); ?></label>
					<?php
					wc_dropdown_variation_attribute_options( array(
						'options'   => $options,
						'attribute' => $attribute_name,
						'product'   => $addon_product,
						'class'     => 'nudgesells-addon-attribute',
						'id'        => 'nudgesells-addon-' . $addon['product_id'] . '-' . sanitize_title( $attribute_name ),
					) );
					?>
				</span>
			<?php endforeach; ?>
		</span>
		<input type="hidden" class="nudgesells-addon-variation-id" value="" />
		<?php
	}
}

if ( ! function_exists( 'nudgesells_render_addon_qty_control' ) ) {

	/**
	 * Nudgesells render addon qty control.
	 *
	 * @param array $addon
	 * @param bool  $show_qty_stepper
	 *
	 * @return void
	 * @since 1.0.0
	 */
	function nudgesells_render_addon_qty_control( $addon, $show_qty_stepper ) {
		if ( empty( $addon['allow_qty'] ) ) {
			return;
		}
		if ( $show_qty_stepper ) :
			?>
			<span class="nudgesells-qty-stepper">
				<button type="button" class="nudgesells-qty-btn nudgesells-qty-minus" aria-label="<?php esc_attr_e( 'Decrease quantity', 'nudgesells' ); ?>">&minus;</button>
				<input type="number" class="nudgesells-addon-qty" min="0" value="<?php echo esc_attr( $addon['default_value'] ); ?>" />
				<button type="button" class="nudgesells-qty-btn nudgesells-qty-plus" aria-label="<?php esc_attr_e( 'Increase quantity', 'nudgesells' ); ?>">+</button>
			</span>
			<?php
		else :
			?>
			<input type="number" class="nudgesells-addon-qty" min="0" value="<?php echo esc_attr( $addon['default_value'] ); ?>" />
			<?php
		endif;
	}
}

if ( ! function_exists( 'nudgesells_render_section_header' ) ) {
	
	/**
	 * Nudgesells render section header.
	 *
	 * @param mixed $title
	 * @param mixed $subline
	 * @param mixed $icon_url
	 * @param mixed $readmore_text
	 * @param mixed $readmore_url
	 * @param int   $countdown_end_timestamp Unix timestamp to count down to, or 0 to omit the countdown pill.
	 * @param string $countdown_format 'hms' or 'dhms'.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	function nudgesells_render_section_header( $title, $subline, $icon_url, $readmore_text, $readmore_url, $countdown_end_timestamp = 0, $countdown_format = 'hms' ) {
		?>
		<div class="nudgesells-section-header">
			<?php if ( $icon_url ) : ?>
				<img src="<?php echo esc_url( $icon_url ); ?>" alt="" class="nudgesells-section-icon" />
			<?php endif; ?>
			<div class="nudgesells-section-heading-text">
				<p class="nudgesells-section-title" role="heading" aria-level="3">
					<?php echo esc_html( $title ); ?>
					<?php if ( $countdown_end_timestamp > 0 ) : ?>
						<span class="nudgesells-countdown-pill" data-end-timestamp="<?php echo esc_attr( $countdown_end_timestamp ); ?>" data-format="<?php echo esc_attr( $countdown_format ); ?>" role="timer">
							<span class="nudgesells-countdown-dot" aria-hidden="true"></span>
							<span class="nudgesells-countdown-time">00:00:00</span>
						</span>
					<?php endif; ?>
				</p>
				<?php if ( $subline ) : ?>
					<p class="nudgesells-section-subline"><?php echo esc_html( $subline ); ?></p>
				<?php endif; ?>
			</div>
			<?php if ( $readmore_text && $readmore_url ) : ?>
				<a class="nudgesells-section-readmore" href="<?php echo esc_url( $readmore_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $readmore_text ); ?><span class="screen-reader-text"> (<?php esc_html_e( 'opens in a new tab', 'nudgesells' ); ?>)</span></a>
			<?php endif; ?>
		</div>
		<?php
	}
}
?>
<div class="nudgesells-upsell-wrap"
	 role="group"
	 aria-label="<?php esc_attr_e( 'Additional offers for this product', 'nudgesells' ); ?>"
	 data-product-id="<?php echo esc_attr( $product->get_id() ); ?>"
	 data-is-variable-host="<?php echo esc_attr( $product->is_type( array( 'variable', 'variable-subscription' ) ) ? '1' : '0' ); ?>"
	 data-has-qty-filter="<?php echo esc_attr( ! empty( $has_qty_filter ) ? '1' : '0' ); ?>"
	 data-display-style="<?php echo esc_attr( $display_style ); ?>"
	 data-bundle="<?php echo esc_attr( wp_json_encode( $bundle ) ); ?>"
	 data-tiers="<?php echo esc_attr( wp_json_encode( $tiers ) ); ?>">

	<?php
	/**
	 * Kept in sync by nudgesells-public.js so third-party "Ajax add to
	 * cart" plugins still carry the selection along. See
	 * class-nudgesells-native-add-to-cart.php.
	 */
	?>
	<input type="hidden" class="nudgesells-selection-field" name="nudgesells_selection" value="" />
	<input type="hidden" name="nudgesells_selection_nonce" value="<?php echo esc_attr( wp_create_nonce( 'nudgesells_frontend' ) ); ?>" />

	<?php if ( ! empty( $upgrades ) ) : ?>
		<div class="nudgesells-section nudgesells-upgrades">
			<?php nudgesells_render_section_header( $upgrades_title, $upgrades_subline, $upgrades_icon_url, $upgrades_readmore_text, $upgrades_readmore_url, $countdown_end_timestamp, $countdown_format ); ?>
			<label class="nudgesells-upgrade-option nudgesells-upgrade-original">
				<input type="radio" name="nudgesells_upgrade_choice" value="" checked="checked" />
				<?php if ( $show_upgrade_thumbnails && $host_image ) : ?>
					<img src="<?php echo esc_url( $host_image ); ?>" alt="" class="nudgesells-addon-thumb nudgesells-upgrade-thumb" />
				<?php endif; ?>
				<span class="nudgesells-upgrade-body">
					<span class="nudgesells-upgrade-text-wrap">
						<span class="nudgesells-upgrade-text">
							<?php echo esc_html( $upgrades_original_label ); ?>
						</span>
						<?php if ( $upgrades_original_sublabel ) : ?>
							<span class="nudgesells-upgrade-sublabel"><?php echo esc_html( $upgrades_original_sublabel ); ?></span>
						<?php endif; ?>
					</span>
					<strong class="nudgesells-price nudgesells-host-price nudgesells-upgrade-price-end"><?php echo wp_kses_post( $host_price_display ); ?></strong>
				</span>
			</label>
			<?php foreach ( $upgrades as $upgrade ) : ?>
				<div class="nudgesells-upgrade-option<?php echo $upgrade['is_variable'] ? ' nudgesells-upgrade-option--variable' : ''; ?>"
					 data-product-id="<?php echo esc_attr( $upgrade['product_id'] ); ?>"
					 data-is-variable="<?php echo esc_attr( $upgrade['is_variable'] ? '1' : '0' ); ?>"
					 data-variations="<?php echo esc_attr( wp_json_encode( $upgrade['variations'] ) ); ?>"
					 data-savings-label="<?php echo esc_attr( $upgrade['savings_label'] ); ?>">
					<label>
						<input type="radio" name="nudgesells_upgrade_choice" value="<?php echo esc_attr( $upgrade['product_id'] ); ?>" />
						<?php if ( $show_upgrade_thumbnails && $upgrade['image'] ) : ?>
							<img src="<?php echo esc_url( $upgrade['image'] ); ?>" alt="" class="nudgesells-addon-thumb nudgesells-upgrade-thumb" />
						<?php endif; ?>
						<span class="nudgesells-upgrade-body">
							<span class="nudgesells-upgrade-text-wrap">
								<?php if ( ! empty( $upgrade['badge_label'] ) ) : ?>
									<span class="nudgesells-addon-badge"><?php echo esc_html( $upgrade['badge_label'] ); ?></span>
								<?php endif; ?>
								<span class="nudgesells-upgrade-text">
									<?php nudgesells_render_product_title( $upgrade['label'], $upgrade['permalink'], $title_link_mode, $upgrade['link_product_id'], $quick_view_css_class ); ?>
								</span>
								<?php if ( ! empty( $upgrade['sublabel'] ) ) : ?>
									<span class="nudgesells-upgrade-sublabel"><?php echo esc_html( $upgrade['sublabel'] ); ?></span>
								<?php endif; ?>
								<?php if ( $upgrade['is_variable'] ) : ?>
									<?php $upgrade_product = wc_get_product( $upgrade['product_id'] ); ?>
									<span class="nudgesells-upgrade-variations">
										<?php foreach ( $upgrade['variation_attributes'] as $attribute_name => $options ) : ?>
											<span class="nudgesells-variation-attribute-group">
												<label class="nudgesells-variation-attribute-label" for="nudgesells-upgrade-<?php echo esc_attr( $upgrade['product_id'] . '-' . sanitize_title( $attribute_name ) ); ?>"><?php echo esc_html( wc_attribute_label( $attribute_name, $upgrade_product ) ); ?></label>
												<?php
												wc_dropdown_variation_attribute_options( array(
													'options'   => $options,
													'attribute' => $attribute_name,
													'product'   => $upgrade_product,
													'class'     => 'nudgesells-upgrade-attribute',
													'id'        => 'nudgesells-upgrade-' . $upgrade['product_id'] . '-' . sanitize_title( $attribute_name ),
												) );
												?>
											</span>
										<?php endforeach; ?>
									</span>
									<input type="hidden" class="nudgesells-upgrade-variation-id" value="" />
								<?php endif; ?>
							</span>
							<strong class="nudgesells-price nudgesells-upgrade-price nudgesells-upgrade-price-end">
								<?php
								/* translators: %s: lowest variation price, e.g. "From $19.00" */
								echo wp_kses_post( $upgrade['is_variable'] ? sprintf( esc_html__( 'From %s', 'nudgesells' ), $upgrade['price_display_discounted'] ) : $upgrade['price_display_discounted'] );
								?>
								<?php if ( ! $upgrade['is_variable'] ) : ?>
									<del class="nudgesells-price-original"><?php echo wp_kses_post( $upgrade['price_display_original'] ); ?></del>
									<?php if ( $upgrade['savings_label'] ) : ?>
										<span class="nudgesells-savings-badge"><?php echo wp_kses_post( $upgrade['savings_label'] ); ?></span>
									<?php endif; ?>
								<?php endif; ?>
							</strong>
						</span>
					</label>
				</div>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $addons ) ) : ?>
		<div class="nudgesells-section nudgesells-addons">
			<?php nudgesells_render_section_header( $addons_title, $addons_subline, $addons_icon_url, $addons_readmore_text, $addons_readmore_url, empty( $upgrades ) ? $countdown_end_timestamp : 0, $countdown_format ); ?>
			<div class="nudgesells-addon-list nudgesells-addon-list--<?php echo esc_attr( $display_style ); ?>">
				<?php foreach ( $addons as $addon ) : ?>
					<div class="nudgesells-addon-row"
						 data-product-id="<?php echo esc_attr( $addon['product_id'] ); ?>"
						 data-is-variable="<?php echo esc_attr( $addon['is_variable'] ? '1' : '0' ); ?>"
						 data-allow-qty="<?php echo esc_attr( $addon['allow_qty'] ? '1' : '0' ); ?>"
						 data-price-original="<?php echo esc_attr( $addon['price_original'] ); ?>"
						 data-price-discounted="<?php echo esc_attr( $addon['price_discounted'] ); ?>"
						 data-variations="<?php echo esc_attr( wp_json_encode( $addon['variations'] ) ); ?>"
						 data-savings-label="<?php echo esc_attr( $addon['savings_label'] ); ?>">

						<?php if ( $addon['allow_qty'] ) : ?>
				<div class="nudgesells-addon-check nudgesells-addon-check--qty">
					<?php if ( $addon['image'] ) : ?>
						<img src="<?php echo esc_url( $addon['image'] ); ?>" alt="" class="nudgesells-addon-thumb" />
					<?php endif; ?>
					<?php if ( 'list' === $display_style ) : ?>
						<span class="nudgesells-addon-body">
							<span class="nudgesells-addon-info">
								<?php if ( ! empty( $addon['badge_label'] ) ) : ?>
									<span class="nudgesells-addon-badge"><?php echo esc_html( $addon['badge_label'] ); ?></span>
								<?php endif; ?>
								<span class="nudgesells-addon-name"><?php nudgesells_render_product_title( $addon['label'], $addon['permalink'], $title_link_mode, $addon['link_product_id'], $quick_view_css_class ); ?></span>
								<?php if ( ! empty( $addon['sublabel'] ) ) : ?>
									<span class="nudgesells-addon-subline"><?php echo esc_html( $addon['sublabel'] ); ?></span>
								<?php endif; ?>
								<?php nudgesells_render_addon_variations( $addon ); ?>
								<?php nudgesells_render_addon_price( $addon ); ?>
							</span>
							<?php nudgesells_render_addon_qty_control( $addon, $show_qty_stepper ); ?>
						</span>
					<?php else : ?>
						<span class="nudgesells-addon-info">
							<?php if ( ! empty( $addon['badge_label'] ) ) : ?>
								<span class="nudgesells-addon-badge"><?php echo esc_html( $addon['badge_label'] ); ?></span>
							<?php endif; ?>
							<span class="nudgesells-addon-name"><?php nudgesells_render_product_title( $addon['label'], $addon['permalink'], $title_link_mode, $addon['link_product_id'], $quick_view_css_class ); ?></span>
							<?php if ( ! empty( $addon['sublabel'] ) ) : ?>
								<span class="nudgesells-addon-subline"><?php echo esc_html( $addon['sublabel'] ); ?></span>
							<?php endif; ?>
							<?php nudgesells_render_addon_price( $addon ); ?>
						</span>
					<?php endif; ?>
				</div>
			<?php else : ?>
				<label class="nudgesells-addon-check">
					<input type="checkbox" class="nudgesells-addon-toggle" <?php checked( 1 === (int) $addon['default_value'] ); ?> />
					<?php if ( $addon['image'] ) : ?>
						<img src="<?php echo esc_url( $addon['image'] ); ?>" alt="" class="nudgesells-addon-thumb" />
					<?php endif; ?>
					<?php if ( 'list' === $display_style ) : ?>
						<span class="nudgesells-addon-body">
							<span class="nudgesells-addon-info">
								<?php if ( ! empty( $addon['badge_label'] ) ) : ?>
									<span class="nudgesells-addon-badge"><?php echo esc_html( $addon['badge_label'] ); ?></span>
								<?php endif; ?>
								<span class="nudgesells-addon-name"><?php nudgesells_render_product_title( $addon['label'], $addon['permalink'], $title_link_mode, $addon['link_product_id'], $quick_view_css_class ); ?></span>
								<?php if ( ! empty( $addon['sublabel'] ) ) : ?>
									<span class="nudgesells-addon-subline"><?php echo esc_html( $addon['sublabel'] ); ?></span>
								<?php endif; ?>
								<?php nudgesells_render_addon_variations( $addon ); ?>
							</span>
							<?php nudgesells_render_addon_price( $addon ); ?>
						</span>
					<?php else : ?>
						<span class="nudgesells-addon-info">
							<?php if ( ! empty( $addon['badge_label'] ) ) : ?>
								<span class="nudgesells-addon-badge"><?php echo esc_html( $addon['badge_label'] ); ?></span>
							<?php endif; ?>
							<span class="nudgesells-addon-name"><?php nudgesells_render_product_title( $addon['label'], $addon['permalink'], $title_link_mode, $addon['link_product_id'], $quick_view_css_class ); ?></span>
							<?php if ( ! empty( $addon['sublabel'] ) ) : ?>
								<span class="nudgesells-addon-subline"><?php echo esc_html( $addon['sublabel'] ); ?></span>
							<?php endif; ?>
							<?php nudgesells_render_addon_price( $addon ); ?>
						</span>
					<?php endif; ?>
				</label>
			<?php endif; ?>

			<?php if ( 'list' !== $display_style ) : ?>
				<?php nudgesells_render_addon_variations( $addon ); ?>
				<?php nudgesells_render_addon_qty_control( $addon, $show_qty_stepper ); ?>
			<?php endif; ?>
			</div>
				<?php endforeach; ?>
			</div>

			<?php $nudgesells_progress_style = NudgeSells_Admin_Settings::get( 'progress_indicator_style', 'pills' ); ?>

			<?php if ( 'none' !== $nudgesells_progress_style && ! empty( $bundle['enabled'] ) && ! empty( $addons ) ) : ?>
				<?php if ( 'bar' === $nudgesells_progress_style ) : ?>
					<div class="nudgesells-progress-bar nudgesells-bundle-progress-bar" role="status" aria-label="<?php esc_attr_e( 'Bundle discount progress', 'nudgesells' ); ?>" data-total="<?php echo esc_attr( count( $addons ) ); ?>">
						<div class="nudgesells-progress-bar-fill"></div>
					</div>
				<?php else : ?>
					<div class="nudgesells-progress-pills nudgesells-bundle-pills" role="status" aria-label="<?php esc_attr_e( 'Bundle discount progress', 'nudgesells' ); ?>">
						<?php foreach ( $addons as $addon ) : ?>
							<span class="nudgesells-pill"></span>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			<?php endif; ?>

			<?php if ( 'none' !== $nudgesells_progress_style && ! empty( $tiers ) ) : ?>
				<?php if ( 'bar' === $nudgesells_progress_style ) : ?>
					<div class="nudgesells-progress-bar nudgesells-tier-progress-bar" role="status" aria-label="<?php esc_attr_e( 'Quantity discount progress', 'nudgesells' ); ?>" data-thresholds="<?php echo esc_attr( wp_json_encode( wp_list_pluck( $tiers, 'min_qty' ) ) ); ?>">
						<div class="nudgesells-progress-bar-fill"></div>
					</div>
				<?php else : ?>
					<div class="nudgesells-progress-pills nudgesells-tier-pills" role="status" aria-label="<?php esc_attr_e( 'Quantity discount progress', 'nudgesells' ); ?>">
						<?php foreach ( $tiers as $tier ) : ?>
							<span class="nudgesells-pill" data-min-qty="<?php echo esc_attr( $tier['min_qty'] ); ?>"></span>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			<?php endif; ?>

			<div class="nudgesells-discount-message" aria-live="polite"></div>

			<div class="nudgesells-summary" aria-live="polite" aria-atomic="true" style="display:none;">
				<span class="nudgesells-summary-savings"></span>
				<span class="nudgesells-summary-total"></span>
			</div>
		</div>
	<?php endif; ?>
</div>
