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

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

if ( $grid_content_type === 'post' ) {
	for ( $i = 1; $i <= 3; $i++ ) {
		$grid_content_ids[] = get_theme_mod( 'scope_news_grid_content_post_' . $i );
	}
	$grid_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 3 ),
		'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( 'scope_news_grid_content_category' );
	$grid_args      = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}
$grid_args = apply_filters( 'scope_news_grid_section_args', $grid_args );

scope_news_render_grid_section( $grid_args );

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

	$query = new WP_Query( $grid_args );
	if ( $query->have_posts() ) {
		$section_title       = get_theme_mod( 'scope_news_grid_title', __( 'Grid Posts', 'scope-news' ) );
		$view_all_button     = get_theme_mod( 'scope_news_grid_button_label', __( 'View All', 'scope-news' ) );
		$grid_content_type   = get_theme_mod( 'scope_news_grid_content_type', 'post' );
		$view_all_button_url = get_theme_mod( 'scope_news_grid_button_link' );
		if ( 'category' === $grid_content_type ) {
			$grid_posts_category = get_theme_mod( 'scope_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="scope_news_grid_section" class="grid-section section-splitter column-3">
			<?php
			if ( is_customize_preview() ) :
				scope_news_section_link( 'scope_news_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 } ?>
						</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">
									<?php if ( has_post_thumbnail() ) { ?>
										<div class="blog-post-image">
											<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'post-thumbnail' ); ?></a>
											<?php scope_news_categories_list(); ?>
										</div>
									<?php } ?>
									<div class="blog-post-detail">
										<h2 class="entry-title">
											<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										</h2>

										<div class="post-meta">
											<?php scope_news_posted_by(); ?>
											<?php scope_news_posted_on(); ?>
										</div>
										<?php if ( ! has_post_thumbnail() ) { ?>
											<p class="post-excerpt">
												<?php echo wp_kses_post( wp_trim_words( get_the_content(), 20 ) ); ?>
											</p>
										<?php } ?>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}
