<?php

if (have_rows('main_story')):
while (have_rows('main_story')): the_row();
$main_story = get_sub_field('choose_main_story');

if (isset($main_story)) {
  if ( !empty($main_story) ) {
  $ns1 = $main_story->ID;
}
}

endwhile;
endif;

$ns1= isset($ns1) ? $ns1: '';

if (have_rows('top_story_1')):
while (have_rows('top_story_1')): the_row();
$top_story_1 = get_sub_field('choose_top_story_1');

if (isset($top_story_1)) {
  if ( !empty($top_story_1) ) {
  $ns2 = $top_story_1->ID;
}
}

endwhile;
endif;

$ns2= isset($ns2) ? $ns2: '';

if (have_rows('top_story_2')):
while (have_rows('top_story_2')): the_row();
$top_story_2 = get_sub_field('choose_top_story_2');

if (isset($top_story_2)) {
  if ( !empty($top_story_2) ) {
  $ns3 = $top_story_2->ID;
}
}

endwhile;
endif;

$ns3= isset($ns3) ? $ns3: '';

if (have_rows('top_story_3')):
while (have_rows('top_story_3')): the_row();
$top_story_3 = get_sub_field('choose_top_story_3');

if (isset($top_story_3)) {
  if ( !empty($top_story_3) ) {
  $ns4 = $top_story_3->ID;
}
}

endwhile;
endif;

$ns4= isset($ns4) ? $ns4: '';


if (have_rows('top_story_4')):
while (have_rows('top_story_4')): the_row();
$top_story_4 = get_sub_field('choose_top_story_4');

if (isset($top_story_4)) {
  if ( !empty($top_story_4) ) {
  $ns5 = $top_story_4->ID;
}
}

endwhile;
endif;

$ns5= isset($ns5) ? $ns5: '';

$args = array( 'posts_per_page' => '1' );
$recent_posts = new WP_Query($args);
while ($recent_posts->have_posts()) :
$recent_posts->the_post();
$permalink = get_the_permalink();
$title = get_the_title();
$no_dup1 = $post->ID;
endwhile;

$args = array('posts_per_page' => '1', 'offset' => '1' );
$recent_posts = new WP_Query($args);
while ($recent_posts->have_posts()) :
$recent_posts->the_post();
$permalink = get_the_permalink();
$title = get_the_title();
$no_dup2 = $post->ID;
endwhile;

$args = array( 'posts_per_page' => '1', 'offset' => '2' );
$recent_posts = new WP_Query($args);
while ($recent_posts->have_posts()) :
$recent_posts->the_post();
$permalink = get_the_permalink();
$title = get_the_title();
$no_dup3 = $post->ID;
endwhile;

$args = array( 'posts_per_page' => '1', 'offset' => '3' );
$recent_posts = new WP_Query($args);
while ($recent_posts->have_posts()) :
$recent_posts->the_post();
$permalink = get_the_permalink();
$title = get_the_title();
$no_dup4 = $post->ID;
endwhile;

$args = array( 'posts_per_page' => '1', 'offset' => '4' );
$recent_posts = new WP_Query($args);
while ($recent_posts->have_posts()) :
$recent_posts->the_post();
$permalink = get_the_permalink();
$title = get_the_title();
$no_dup5 = $post->ID;
endwhile;
?>

