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

$grid_content_ids  = array();
$grid_content_type = get_theme_mod( 'routine_news_grid_content_type', 'post' );

if ( $grid_content_type === 'post' ) {
	for ( $i = 1; $i <= 5; $i++ ) {
		$grid_content_ids[] = get_theme_mod( 'routine_news_grid_content_post_' . $i );
	}
	$grid_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 5 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $grid_content_ids ) ) ) {
		$grid_args['post__in'] = array_filter( $grid_content_ids );
		$grid_args['orderby']  = 'post__in';
	} else {
		$grid_args['orderby'] = 'date';
	}
} else {
	$cat_content_id = get_theme_mod( 'routine_news_grid_content_category' );
	$grid_args      = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 5 ),
	);
}
$grid_args = apply_filters( 'routine_news_grid_section_args', $grid_args );

routine_news_render_grid_section( $grid_args );

/**
 * Render Grid Section.
 */
function routine_news_render_grid_section( $grid_args ) {

	$query = new WP_Query( $grid_args );
	if ( $query->have_posts() ) {
		$section_title       = get_theme_mod( 'routine_news_grid_title', __( 'Grid Posts', 'routine-news' ) );
		$view_all_button     = get_theme_mod( 'routine_news_grid_button_label', __( 'View All', 'routine-news' ) );
		$grid_content_type   = get_theme_mod( 'routine_news_grid_content_type', 'post' );
		$view_all_button_url = get_theme_mod( 'routine_news_grid_button_link' );
		if ( 'category' === $grid_content_type ) {
			$grid_posts_category = get_theme_mod( 'routine_news_grid_content_category' );
			$view_all_button_url = ! empty( $view_all_button_url ) ? $view_all_button_url : get_category_link( $grid_posts_category );
		} else {
			$view_all_button_url = ! empty( $view_all_button_url ) ? $view_all_button_url : '#';
		}

		?>
		<section id="routine_news_grid_section" class="grid-section section-splitter column-5" data-aos="fade-up" data-aos-duration="1000">
			<?php
			if ( is_customize_preview() ) :
				routine_news_section_link( 'routine_news_grid_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="main-wrap">
					<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 } ?>
							</div>
						<?php endif; ?>
						<div class="grid-wrap">
							<?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">
											<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'post-thumbnail' ); ?></a>
										</div>
										<div class="blog-post-detail">
											<?php routine_news_categories_list(); ?>
											<h2 class="entry-title">
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h2>
											<?php if ( ! has_post_thumbnail() ) { ?>
												<p class="post-excerpt">
													<?php echo wp_kses_post( wp_trim_words( get_the_content(), 20 ) ); ?>
												</p>
											<?php } ?>
											<div class="post-meta">
												<?php routine_news_posted_by(); ?>
												<?php routine_news_posted_on(); ?>
											</div>
										</div>
									</div>
								</div>
								<?php
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}
