<?php

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

$content_ids   = array();
$content_count = get_theme_mod( 'construktly_blog_count', 5 );
$content_type  = get_theme_mod( 'construktly_blog_content_type', 'page' );

if ( in_array( $content_type, array( 'post', 'page' ) ) ) {

	for ( $i = 1; $i <= $content_count; $i++ ) {
		$content_ids[] = get_theme_mod( 'construktly_blog_content_' . $content_type . '_' . $i );
	}

	$args = array(
		'post_type'           => $content_type,
		'post__in'            => $content_ids,
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( $content_count ),
		'ignore_sticky_posts' => true,
	);

} else {
	$cat_content_id = get_theme_mod( 'construktly_blog_content_category' );
	$args           = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( $content_count ),
	);
}

$args = apply_filters( 'construktly_blog_section_args', $args );

construktly_render_blog_section( $args );

/**
 * Render Blog Section.
 */
function construktly_render_blog_section( $args ) {
	$section_title = get_theme_mod( 'construktly_blog_title', __( 'Recent News', 'construktly' ) );
	$section_text  = get_theme_mod( 'construktly_blog_text' );
	$button_label  = get_theme_mod( 'construktly_blog_button_label', __( 'View All', 'construktly' ) );
	$button_link   = get_theme_mod( 'construktly_blog_button_link' );
	$button_link   = ! empty( $button_link ) ? $button_link : '#';

	$query = new WP_Query( $args );
	if ( $query->have_posts() ) :
		?>
		<section id="construktly_blog_section" class="construktly-frontpage-section construktly-blog-section blog-style-1">
			<?php
			if ( is_customize_preview() ) :
				construktly_section_link( 'construktly_blog_section' );
			endif;
			?>
			<div class="ascendoor-wrapper">
				<?php if ( ! empty( $section_title || $section_text ) ) { ?>
				<div class="section-header-subtitle">
					<h3 class="section-title"><?php echo esc_html( $section_title ); ?></h3>
					<p class="section-subtitle"><?php echo esc_html( $section_text ); ?></p>
				</div>
				<?php } ?>
				<div class="construktly-section-body">
					<div class="construktly-blog-section-wrapper">
						<?php
						while ( $query->have_posts() ) :
							$query->the_post();
							?>
						<div class="construktly-blog-single">
							<div class="construktly-blog-img">
								<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
							</div>
							<div class="construktly-detail">
								<div class="construktly-meta">
									<?php echo esc_html( get_the_date() ); ?>
								</div>
								<h3 class="construktly-blog-title">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h3>
								<div class="construktly-description">
									<?php the_excerpt(); ?>
								</div>
								<div class="construktly-button construktly-readmore construktly-button-noborder-noalternate">
									<a href="<?php the_permalink(); ?>"><?php echo esc_html( get_theme_mod( 'construktly_excerpt_more_text', __( 'Read More', 'construktly' ) ) ); ?></a>
								</div>
							</div>
						</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
					<?php if ( ! empty( $button_label ) ) { ?>
						<div class="construktly-blog-view-all construktly-button">
							<a href="<?php echo esc_url( $button_link ); ?>"><?php echo esc_html( $button_label ); ?></a>
						</div>
					<?php } ?>
				</div>
			</div>
		</section>
		<?php
	endif;
}
