<?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 ( $i = 1; $i <= $construction_firm_slider_count; $i++ ) {
            $building_construction_lite_mod =  get_theme_mod( 'construction_firm_post_setting' . $i );
            if ( 'page-none-selected' != $building_construction_lite_mod ) {
              $construction_firm_slide_post[] = $building_construction_lite_mod;
            }
          }
           if( !empty($construction_firm_slide_post) ) :
          $building_construction_lite_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()) {
                $building_construction_lite_args['post__in'] = get_option('sticky_posts');
            }

          $building_construction_lite_query = new WP_Query( $building_construction_lite_args );
          if ( $building_construction_lite_query->have_posts() ) :
            $i = 1;
        ?>
        <div class="carousel-inner" role="listbox">
          <?php  while ( $building_construction_lite_query->have_posts() ) : $building_construction_lite_query->the_post(); ?>
          <div <?php if($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_stylesheet_directory_uri()); ?>/assets/images/slider.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','building-construction-lite'))); ?></a>
              </div>
            </div>
          </div>
          <?php $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','building-construction-lite'); ?></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','building-construction-lite'); ?></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++ ) {
              $building_construction_lite_mod =  get_theme_mod( 'construction_firm_middle_sec_settigs' . $construction_firm_s );
              if ( 'page-none-selected' != $building_construction_lite_mod ) {
                $construction_firm_post[] = $building_construction_lite_mod;
              }
            }
             if( !empty($construction_firm_post) ) :
            $building_construction_lite_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()) {
                $building_construction_lite_args['post__in'] = get_option('sticky_posts');
            }

            $building_construction_lite_query = new WP_Query( $building_construction_lite_args );
            if ( $building_construction_lite_query->have_posts() ) :
              $construction_firm_s = 1;
          ?>
          <?php  while ( $building_construction_lite_query->have_posts() ) : $building_construction_lite_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('building_construction_lite_about_show_hide', false) !== 'off'){ ?>
    <?php if( get_theme_mod('building_construction_lite_about_us_title') != '' || get_theme_mod('building_construction_lite_about_us_settigs') != ''){ ?>
      <section id="about-us" class="py-5">
        <div class="container">
          <div class="row">
            <?php
              $building_construction_lite_mod =  get_theme_mod( 'building_construction_lite_about_us_settigs');
              if ( 'page-none-selected' != $building_construction_lite_mod ) {
                $construction_firm_about_page[] = $building_construction_lite_mod;
              }
              if( !empty($construction_firm_about_page) ) :
              $building_construction_lite_args = array(
                'post_type' =>array('page'),
                'post__in' => $construction_firm_about_page
              );
              $building_construction_lite_query = new WP_Query( $building_construction_lite_args );
              if ( $building_construction_lite_query->have_posts() ) :
            ?>
            <?php  while ( $building_construction_lite_query->have_posts() ) : $building_construction_lite_query->the_post(); ?>
              <div class="col-lg-5 col-md-5 align-self-center wow slideInLeft">
                <?php if(has_post_thumbnail()){ ?>
                  <div class="img-box mb-4 mb-md-0">
                    <?php the_post_thumbnail(); ?>
                  </div>
                <?php }?>
              </div>
              <div class="col-lg-7 col-md-7 align-self-center wow slideInRight">
                <h3 class="mb-3"><?php echo esc_html( get_theme_mod( 'building_construction_lite_about_us_title','') ); ?></h3>
                <h4 class="mb-4"><?php the_title();?></h4>
                <p class="mb-0"><?php the_excerpt(); ?></p>
              </div>
            <?php endwhile;
            wp_reset_postdata();?>
            <?php else : ?>
            <div class="no-postfound"></div>
              <?php endif;
            endif;?>
          </div>
        </div>
      </section>
    <?php }?>
  <?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
              $building_construction_lite_project_category=  get_theme_mod('construction_firm_category_setting');
              $construction_firm_project_order = get_theme_mod('construction_firm_project_order_type','descending');
              if($building_construction_lite_project_category){
              $construction_firm_args = array( 
                'category_name' => esc_html($building_construction_lite_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';
              }
              $building_construction_lite_page_query = new WP_Query($construction_firm_args);?>
                <?php while( $building_construction_lite_page_query->have_posts() ) : $building_construction_lite_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><?php the_title();?></h4></a>
                            <p class="mb-0"><?php echo wp_trim_words( get_the_content(),15);?></p>
                            <div class="box-button mt-3">
                              <a class="py-2 px-3" href="<?php the_permalink(); ?>"><?php esc_html_e('READ MORE','building-construction-lite');?></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(); ?>
