<?php
/**
 * Modal Template
 *
 * @since      1.0.0
 * @author     WPBean
 * @global     $popup_data
 *
 * @package Finch Cart
 */

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

use function FinchCart\fc_data_attributes;
use function FinchCart\fc_kses_allowed_svg;

$current_product        = wc_get_product( $popup_data['product_id'] );
$query                  = $popup_data['recommended_products'];
$products_wrapper_class = apply_filters( 'finchcart_wrapper_class', array( 'finchcart-modal-products', 'products' ) );
if ( isset( $popup_data['products_layout'] ) ) {
	$products_wrapper_class[] = 'finchcart-modal-products-' . $popup_data['products_layout'];
}
if ( isset( $popup_data['products_layout'] ) && 'slider' === $popup_data['products_layout'] ) {
	$products_wrapper_class[] = 'swiper';
}
?>

<div class="finchcart-modal-head">
	<?php if ( isset( $popup_data['show_notification_bar'] ) && true === $popup_data['show_notification_bar'] ) : ?>
	<div class="finchcart-notice">
		<div class="finchcart-notice-message">
			<?php
			if ( isset( $popup_data['notice_show_image'] ) && true === $popup_data['notice_show_image'] ) {
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo $current_product->get_image( 'thumbnail' );
			}

				printf(
					'<div class="finchcart-notice-text">%s %s</div>',
					wp_kses( $popup_data['checked_icon'], fc_kses_allowed_svg() ),
					isset( $popup_data['notice_message_text'] ) ? wp_kses_post( str_replace( '%title%', '<strong>' . esc_html( $current_product->get_name() ) . '</strong>', $popup_data['notice_message_text'] ) ) : '',
				);

			if ( isset( $popup_data['notice_show_view_cart'] ) && true === $popup_data['notice_show_view_cart'] ) {
				printf(
					'<a href="%s" tabindex="1" class="finchcart-cart-link">%s</a>',
					esc_url( wc_get_cart_url() ),
					esc_html( $popup_data['cart_link_text'] )
				);
			}
			?>
		</div>
		<div class="finchcart-notice-buttons">
			<?php
				echo '<div class="finchcart-fill-buttons">';

			if ( isset( $popup_data['notice_show_con_shop'] ) && true === $popup_data['notice_show_con_shop'] ) {
				printf( '<span class="finchcart-button finchcart-close-modal finchcart-continue-shopping">%s</span>', esc_html( $popup_data['continue_shopping_text'] ) );
			}

			if ( isset( $popup_data['notice_show_checkout'] ) && true === $popup_data['notice_show_checkout'] ) {
				printf( '<a href="%s" class="finchcart-button finchcart-checkout">%s</a>', esc_url( wc_get_checkout_url() ), esc_html( $popup_data['checkout_text'] ) );
			}

				echo '</div>';

			if ( isset( $popup_data['notice_show_cart_count'] ) && true === $popup_data['notice_show_cart_count'] ) {
				printf(
					'<a href="%s" class="finchcart-cart-count"><span class="finchcart-cart-total">%s</span>%s</a>',
					esc_url( wc_get_cart_url() ),
					esc_html( WC()->cart->get_cart_contents_count() ),
					wp_kses( $popup_data['cart_icon'], fc_kses_allowed_svg() )
				);
			}
			?>
		</div>
	</div>
	<?php endif; ?>

	<?php if ( isset( $popup_data['show_heading'] ) && true === $popup_data['show_heading'] && $query->have_posts() ) : ?>
	<div class="finchcart-popup-header">
		<?php if ( isset( $popup_data['heading'] ) && '' !== $popup_data['heading'] ) : ?>
			<h3><?php echo wp_kses_post( $popup_data['heading'] ); ?></h3>
		<?php endif; ?>
		<?php
		if ( isset( $popup_data['subheading'] ) && '' !== $popup_data['subheading'] ) {
			echo wp_kses_post( wpautop( $popup_data['subheading'] ) );
		}
		?>
	</div>
	<?php endif; ?>
	<div style="display: none" class="finchcart-inside-popup-cart-notice"></div>
</div>

<?php if ( true === $popup_data['show_products'] ) : ?>
	<div class="finchcart-modal-body woocommerce">
		<?php if ( $query->have_posts() ) : ?>
			<div class="finchcart-modal-products-wrapper finchcart-modal-products-wrapper-<?php echo esc_attr( $popup_data['products_layout'] ); ?>">
				<div class="<?php echo esc_attr( implode( ' ', $products_wrapper_class ) ); ?>" <?php ( 'slider' === $popup_data['products_layout'] ? esc_html( fc_data_attributes( $popup_data['slider_atts'] ) ) : '' ); ?>>
					<?php
						printf( isset( $popup_data['products_layout'] ) && 'slider' === $popup_data['products_layout'] ? '<div class="finchcart-swiper-wrapper swiper-wrapper">' : '' );
					while ( $query->have_posts() ) :
						$query->the_post();
						include $this->get_templates_path( 'templates/content-product.php' );
						wp_reset_postdata();
						endwhile;
						printf( isset( $popup_data['products_layout'] ) && 'slider' === $popup_data['products_layout'] ? '</div>' : '' );
					?>
				</div>
				<?php printf( isset( $popup_data['products_layout'] ) && 'slider' === $popup_data['products_layout'] ? '<div class="swiper-pagination"></div>' : '' ); ?>
				<?php printf( isset( $popup_data['products_layout'] ) && 'slider' === $popup_data['products_layout'] ? '<div class="swiper-button-prev"></div><div class="swiper-button-next"></div>' : '' ); ?>
			</div>
		<?php endif; ?>
	</div>
<?php endif; ?>