<?php
/**
 * Grid widget template.
 *
 * @package storzen
 */

if (! defined('ABSPATH')) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template file; variables are passed in from parent widget scope, not true globals.

// Optional: thank-you upsell widget sets these before including this template.
$szp_grid_hide_pagination     = isset( $szp_grid_hide_pagination ) ? (bool) $szp_grid_hide_pagination : false;
$szp_grid_section_extra_class = isset( $szp_grid_section_extra_class ) ? (string) $szp_grid_section_extra_class : '';

if (($settings['style'] ?? 'style-1') !== 'style-1') {
	return;
}
?>
<section class="shop-section fix section-padding<?php echo $szp_grid_section_extra_class !== '' ? ' ' . esc_attr( $szp_grid_section_extra_class ) : ''; ?>">
	<div class="wco-container">
		<div class="wco-row wco-g-4">
			<?php
			if ($storzen_posts_query->have_posts()) :
				while ($storzen_posts_query->have_posts()) :
					$storzen_posts_query->the_post();
					$storzen_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
					$product              = wc_get_product(get_the_ID());
					if (! $product) {
						continue;
					}
			?>
					<div class="wco-col-xl-<?php echo esc_attr($settings['column']); ?> wco-col-lg-4 wco-col-sm-6">
						<div class="shop-card-items mt-0">
							<div class="image">
								<div class="badges">
									<?php do_action( 'storzen_shop_card_badges', $product ); ?>
								</div>
								<a href="<?php the_permalink(); ?>">
									<?php the_post_thumbnail(); ?>
								</a>
								<div class="social-style-two">
									<?php echo storzen_add_to_cart_button(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- storzen_add_to_cart_button() returns markup already escaped with esc_url/esc_attr/wc_implode_html_attributes. ?>
									<?php do_action( 'storzen_shop_card_overlay', $product ); ?>
								</div>
							</div>
							<div class="content">
								<?php
								$storzen_categories = get_the_terms(get_the_ID(), 'product_cat');
								if ($storzen_categories && ! is_wp_error($storzen_categories)) :
									$storzen_category = array_shift($storzen_categories);
								?>
									<a href="<?php echo esc_url(get_term_link($storzen_category)); ?>" class="category"><?php echo esc_html($storzen_category->name); ?></a>
								<?php endif; ?>
								<<?php echo esc_attr($settings['titltag']); ?>>
									<a <?php echo ('new-window' === ($settings['open-url'] ?? 'same-window')) ? 'target="_blank" rel="noopener noreferrer"' : ''; ?> href="<?php the_permalink(); ?>" class="woo-title">
										<?php
										if ('full' === ($settings['dtitle'] ?? 'full')) {
											the_title();
										} else {
											echo esc_html(wp_trim_words(get_the_title(), (int) ($settings['title_excerpt_length'] ?? 8), ''));
										}
										?>
									</a>
								</<?php echo esc_attr($settings['titltag']); ?>>
								<div class="prices"><?php echo wp_kses_post($product->get_price_html()); ?></div>
								<?php
								/**
								 * Fires inside the product card content area after price,
								 * for modules that render below the price (Stock Scarcity,
								 * Flash Countdown timer, Variation Swatches shop card).
								 *
								 * @param WC_Product $product
								 */
								do_action( 'storzen_shop_card_after_title', $product );
								?>
							</div>
						</div>
					</div>
				<?php
				endwhile;
			else :
				?>
				<h3 class="no-post-found"><?php esc_html_e('Sorry, No Product Found', 'storzen'); ?></h3>
			<?php endif; ?>
		</div>

		<?php
		if ( ! $szp_grid_hide_pagination && 'yes' === ($settings['enable_pagination'] ?? 'yes') && $storzen_posts_query->max_num_pages > 1) :
			$current = is_front_page() ? (int) get_query_var('page') : (int) get_query_var('paged');
			$current = max(1, $current);
			$links   = paginate_links(
				array(
					'base'      => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
					'format'    => '?paged=%#%',
					'current'   => $current,
					'total'     => (int) $storzen_posts_query->max_num_pages,
					'type'      => 'array',
					'prev_text' => esc_html__('Previous', 'storzen'),
					'next_text' => esc_html__('Next', 'storzen'),
				)
			);
			if (! empty($links)) :
			?>
				<ul class="pagination style-two">
					<?php foreach ($links as $link) : ?>
						<li><?php echo wp_kses_post($link); ?></li>
					<?php endforeach; ?>
				</ul>
			<?php
			endif;
		endif;
		wp_reset_postdata();
		?>
	</div>
</section>
