<?php

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

$content_ids   = array();
$content_count = get_theme_mod( 'construktly_projects_count', 3 );
$content_type  = get_theme_mod( 'construktly_projects_content_type', 'page' );

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

	for ( $i = 1; $i <= $content_count; $i++ ) {
		$content_ids[] = get_theme_mod( 'construktly_projects_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,
	);

}

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

construktly_render_projects_section( $args );

/**
 * Render Projects Section.
 */
function construktly_render_projects_section( $args ) {
	$section_title = get_theme_mod( 'construktly_projects_title', __( 'Latest Projects', 'construktly' ) );
	$section_text  = get_theme_mod( 'construktly_projects_text' );
	$button_label  = get_theme_mod( 'construktly_projects_button_label', __( 'View All', 'construktly' ) );
	$button_link   = get_theme_mod( 'construktly_projects_button_link' );
	$button_link   = ! empty( $button_link ) ? $button_link : '#';
	$content_type  = get_theme_mod( 'construktly_projects_content_type', 'page' );

	$query = new WP_Query( $args );
	if ( $query->have_posts() ) :
		?>
		<section id="construktly_projects_section" class="construktly-frontpage-section construktly-prom-section construktly-three-col-prom-section">
			<?php
			if ( is_customize_preview() ) :
				construktly_section_link( 'construktly_projects_section' );
			endif;
			?>
			<div class="ascendoor-wrapper">
				<?php if ( ! empty( $section_title || $section_text ) ) : ?>
					<div class="section-header-subtitle">
						<div class="section-header-subtitle-wrapper">
							<h3 class="section-title"><?php echo esc_html( $section_title ); ?></h3>
							<p class="section-subtitle"><?php echo esc_html( $section_text ); ?></p>
						</div>
					</div>
				<?php endif; ?>
				<div class="construktly-section-body">
					<div class="construktly-prom-section-wrapper">
						<?php
						$i = 1;
						while ( $query->have_posts() ) :
							$query->the_post();
							$location = get_theme_mod( 'construktly_projects_location_' . $i );
							?>
							<div class="construktly-prom-single">
								<div class="construktly-prom-img">
									<?php the_post_thumbnail( 'full' ); ?>
								</div>
								<div class="construktly-prom-detail">
									<?php if ( 'page' !== $content_type ) : ?>
										<div class="ascendoor-category-links">
											<?php echo get_the_category_list( ', ', '', get_the_ID() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
										</div>
									<?php endif; ?>
									<h3 class="construktly-prom-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
									<h4 class="construktly-prom-subtitle">
										<?php the_excerpt(); ?>
									</h4>
								</div>
							</div>
							<?php
							$i++;
						endwhile;
						wp_reset_postdata();
						?>
					</div>  
					<?php if ( ! empty( $button_label ) ) : ?>
						<div class="construktly-latest-news-view-all bottom-viewall-button construktly-button">
							<a href="<?php echo esc_url( $button_link ); ?>"><?php echo esc_html( $button_label ); ?></a>
						</div>
					<?php endif; ?>  
				</div>    
			</div>
		</section>
		<?php
	endif;
}
