<?php
if ( ! get_theme_mod( 'fuzion_blog_enable_grid_carousel_section', false ) ) {
	return;
}

$carousel_content_ids  = array();
$carousel_content_type = get_theme_mod( 'fuzion_blog_grid_carousel_content_type', 'post' );

if ( $carousel_content_type === 'post' ) {
	for ( $i = 1; $i <= 4; $i++ ) {
		$carousel_content_ids[] = get_theme_mod( 'fuzion_blog_grid_carousel_content_post_' . $i );
	}
	$grid_carousel_args = array(
		'post_type'           => $carousel_content_type,
		'posts_per_page'      => absint( 4 ),
		'ignore_sticky_posts' => true,
	);

	if ( ! empty( array_filter( $carousel_content_ids ) ) ) {
		$grid_carousel_args['post__in'] = array_filter( $carousel_content_ids );
		$grid_carousel_args['orderby']  = 'post__in';
	} else {
		$grid_carousel_args['orderby'] = 'date';
	}

} elseif ( $carousel_content_type === 'category' ) {
	$cat_content_id     = get_theme_mod( 'fuzion_blog_grid_carousel_content_category' );
	$grid_carousel_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 4 ),
	);
}
$grid_carousel_args = apply_filters( 'fuzion_blog_grid_carousel_section_args', $grid_carousel_args );

fuzion_blog_render_grid_carousel_section( $grid_carousel_args );

/**
 * Render Grid Carousel Section.
 */
function fuzion_blog_render_grid_carousel_section( $grid_carousel_args ) {

	$query = new WP_Query( $grid_carousel_args );
	if ( $query->have_posts() ) {
		$carousel_title = get_theme_mod( 'fuzion_blog_grid_carousel_section_title', __( 'Explore Asia Wonder', 'fuzion-blog' ) );
		?>

		<section id="fuzion_blog_grid_carousel_section" class="grid-carousel-section section-splitter">
			<?php
			if ( is_customize_preview() ) :
				fuzion_blog_section_link( 'fuzion_blog_grid_carousel_section' );
			endif;
			?>
			<div class="section-wrapper">
				<?php if ( ! empty( $carousel_title ) ) : ?>
					<div class="title-heading">
						<h3 class="main-title"><?php echo esc_html( $carousel_title ); ?></h3>
					</div>
				<?php endif; ?>
				<div class="grid-carousel slick-button">
					<?php
					while ( $query->have_posts() ) :
						$query->the_post();
						?>
						<div class="blog-post-container grid-layout">
							<div class="blog-post-inner">
								<div class="blog-post-image">
									<?php if ( has_post_thumbnail() ) { ?>
										<a href="<?php the_permalink(); ?>">
											<?php the_post_thumbnail( 'post-thumbnail' ); ?>
										</a>
									<?php } ?>
									<div class="post-meta-comment">
										<span class="post-comments">
											<a href="<?php the_permalink(); ?>"><i class="fa fa-comment" aria-hidden="true"></i><?php echo absint( get_comments_number( get_the_ID() ) ); ?></a>
										</span>
									</div>
								</div>
								<div class="blog-post-detail">
									<div class="post-category">
										<?php the_category( '', '', get_the_ID() ); ?>
									</div>
									<h3 class="post-main-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<div class="post-meta">
										<span class="post-author">
											<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><i class="fa-regular fa-circle-user"></i><?php echo esc_html( get_the_author() ); ?></a>
										</span>
										<span class="post-date">
											<a href="<?php the_permalink(); ?>"><i class="fa-regular fa-calendar"></i><?php echo esc_html( get_the_date() ); ?></a>
										</span>
									</div>
								</div>
							</div>
						</div>
						<?php
					endwhile;
					wp_reset_postdata();
					?>
				</div>
			</div>
		</section>
		<?php
	}
}
