<?php
/**
 * FoodOrd Shared Upsells Rail Template
 *
 * @package FoodOrd
 * @since 1.3.36
 *
 * @var WC_Product[]                  $foodord_upsell_products Eligible upsell products.
 * @var string                        $foodord_upsell_context  Render context.
 * @var Foodord_Product_Modal_Upsells $foodord_upsell_helper   Upsell helper.
 */

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

if ( empty( $foodord_upsell_products ) || ! is_array( $foodord_upsell_products ) ) {
	return;
}

$foodord_upsell_context     = isset( $foodord_upsell_context ) ? (string) $foodord_upsell_context : 'modal';
$foodord_upsell_title_id    = function_exists( 'wp_unique_id' ) ? wp_unique_id( 'foodord-' . $foodord_upsell_context . '-upsells-title-' ) : 'foodord-' . $foodord_upsell_context . '-upsells-title';
$foodord_upsell_button_attr = 'card' === $foodord_upsell_context ? 'data-foodord-card-upsell-trigger' : 'data-foodord-modal-upsell-trigger';
$foodord_upsell_section_css = 'foodord-modal-upsells';

if ( 'card' === $foodord_upsell_context ) {
	$foodord_upsell_section_css .= ' foodord-modal-upsells--card';
}

$foodord_show_upsell_images = 'yes' === get_option( 'foodord_show_product_card_images', 'yes' );

$foodord_allowed_upsell_price_html = array(
	'span'   => array(
		'class'       => array(),
		'aria-hidden' => array(),
	),
	'bdi'    => array(),
	'del'    => array(),
	'ins'    => array(),
	'button' => array(
		'type'                        => array(),
		'class'                       => array(),
		'aria-label'                  => array(),
		'data-foodord-open-campaigns' => array(),
	),
);

$foodord_allowed_upsell_image_html = array(
	'img' => array(
		'class'                    => true,
		'src'                      => true,
		'alt'                      => true,
		'width'                    => true,
		'height'                   => true,
		'loading'                  => true,
		'decoding'                 => true,
		'data-foodord-lazy-src'    => true,
		'data-foodord-lazy-srcset' => true,
		'data-foodord-lazy-sizes'  => true,
	),
);
?>

