<?php
/**
 * Template Name: Custom Home Page
 */
get_header(); ?>

<main id="content">
  <?php if( get_option('construction_firm_slider_arrows', false) !== 'off'){ ?>
    <section id="slider">
      <div id="carouselExampleAutoplaying" class="carousel slide" data-bs-ride="carousel">
        <?php $construction_firm_slider_count = get_theme_mod('construction_firm_slider_count'); ?>
        <?php
          for ( $construction_firm_i = 1; $construction_firm_i <= $construction_firm_slider_count; $construction_firm_i++ ) {
            $construction_firm_mod =  get_theme_mod( 'construction_firm_post_setting' . $construction_firm_i );
            if ( 'page-none-selected' != $construction_firm_mod ) {
              $construction_firm_slide_post[] = $construction_firm_mod;
            }
          }
           if( !empty($construction_firm_slide_post) ) :
          $construction_firm_args = array(
            'post_type' =>array('post','page'),
            'post__in' => $construction_firm_slide_post,
            'ignore_sticky_posts'  => true, // Exclude sticky posts by default
          );

          // Check if specific posts are selected
          if (empty($construction_firm_slide_post) && is_sticky()) {
              $construction_firm_args['post__in'] = get_option('sticky_posts');
          }

          $construction_firm_query = new WP_Query( $construction_firm_args );
          if ( $construction_firm_query->have_posts() ) :
            $construction_firm_i = 1;
        ?>
        <div class="carousel-inner" role="listbox">
          <?php  while ( $construction_firm_query->have_posts() ) : $construction_firm_query->the_post(); ?>
          <div <?php if($construction_firm_i == 1){echo 'class="carousel-item active"';} else{ echo 'class="carousel-item"';}?>>
              <?php if(has_post_thumbnail()){ ?>
                <img src="<?php the_post_thumbnail_url('full'); ?>"/>
              <?php }else{?>
                <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/images/header-img.jpg" alt="" />
              <?php } ?>
            <div class="carousel-caption slider-inner">
              <?php if (get_theme_mod('construction_firm_slide_heading') != '') { ?>
                <h3 class="slider-short py-3 px-2"><?php echo esc_html(get_theme_mod('construction_firm_slide_heading', '')); ?></h3>
              <?php } ?>
              <h2 class="slid-title"><?php the_title();?></h2>
              <?php if( get_option('construction_firm_slider_excerpt_show_hide',false) != 'off'){ ?> 
                <p class="slider-excerpt mb-0"><?php echo wp_trim_words(get_the_content(), get_theme_mod('construction_firm_slider_excerpt_count',20) );?></p>
              <?php } ?>
              <div class="home-btn my-4">
                <a class="py-sm-3 px-sm-4 py-2 px-3" href="<?php the_permalink(); ?>"><?php echo esc_html(get_theme_mod('construction_firm_slider_read_more',__('READ MORE','construction-firm'))); ?></a>
              </div>
            </div>
          </div>
          <?php $construction_firm_i++; endwhile;
          wp_reset_postdata();?>
        </div>
        <?php else : ?>
        <div class="no-postfound"></div>
          <?php endif;
        endif;?>
          <a class="carousel-control-prev" type="button" data-bs-target="#carouselExampleAutoplaying" data-bs-slide="prev">
            <span class="carousel-control-prev-icon px-3 py-2" aria-hidden="true"><?php esc_html_e('PREV','construction-firm'); ?></span>
          </a>
          <a class="carousel-control-next" type="button" data-bs-target="#carouselExampleAutoplaying" data-bs-slide="next">
            <span class="carousel-control-next-icon px-3 py-2" aria-hidden="true"><?php esc_html_e('NEXT','construction-firm'); ?></span>
          </a>
      </div>
      <div class="clearfix"></div>
    </section>
  <?php }?>
  <?php if( get_option('construction_firm_services_enable', false) !== 'off'){ ?>
    <section id="middle-sec">
      <div class="container">
        <div class="row">
          <?php
            for ( $construction_firm_s = 1; $construction_firm_s <= 4; $construction_firm_s++ ) {
              $construction_firm_mod =  get_theme_mod( 'construction_firm_middle_sec_settigs' . $construction_firm_s );
              if ( 'page-none-selected' != $construction_firm_mod ) {
                $construction_firm_post[] = $construction_firm_mod;
              }
            }
            if( !empty($construction_firm_post) ) :
            $construction_firm_args = array(
              'post_type' =>array('post','page'),
              'post__in' => $construction_firm_post,
              'ignore_sticky_posts'  => true, // Exclude sticky posts by default
            );

            // Check if specific posts are selected
            if (empty($construction_firm_post) && is_sticky()) {
                $construction_firm_args['post__in'] = get_option('sticky_posts');
            }

            $construction_firm_query = new WP_Query( $construction_firm_args );
            if ( $construction_firm_query->have_posts() ) :
              $construction_firm_s = 1;
          ?>
          <?php  while ( $construction_firm_query->have_posts() ) : $construction_firm_query->the_post(); ?>
            <div class="col-lg-3 col-sm-6 wow swing" data-wow-duration="2s">
              <div class="inner-box mb-3 p-3 text-center">
                <h4><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h4>
                <hr>
                <p class="mb-0"><?php echo wp_trim_words( get_the_content(),8 );?></p>
              </div>
            </div>
          <?php $construction_firm_s++; endwhile;
          wp_reset_postdata();?>
          <?php else : ?>
          <div class="no-postfound"></div>
            <?php endif;
          endif;?>
        </div>
      </div>
    </section>
  <?php }?>
  <?php if( get_option('construction_firm_project_enable', false) !== 'off'){ ?>
    <?php if( get_theme_mod('construction_firm_project_section_title') != '' || get_theme_mod('construction_firm_category_setting') != ''){ ?>
      <section id="projects-box" class="py-5">
        <div class="container">
          <h3 class="text-center mb-5"><?php echo esc_html( get_theme_mod( 'construction_firm_project_section_title','') ); ?></h3>
          <div class="row">
            <?php
              $construction_firm_project_category=  get_theme_mod('construction_firm_category_setting');
              $construction_firm_project_order = get_theme_mod('construction_firm_project_order_type','descending');
              if($construction_firm_project_category){
              $construction_firm_args = array( 
                'category_name' => esc_html($construction_firm_project_category ,'construction-firm'),
                'posts_per_page' => get_theme_mod('construction_firm_project_count'),
                'order' => 'DESC'
              );
              // Adjust ordering based on user selection
              if ($construction_firm_project_order == 'ascending') {
                $construction_firm_args['order'] = 'ASC';
              } else if ($construction_firm_project_order == 'a-to-z') {
                $construction_firm_args['orderby'] = 'title';
                $construction_firm_args['order'] = 'ASC';
              } else if ($construction_firm_project_order == 'z-to-a') {
                $construction_firm_args['orderby'] = 'title';
                $construction_firm_args['order'] = 'DESC';
              }
              $construction_firm_page_query = new WP_Query($construction_firm_args);?>
            <?php while( $construction_firm_page_query->have_posts() ) : $construction_firm_page_query->the_post(); ?>
              <div class="col-lg-6 col-md-12">
                <div class="box mb-5 text-center text-lg-start text-md-start">
                  <div class="row">
                    <div class="col-lg-6 col-md-6 wow zoomIn">
                      <?php if(has_post_thumbnail()){ ?>
                        <div class="img-box">
                          <?php the_post_thumbnail(); ?>
                        </div>
                      <?php }?>
                    </div>
                    <div class="col-lg-6 col-md-6">
                      <div class="my-md-5 my-lg-0">
                        <h6><?php the_category(); ?></h6>
                        <a href="<?php the_permalink(); ?>"><h4 class="project-head"><?php the_title();?></h4></a>
                        <p class="mb-0"><?php echo wp_trim_words( get_the_content(),15 );?></p>
                        <div class="box-button mt-2">
                          <a class="py-2 px-3" href="<?php the_permalink(); ?>"><?php esc_html_e('READ MORE','construction-firm');?></a>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            <?php endwhile;
              wp_reset_postdata();
            }?>
          </div>
        </div>
      </section>
    <?php }?>
  <?php }?>
  <section id="custom-page-content" <?php if ( have_posts() && trim( get_the_content() ) !== '' ) echo 'class="pt-3"'; ?>>
    <div class="container">
      <?php while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
      <?php endwhile; ?>
    </div>
  </section>
</main>

<?php get_footer(); ?>
