<?php

if ( ! get_theme_mod( 'cosmic_blog_enable_tile_grid_section', false ) ) {
	return;
}

$tile_grid_content_ids  = array();
$tile_grid_content_type = get_theme_mod( 'cosmic_blog_tile_grid_content_type', 'post' );

if ( $tile_grid_content_type === 'post' ) {
	for ( $i = 1; $i <= 3; $i++ ) {
		$tile_grid_content_ids[] = get_theme_mod( 'cosmic_blog_tile_grid_content_post_' . $i );
	}
	$tile_grid_args = array(
		'post_type'           => 'post',
		'post__in'            => array_filter( $tile_grid_content_ids ),
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);
} else {
	$cat_content_id = get_theme_mod( 'cosmic_blog_tile_grid_content_category' );
	$tile_grid_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}
$tile_grid_args = apply_filters( 'cosmic_blog_tile_grid_section_args', $tile_grid_args );

cosmic_blog_render_tile_grid_section( $tile_grid_args );

/**
 * Render Grid Posts Section.
 */
function cosmic_blog_render_tile_grid_section( $tile_grid_args ) {
	$query = new WP_Query( $tile_grid_args );
	if ( $query->have_posts() ) {
		$tile_grid_title    = get_theme_mod( 'cosmic_blog_tile_grid_section_title', __( 'Explore Asia Architecture', 'cosmic-blog' ) );
		$tile_grid_subtitle = get_theme_mod( 'cosmic_blog_tile_grid_section_subtitle', '' );
		?>
		<section id="cosmic_blog_tile_grid_section" class="tile-grid-section section-splitter tile-grid-style-1">
			<?php
			if ( is_customize_preview() ) :
				cosmic_blog_section_link( 'cosmic_blog_tile_grid_section' );
			endif;
			?>
			<div class="section-wrapper">
				<?php if ( ! empty( $tile_grid_title || $tile_grid_subtitle ) ) : ?>
					<div class="title-heading">
						<h3 class="main-title"><?php echo esc_html( $tile_grid_title ); ?></h3>
						<span class="sub-title"><?php echo esc_html( $tile_grid_subtitle ); ?></span>
					</div>
				<?php endif; ?>
				<div class="blog-post-container-wrapper">
					<?php
					$i = 1;
					while ( $query->have_posts() ) :
						$query->the_post();
						?>
						<div class="blog-post-container <?php echo esc_attr( $i === 1 ? 'tile-layout' : 'grid-layout' ); ?>">
							<div class="blog-post-inner">
								<div class="blog-post-image">
									<a href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail( 'post-thumbnail' ); ?>
									</a>
								</div>
								<div class="blog-post-detail">
									<?php the_category( '', '', get_the_ID() ); ?>
									<h3 class="post-main-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<div class="post-meta-button">
										<div class="post-meta">
											<span class="post-author">
												<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a>
											</span>
											<span class="post-date">
												<a href="<?php the_permalink(); ?>"><?php echo esc_html( get_the_date() ); ?></a>
											</span>
										</div>
									</div>
									<?php if ( $i === 1 ) { ?>
										<p class="post-excerpt">
											<?php echo esc_html( wp_trim_words( get_the_excerpt(), 15 ) ); ?>
										</p>
									<?php } ?>	
								</div>
							</div>
						</div>
						<?php
						$i++;
					endwhile;
					wp_reset_postdata();
					?>
				</div>
			</div>
		</section>
		<?php
	}
}
