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

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

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

millennium_news_render_tile_grid_section( $tile_grid_args );

/**
 * Render Tile and List Section.
 */
function millennium_news_render_tile_grid_section( $tile_grid_args ) {

	$query = new WP_Query( $tile_grid_args );
	if ( $query->have_posts() ) {
		$section_title          = get_theme_mod( 'millennium_news_tile_grid_title', __( 'Tile and List', 'millennium-news' ) );
		$view_all_button        = get_theme_mod( 'millennium_news_tile_grid_button_label', __( 'View All', 'millennium-news' ) );
		$tile_grid_content_type = get_theme_mod( 'millennium_news_tile_grid_content_type', 'post' );
		$view_all_button_url    = get_theme_mod( 'millennium_news_tile_grid_button_link' );
		if ( 'category' === $tile_grid_content_type ) {
			$tile_grid_posts_category = get_theme_mod( 'millennium_news_tile_grid_content_category' );
			$view_all_button_url      = ! empty( $view_all_button_url ) ? $view_all_button_url : get_category_link( $tile_grid_posts_category );
		} else {
			$view_all_button_url = ! empty( $view_all_button_url ) ? $view_all_button_url : '#';
		}

		?>
		<div id="millennium_news_tile_grid_section" class="tile-grid-section section-splitter style-2" data-aos="fade-up" data-aos-duration="1000">
			<?php
			if ( is_customize_preview() ) :
				millennium_news_section_link( 'millennium_news_tile_grid_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="main-container-wrap"> 
					<?php if ( ! empty( $section_title || $view_all_button ) ) { ?>
						<div class="title-heading">
							<?php if ( ! empty( $section_title ) ) : ?>
								<h3 class="section-title">
									<?php echo esc_html( $section_title ); ?>
								</h3>
							<?php endif; ?>
							<?php if ( ! empty( $view_all_button ) ) : ?>
								<a href="<?php echo esc_url( $view_all_button_url ); ?>" class="view-all"><?php echo esc_html( $view_all_button ); ?></a>
							<?php endif; ?>
						</div>
					<?php } ?>
					<div class="tile-grid-wrapper">
						<?php
						if ( $query->have_posts() ) {
							$i = 1;
							while ( $query->have_posts() ) :
								$query->the_post();
								$classes = $i === 1 ? 'tile-layout' : 'grid-layout';
								?>
								<div class="blog-post-container <?php echo esc_attr( $classes ); ?>">
									<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 millennium_news_categories_list(); ?>
											<h2 class="entry-title">
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h2>
											<div class="post-meta">
												<?php millennium_news_posted_by(); ?>
												<?php millennium_news_posted_on(); ?>
											</div>
										</div>
									</div>
								</div>
								<?php
								$i++;
							endwhile;
						}
						?>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
