<?php
/**
 * Homepage "We Would Suggest You" section.
 *
 * Pulls coupon deals from the "coupons" post type (registered through the
 * Simple CPT plugin) and reads their discount/expiry details through
 * Advanced Custom Fields. Populated automatically by the theme's demo
 * importer, or by adding "Coupons" content manually once Simple CPT and
 * ACF are set up.
 *
 * @package coupon-flash-deals
 */

if ( 'on' !== get_theme_mod( 'coupon_flash_deals_suggest_on_off', 'on' ) ) {
	return;
}

if ( ! post_type_exists( 'coupons' ) ) {
	return;
}

$coupon_flash_deals_suggest_heading = get_theme_mod( 'coupon_flash_deals_suggest_heading', __( 'We Would Suggest You', 'coupon-flash-deals' ) );
$coupon_flash_deals_suggest_count   = (int) get_theme_mod( 'coupon_flash_deals_suggest_count', 4 );

if ( $coupon_flash_deals_suggest_count < 1 ) {
	$coupon_flash_deals_suggest_count = 4;
}

$coupon_flash_deals_suggest_query = new WP_Query(
	array(
		'post_type'              => 'coupons',
		'post_status'            => 'publish',
		'posts_per_page'         => $coupon_flash_deals_suggest_count,
		'orderby'                => 'date',
		'order'                  => 'DESC',
		'no_found_rows'          => true,
		'update_post_term_cache' => false,
	)
);

// Bundled fallback photos, used per-card (cycled) whenever a coupon has no
// featured image set, so cards never all collapse onto one placeholder.
$coupon_flash_deals_suggest_fallback_images = array(
	'coupons1.png',
	'coupons2.png',
	'coupons3.png',
	'coupons4.png',
);
$coupon_flash_deals_suggest_index = 0;

if ( $coupon_flash_deals_suggest_query->have_posts() ) :
	?>
	<section id="home_suggest" class="suggest-section">
		<div class="container">
			<?php if ( ! empty( $coupon_flash_deals_suggest_heading ) ) : ?>
				<div class="course-heading text-center">
					<h5><?php echo esc_html( $coupon_flash_deals_suggest_heading ); ?></h5>
				</div>
			<?php endif; ?>

			<div class="row suggest-grid">
				<?php while ( $coupon_flash_deals_suggest_query->have_posts() ) : $coupon_flash_deals_suggest_query->the_post(); ?>
					<?php
					$coupon_flash_deals_amount      = coupon_flash_deals_get_coupon_field( 'coupon_amount', get_the_ID() );
					$coupon_flash_deals_expiry_text = coupon_flash_deals_get_coupon_expiry_text( get_the_ID() );
					?>
					<div class="col-lg-3 col-md-6 col-sm-6">
						<div class="suggest-card">
							<a class="suggest-thumb" href="<?php the_permalink(); ?>">
								<?php if ( has_post_thumbnail() ) : ?>
									<?php the_post_thumbnail( 'medium_large', array( 'alt' => the_title_attribute( array( 'echo' => false ) ) ) ); ?>
								<?php else : ?>
									<?php
									$coupon_flash_deals_suggest_fallback_image = $coupon_flash_deals_suggest_fallback_images[ $coupon_flash_deals_suggest_index % count( $coupon_flash_deals_suggest_fallback_images ) ];
									$coupon_flash_deals_suggest_index++;
									?>
									<img src="<?php echo esc_url( get_template_directory_uri() . '/images/' . $coupon_flash_deals_suggest_fallback_image ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>">
								<?php endif; ?>
								<?php if ( ! empty( $coupon_flash_deals_amount ) ) : ?>
									<span class="suggest-badge"><?php echo esc_html( $coupon_flash_deals_amount ); ?></span>
								<?php endif; ?>
							</a>
							<div class="suggest-body">
								<h3 class="suggest-title">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h3>
								<p class="suggest-desc"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 6, '' ) ); ?></p>
							</div>
							<?php if ( ! empty( $coupon_flash_deals_expiry_text ) ) : ?>
								<div class="suggest-footer"><?php echo esc_html( $coupon_flash_deals_expiry_text ); ?></div>
							<?php endif; ?>
						</div>
					</div>
				<?php endwhile; ?>
			</div>
		</div>
	</section>
	<?php
endif;
wp_reset_postdata();
