<?php

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

$content_ids   = array();
$content_count = get_theme_mod( 'corporaze_blog_count', 3 );
$content_type  = get_theme_mod( 'corporaze_blog_content_type', 'post' );

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

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

if ( ! empty( array_filter( $content_ids ) ) ) {
	$args['post__in'] = array_filter( $content_ids );
	$args['orderby']  = 'post__in';
} else {
	$args['orderby'] = 'date';
}

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

corporaze_render_blog_section( $args );

/**
 * Render Blog Section.
 */
function corporaze_render_blog_section( $args ) {
	$section_title     = get_theme_mod( 'corporaze_blog_title', __( 'Recent News', 'corporaze' ) );
	$section_text      = get_theme_mod( 'corporaze_blog_text' );
	$excerpt_count     = get_theme_mod( 'corporaze_blog_excerpt_count', 30 );
	$post_button_label = get_theme_mod( 'corporaze_blog_posts_button', __( 'Read More', 'corporaze' ) );
	$button_label      = get_theme_mod( 'corporaze_blog_button_label', __( 'View All', 'corporaze' ) );
	$button_link       = get_theme_mod( 'corporaze_blog_button_link' );
	$button_link       = ! empty( $button_link ) ? $button_link : '#';

	$query = new WP_Query( $args );
	if ( $query->have_posts() ) :
		?>
		<section id="corporaze_blog_section" class="corporaze-frontpage-section corporaze-blog-section blog-style-1">
			<?php
			if ( is_customize_preview() ) :
				corporaze_section_link( 'corporaze_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="corporaze-section-body">
					<div class="corporaze-blog-section-wrapper">
						<?php
						$i = 1;
						while ( $query->have_posts() ) :
							$query->the_post();
							?>
							<div class="corporaze-blog-single wow fadeInUp" data-wow-delay="<?php echo esc_attr( $i * 200 ); ?>ms">
								<div class="corporaze-blog-img">
									<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
								</div>
								<div class="corporaze-detail">
									<div class="corporaze-meta">
										<?php echo esc_html( get_the_date() ); ?>
									</div>
									<h3 class="corporaze-blog-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<div class="corporaze-description">
										<?php echo wp_kses_post( wp_trim_words( get_the_content(), $excerpt_count ) ); ?>
									</div>
									<?php if ( ! empty( $post_button_label ) ) : ?>
									<div class="corporaze-button corporaze-readmore corporaze-button-noborder-noalternate">
										<a href="<?php the_permalink(); ?>"><?php echo esc_html( $post_button_label ); ?></a>
									</div>
									<?php endif; ?>
								</div>
							</div>
							<?php
							$i++;
						endwhile;
						wp_reset_postdata();
						?>
					</div>
					<?php if ( ! empty( $button_label ) ) { ?>
					<div class="corporaze-blog-view-all corporaze-button">
						<a href="<?php echo esc_url( $button_link ); ?>"><?php echo esc_html( $button_label ); ?></a>
					</div>
					<?php } ?>
				</div>
			</div>
		</section>
		<?php
	endif;
}