<section class="<?php echo esc_attr( $foodord_upsell_section_css ); ?>" aria-labelledby="<?php echo esc_attr( $foodord_upsell_title_id ); ?>">
	<div class="foodord-modal-upsells__header">
		<h3 class="foodord-modal-upsells__title" id="<?php echo esc_attr( $foodord_upsell_title_id ); ?>">
			<?php esc_html_e( 'You may also like this.', 'foodord' ); ?>
		</h3>

		<div class="foodord-modal-upsells__nav" hidden aria-label="<?php echo esc_attr__( 'Upsell navigation', 'foodord' ); ?>">
			<button
				type="button"
				class="foodord-modal-upsells__nav-button"
				data-foodord-upsells-scroll="prev"
				aria-label="<?php echo esc_attr__( 'Scroll upsells left', 'foodord' ); ?>">
				<svg class="foodord-modal-upsells__nav-icon" width="28" height="28" viewBox="0 0 28 28" fill="none" aria-hidden="true" focusable="false">
					<path d="M16.5 7L9.5 14L16.5 21" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"/>
				</svg>
			</button>

			<button
				type="button"
				class="foodord-modal-upsells__nav-button"
				data-foodord-upsells-scroll="next"
				aria-label="<?php echo esc_attr__( 'Scroll upsells right', 'foodord' ); ?>">
				<svg class="foodord-modal-upsells__nav-icon" width="28" height="28" viewBox="0 0 28 28" fill="none" aria-hidden="true" focusable="false">
					<path d="M11.5 7L18.5 14L11.5 21" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"/>
				</svg>
			</button>
		</div>
	</div>

	<div class="foodord-modal-upsells__grid">
		<?php foreach ( $foodord_upsell_products as $foodord_modal_upsell ) : ?>
			<?php
			if ( ! $foodord_modal_upsell instanceof WC_Product ) {
				continue;
			}

			$foodord_modal_upsell_id         = $foodord_modal_upsell->get_id();
			$foodord_modal_upsell_name       = $foodord_modal_upsell->get_name();
			$foodord_modal_upsell_price      = $foodord_modal_upsell->get_price_html();
			$foodord_modal_upsell_image_id   = $foodord_modal_upsell->get_image_id();
			$foodord_modal_upsell_alt        = '';
			$foodord_modal_upsell_image_src  = '';
			$foodord_modal_upsell_direct_add = $foodord_upsell_helper instanceof Foodord_Product_Modal_Upsells && $foodord_upsell_helper->supports_direct_add( $foodord_modal_upsell );
			$foodord_modal_upsell_cta_label  = $foodord_modal_upsell_direct_add ? __( 'Add to cart', 'foodord' ) : __( 'View item', 'foodord' );
				/* translators: %s: upsell product name. */
				$foodord_modal_upsell_cta_aria   = $foodord_modal_upsell_direct_add ? __( 'Add to cart', 'foodord' ) : sprintf( __( 'View %s', 'foodord' ), $foodord_modal_upsell_name );

			if ( $foodord_modal_upsell->is_type( 'variable' ) ) {
				$foodord_modal_upsell_min_price = $foodord_modal_upsell->get_variation_price( 'min', false );

				if ( $foodord_modal_upsell_min_price ) {
					$foodord_modal_upsell_display_price   = wc_get_price_to_display(
						$foodord_modal_upsell,
						array(
							'price' => $foodord_modal_upsell_min_price,
						)
					);
					$foodord_modal_upsell_formatted_price = wc_price( $foodord_modal_upsell_display_price );
					$foodord_modal_upsell_price           = '<span class="foodord-price-prefix">' . esc_html__( 'from: ', 'foodord' ) . '</span>' . $foodord_modal_upsell_formatted_price;
				}
			}

			if ( $foodord_modal_upsell_image_id > 0 ) {
				$foodord_modal_upsell_alt = (string) get_post_meta( $foodord_modal_upsell_image_id, '_wp_attachment_image_alt', true );
			}

			if ( '' === $foodord_modal_upsell_alt ) {
				$foodord_modal_upsell_alt = $foodord_modal_upsell_name;
			}

			if ( $foodord_show_upsell_images && $foodord_modal_upsell_image_id > 0 ) {
				$foodord_modal_upsell_image_src = wp_get_attachment_image_url( $foodord_modal_upsell_image_id, 'medium' );
			}

			if ( '' === $foodord_modal_upsell_image_src ) {
				$foodord_modal_upsell_image_id  = 0;
				$foodord_modal_upsell_image_src = wc_placeholder_img_src( 'medium' );
			}
			?>
			<article class="foodord-modal-upsells__item">
				<div class="foodord-modal-upsells__image-wrap">
					<?php
					echo wp_kses(
						foodord_get_lazy_image_html(
							array(
								'id'         => 0,
								'src'        => $foodord_modal_upsell_image_src,
								'size'       => 'medium',
								'alt'        => $foodord_modal_upsell_alt,
								'class'      => 'foodord-modal-upsells__image',
								'attributes' => array(
								),
								'loading'    => 'eager',
							)
						),
						$foodord_allowed_upsell_image_html
					);
					?>
				</div>

				<div class="foodord-modal-upsells__content">
					<h4 class="foodord-modal-upsells__item-title"><?php echo esc_html( $foodord_modal_upsell_name ); ?></h4>
					<div class="foodord-modal-upsells__price">
						<?php echo wp_kses( $foodord_modal_upsell_price, $foodord_allowed_upsell_price_html ); ?>
					</div>
					<button
						type="button"
						class="foodord-modal-upsells__button"
						<?php if ( $foodord_modal_upsell_direct_add ) : ?>
							data-foodord-upsell-direct-add="1"
						<?php else : ?>
							<?php echo esc_attr( $foodord_upsell_button_attr ); ?>="true"
						<?php endif; ?>
						data-product-id="<?php echo esc_attr( (string) $foodord_modal_upsell_id ); ?>"
						aria-label="<?php echo esc_attr( $foodord_modal_upsell_cta_aria ); ?>">
						<?php echo esc_html( $foodord_modal_upsell_cta_label ); ?>
					</button>
				</div>
			</article>
		<?php endforeach; ?>
	</div>
</section>
