<?php

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

$construction_engineer_content_ids  = array();
$construction_engineer_content_type = get_theme_mod( 'construction_engineer_service_content_type', 'post' );

for ( $construction_engineer_i = 1; $construction_engineer_i <= 8; $construction_engineer_i++ ) {
  $construction_engineer_content_ids[] = get_theme_mod( 'construction_engineer_service_content_' . $construction_engineer_content_type . '_' . $construction_engineer_i );
}

// Get the category for the services slider from theme mods or a default category
$construction_engineer_services_category = get_theme_mod('construction_engineer_services_category', 'services');

// Modify query to fetch posts from a specific category
$construction_engineer_services_args = array(
    'post_type'           => $construction_engineer_content_type,
    'post__in'            => array_filter( $construction_engineer_content_ids ),
    'orderby'             => 'post__in',
    'posts_per_page'      => absint(1),
    'ignore_sticky_posts' => true,
);

// Apply category filter only if content type is 'post'
if ( 'post' === $construction_engineer_content_type && ! empty( $construction_engineer_services_category ) ) {
    $construction_engineer_services_args['category_name'] = $construction_engineer_services_category;
}

$construction_engineer_services_args = apply_filters( 'construction_engineer_service_section_args', $construction_engineer_services_args );

construction_engineer_render_service_section( $construction_engineer_services_args );

/**
 * Render About Us Section.
 */
function construction_engineer_render_service_section( $construction_engineer_services_args ) { ?>

  <section id="construction_engineer_service_section" class="asterthemes-frontpage-section service-section service-style-1">
    <?php
    if ( is_customize_preview() ) :
      construction_engineer_section_link( 'construction_engineer_service_section' );
    endif;

    $construction_engineer_trending_product_heading = get_theme_mod( 'construction_engineer_trending_product_heading');
    $construction_engineer_default_image_url = get_template_directory_uri() . '/resource/img/default.png'; // Update this path to your default image
    ?>
    <div class="asterthemes-wrapper">
      <div class="video-main-box">
        <?php 
        $construction_engineer_query = new WP_Query( $construction_engineer_services_args );
        if ( $construction_engineer_query->have_posts() ) :
          ?>
          <div class="section-body">
            <div class="service-section-wrapper">
              <?php
              $construction_engineer_i = 1;
              while ( $construction_engineer_query->have_posts() ) :
                $construction_engineer_query->the_post();
                ?>
                <div class="service-single">
                  <div class="service-image-box">
                    <?php if ( has_post_thumbnail() ) { ?>
                      <div class="service-image">
                        <?php the_post_thumbnail( 'full' ); ?>
                      </div>
                    <?php } else { ?>
                      <div class="service-image">
                        <img src="<?php echo esc_url( $construction_engineer_default_image_url ); ?>" alt="<?php the_title(); ?>">
                      </div>
                    <?php } ?>
                  </div>
                  <div class="service-title">
                    <?php if ( ! empty( $construction_engineer_trending_product_heading ) ) { ?>
                      <h4><?php echo esc_html( $construction_engineer_trending_product_heading ); ?></h4>
                    <?php } ?>
                    <h3>
                      <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                      </a>
                    </h3>
                    <div class="mag-post-excerpt">
                      <?php the_excerpt(); ?>
                    </div>
                    <div class="services-box">
                      <?php for ($construction_engineer_i=1; $construction_engineer_i <= 4 ; $construction_engineer_i++) { ?>
                        <?php  $construction_engineer_trending_product_content = get_theme_mod( 'construction_engineer_trending_product_content'.$construction_engineer_i);
                        if ( ! empty( $construction_engineer_trending_product_content ) ) { ?>
                          <p><i class="fas fa-forward"></i><?php echo esc_html( $construction_engineer_trending_product_content ); ?></p>
                        <?php } 
                      } ?>
                    </div>
                  </div>
                </div>
                <?php
                $construction_engineer_i++;
              endwhile;
              wp_reset_postdata();
              ?>
            </div>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </section>
  <?php
}