<?php if( get_sub_field('home_news_grid_position') == 'one' ) { ?>

  <section class="my-5 news-grid">
    <div class="container">
      <div class="d-none d-sm-none d-lg-block">
        <div class="row justify-content-center">

        <?php
    $args = array(
        'post__not_in' => array($ns1, $ns2, $ns3, $ns4, $ns5, $no_dup1, $no_dup2, $no_dup3, $no_dup4, $no_dup5),
        'posts_per_page' => 4,
        'offset' => 1,
        'post_type' => 'post',
        'category__not_in' => array(23512,23524),
        'order' => 'DESC'
    );
    $news = new WP_Query($args);
    ?>

          <?php if ($news->have_posts()) : ?>

            <?php while ($news->have_posts()) : $news->the_post(); ?>

              <?php
            $permalink = get_the_permalink();
            $title = get_the_title();
            ?>
              <a class="col-12 col-lg-6" href="<?php echo $permalink; ?>">

                    <div class="row mt-4 me-2 news-story">
                      <div class="col-4 p-0 image">

                        <?php
                              if ( has_post_thumbnail() ) {
                              the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' ));
                                }
                                else {
                                echo '<img class="img-fluid w-100" src="' . get_bloginfo( 'stylesheet_directory' )
                                  . '/src/assets/images/default-image.jpg" />';
                                } ?>

                      </div>
                      <div class="col-8 m-auto">
                      <div class="px-5 category">
                         <?php
                        $category = get_the_category();
                        echo $category[0]->cat_name;
                        ?>
                      </div>
                        <h3 class="h4 px-5">
                            <?php echo $title; ?>
                          </h3>
                      </div>
                    </div>

                </a>

                <?php endwhile; wp_reset_postdata(); ?>
                  <?php endif; ?>

      </div>
      </div>
        <div class="d-block d-lg-none">
          <div class="row">
        <?php
         $args = array(
           'post__not_in' => array($ns1, $ns2, $ns3, $ns4, $ns5, $no_dup1, $no_dup2, $no_dup3, $no_dup4, $no_dup5),
           'posts_per_page' => 4,
           'category__not_in' => array(23512,9),
              );
         $loop = new WP_Query( $args ); ?>

          <div class="row mx-auto my-auto">
            <div id="locationCarousel" class="carousel slide w-100" data-bs-interval="false" data-ride="carousel">
              <div class="carousel-inner col-10 mx-auto" role="listbox">
                <?php
                  $c = 0;
                  $class = '';
                  while ( $loop->have_posts() ) : $loop->the_post();
                  $excludeID[] = $post->ID;
                  $GLOBALS['globalexcludeID'] = $excludeID;

                  $c++;
                  if ( $c == 1 ) $class .= ' active';
                  else $class = '';
                  ?>
                  <div class="carousel-item <?php echo $class; ?>">

                    <a class="w-100" href="<?php the_permalink(); ?>">

                      <?php if(has_post_thumbnail()){

            the_post_thumbnail('medium', array( 'class' => 'img-fluid d-block w-100' ));

        } ?>

              <div class="bg-black pt-5 pb-3 text-center">
                <p class="text-white">

                <?php
                $category = get_the_category();
                echo $category[0]->cat_name;
                ?>

                          </p>
                          <h3 class="h6 text-white text-center px-4">

            <?php the_title(); ?>

          </h3>
                        </div>
                    </a>
                  </div>
                  <?php
         endwhile;
         ?>
              </div>
              <button class="carousel-control-prev" type="button" data-bs-target="#locationCarousel" data-bs-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="visually-hidden">Previous</span>
              </button>
              <button class="carousel-control-next" type="button" data-bs-target="#locationCarousel" data-bs-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="visually-hidden">Next</span>
              </button>
              <div class="carousel-indicators pt-2">

                <?php
                $args = array(
                  'post__not_in' => array($ns1, $ns2, $ns3, $ns4, $ns5, $no_dup1, $no_dup2, $no_dup3, $no_dup4, $no_dup5),
                  'posts_per_page' => 4,
                     );
                      $query = new WP_Query( $args );
                      ?>
                  <?php if($query->have_posts()) : ?>
                    <?php $i = 0; ?>
                      <?php while($query->have_posts()) : $query->the_post(); ?>



                        <button type="button" data-bs-target="#locationCarousel" data-bs-slide-to="<?php echo $i ?>" class="<?php if($i === 0): ?>active<?php endif; ?>" aria-current="true" aria-label="Slide <?php echo $i ?>"></button>

                        <?php $i++; ?>

                          <?php endwhile ?>
                            <?php endif ?>
                              <?php wp_reset_postdata(); ?>

              </div>

            </div>
            <!-- /#locationCarousel-->

          </div>

          <?php wp_reset_postdata(); ?>

      </div>
        </div>
    </div>
  </section>

  <?php }

 if( get_sub_field('home_news_grid_position') == 'two' ) { ?>

   <section class="my-5 news-grid">
     <div class="container">
       <div class="d-none d-sm-none d-lg-block">
         <div class="row justify-content-center">

         <?php
     $args = array(
         'post__not_in' => array($ns1, $ns2, $ns3, $ns4, $ns5, $no_dup1, $no_dup2, $no_dup3, $no_dup4, $no_dup5),
         'posts_per_page' => 4,
         'post_type' => 'post',
         'offset' => '4',
         'order' => 'DESC'
     );
     $news = new WP_Query($args);
 ?>

           <?php if ($news->have_posts()) : ?>

             <?php while ($news->have_posts()) : $news->the_post(); ?>

               <?php
             $permalink = get_the_permalink();
             $title = get_the_title();
             ?>
               <a class="col-12 col-lg-6 p-0" href="<?php echo $permalink; ?>">
                   <div class="news-wrap">
                     <div class="row">
                       <div class="col-4 image p-0">

                         <?php
                               if ( has_post_thumbnail() ) {
                               the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' ));
                                 }
                                 else {
                                 echo '<img class="img-fluid w-100" src="' . get_bloginfo( 'stylesheet_directory' )
                                   . '/src/assets/images/default-image.jpg" />';
                                 } ?>

                       </div>
                       <div class="col-8 p-0 m-auto">
                       <p class="px-5 mb-0">
                          <?php
                         $category = get_the_category();
                         echo $category[0]->cat_name;
                         ?>
                       </p>
                         <h3 class="h4 px-5">

                             <?php echo $title; ?>

                           </h3>
                       </div>
                     </div>
                   </div>
                 </a>

                 <?php endwhile; wp_reset_postdata(); ?>
                   <?php endif; ?>

       </div>
     </div>
     <div class="d-block d-lg-none">
       <div class="row">
     <?php
      $args = array(
        'post__not_in' => array($ns1, $ns2, $ns3, $ns4, $ns5, $no_dup1, $no_dup2, $no_dup3, $no_dup4, $no_dup5),
        'posts_per_page' => 4,
        'post_type' => 'post',
        'offset' => '4',
           );
      $loop = new WP_Query( $args ); ?>

       <div class="row mx-auto my-auto">
         <div id="locationCarousel" class="carousel slide w-100" data-bs-interval="false" data-ride="carousel">
           <div class="carousel-inner col-10 mx-auto" role="listbox">
             <?php
               $c = 0;
               $class = '';
               while ( $loop->have_posts() ) : $loop->the_post();
               $excludeID[] = $post->ID;

               $c++;
               if ( $c == 1 ) $class .= ' active';
               else $class = '';
               ?>
               <div class="carousel-item <?php echo $class; ?>">

                 <a class="w-100" href="<?php the_permalink(); ?>">

                   <?php if(has_post_thumbnail()){

         the_post_thumbnail('medium', array( 'class' => 'img-fluid d-block w-100' ));

     } ?>

           <div class="bg-black pt-5 pb-3 text-center">
             <p class="text-white">

             <?php
             $category = get_the_category();
             echo $category[0]->cat_name;
             ?>

                       </p>
                       <h3 class="h6 text-white text-center px-4">

         <?php the_title(); ?>

       </h3>
                     </div>
                 </a>
               </div>
               <?php
      endwhile;
      ?>
           </div>
           <button class="carousel-control-prev" type="button" data-bs-target="#locationCarousel" data-bs-slide="prev">
             <span class="carousel-control-prev-icon" aria-hidden="true"></span>
             <span class="visually-hidden">Previous</span>
           </button>
           <button class="carousel-control-next" type="button" data-bs-target="#locationCarousel" data-bs-slide="next">
             <span class="carousel-control-next-icon" aria-hidden="true"></span>
             <span class="visually-hidden">Next</span>
           </button>
           <div class="carousel-indicators pt-2">

             <?php
             $args = array(
               'post__not_in' => array($ns1, $ns2, $ns3, $ns4, $ns5, $no_dup1, $no_dup2, $no_dup3, $no_dup4, $no_dup5),
               'posts_per_page' => 4,
               'post_type' => 'post',
               'offset' => '4',
                  );
                   $query = new WP_Query( $args );
                   ?>
               <?php if($query->have_posts()) : ?>
                 <?php $i = 0; ?>
                   <?php while($query->have_posts()) : $query->the_post(); ?>



                     <button type="button" data-bs-target="#locationCarousel" data-bs-slide-to="<?php echo $i ?>" class="<?php if($i === 0): ?>active<?php endif; ?>" aria-current="true" aria-label="Slide <?php echo $i ?>"></button>

                     <?php $i++; ?>

                       <?php endwhile ?>
                         <?php endif ?>
                           <?php wp_reset_postdata(); ?>

           </div>

         </div>
         <!-- /#locationCarousel-->

       </div>

       <?php wp_reset_postdata(); ?>

   </div>
     </div>
   </div>
   </section>

  <?php } ?>
