<section class="featured mb-5">
  <div class="container">
    <div class="row" data-aos="fade-up">
      <div class="col-12 col-md-5">

        <?php if (have_rows('feature_story')):
            while (have_rows('feature_story')): the_row();

            $main_story = get_sub_field('choose_main_story');
            $category_name = get_sub_field('choose_display_category') ;
            $post = $main_story; setup_postdata($post);

            if ($main_story) { ?>

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

              <?php the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' )); ?>

            </a>
            <div class="d-flex flex-column">
              <div class="category my-0 my-md-2 my-lg-3">

                <?php

                if ($category_name): ?>

                  <a class="no-transition-link" href="<?php echo esc_url(get_term_link($category_name)); ?>"><?php echo esc_html($category_name->name); ?></a>

                  <?php else :

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

              endif; ?>

                    |
                    <?php the_time( 'd/m/y');?>

              </div>

              <a href="<?php the_permalink(); ?>">

                <p class="h1 mb-3 pe-4">

                  <?php the_title(); ?>

                </p>
              </a>
            </div>
          </div>

          <?php } else {

              $args = array(
                'posts_per_page'      => 1,  
                'orderby'             => 'date',  
                'order'               => 'DESC', 
                'category__not_in' => array(1596,23512,9), // Excludes the category IDs 
                'ignore_sticky_posts' => true // This ignores sticky posts
              );  

              $recent_posts1 = new WP_Query($args);
              while ($recent_posts1->have_posts()) :
              $recent_posts1->the_post();
              $permalink = get_the_permalink();
              $title = get_the_title();
              ?>

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

                <?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" />';
                    }
                    ?>
              </a>

              <div class="d-flex flex-column">
                <div class="category my-0 my-md-2 my-lg-3">

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

                    |
                    <?php the_time( 'd/m/y');?>

                </div>
                <a href="<?php the_permalink(); ?>">

                  <p class="h1 mb-3 pe-4">

                    <?php the_title(); ?>

                  </p>
                </a>
              </div>
            </div>

            <?php endwhile; ?>

            <?php wp_reset_postdata(); ?>

              <?php } ?>

                <?php endwhile; ?>

                  <?php endif; ?>

                    <?php wp_reset_postdata(); ?>


                      <?php if (have_rows('feature_story_two')):

              while (have_rows('feature_story_two')): the_row();

              $category_name_two = get_sub_field('choose_display_category_two') ;
              $main_story_two = get_sub_field('choose_main_story_two');
              $post = $main_story_two; setup_postdata($post); ?>

                        <?php  if ($main_story_two) { ?>

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

                              <?php the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' )); ?>

                                <div class="d-flex flex-column">
                                  <div class="category my-0 my-md-2 my-lg-3">

                                    <?php

                              if ($category_name_two): ?>

                                      <?php echo esc_html($category_name_two->name); ?>

                                        <?php else :

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

                            endif; ?>

                                          |
                                          <?php the_time( 'd/m/y');?>

                                  </div>


                                  <p class="h1 mb-3 pe-4">

                                    <?php the_title(); ?>

                                  </p>

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

                          <?php } else { ?>

                            <?php
                                $args = array(
                                  'posts_per_page'      => 1,
                                  'offset'              => 3,  // Skips the latest post and fetches the second latest post
                                  'orderby'             => 'date',
                                  'order'               => 'DESC',
                                  'category__not_in' => array(1596,23512,9), // Excludes the category IDs
                                  'ignore_sticky_posts' => true
                              );
                              
                                $recent_posts1 = new WP_Query($args);
                                while ($recent_posts1->have_posts()) :
                                $recent_posts1->the_post();
                                $permalink = get_the_permalink();
                                $title = get_the_title();
                                ?>


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

                                  <?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 class="d-flex flex-column">
                                      <div class="category my-0 my-md-2 my-lg-3">

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

                                          |
                                          <?php the_time( 'd/m/y');?>

                                      </div>

                                      <p class="h5 mb-3">

                                        <?php the_title(); ?>

                                      </p>
                                    </div>
                                      </a>
                                </div>

                              <?php endwhile; ?>

                                <?php } ?>

                                  <?php endwhile; ?>

                                    <?php endif; ?>

                                      <?php wp_reset_postdata(); ?>

      </div>

      <div class="col-12 col-md-7">
        <div class="row">

          <?php if (have_rows('top_story_1')):

                  while (have_rows('top_story_1')): the_row();

                    $top_story_1 = get_sub_field('choose_top_story_1');
                    $override_heading_top_1 = get_sub_field('override_heading_top_1');
                    $top_story_1_heading = get_sub_field('top_story_1_heading') ;
                    $category_name = get_sub_field('choose_display_category_top_1') ;?>

            <div class="col-12 col-sm-6 col-lg-4 my-4 portrait">

              <?php if ($top_story_1): ?>

                <?php $post = $top_story_1; setup_postdata($post); ?>

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

                      <?php the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' )); ?>

                    </a>
                    <div class="d-flex flex-column">
                      <div class="category my-0 my-md-2 my-lg-3">

                        <?php

                                if ($category_name): ?>

                          <a class="no-transition-link" href="<?php echo esc_url(get_term_link($category_name)); ?>"><?php echo esc_html($category_name->name); ?></a>

                          <?php endif; ?>

                            |
                            <?php the_time( 'd/m/y');?>

                      </div>
                      <a href="<?php the_permalink(); ?>">

                        <p class="h5 mb-3">

                          <?php the_title(); ?>

                        </p>
                      </a>
                    </div>
                  </div>

                  <?php wp_reset_postdata(); ?>
                  <?php break; // Exit the loop after the first top-story-1 post ?>

              <?php else: ?>

                    <?php
                                $args = array(
                                  'posts_per_page'      => 1,
                                  'offset'              => 1, 
                                  'orderby'             => 'date',
                                  'order'               => 'DESC',
                                  'category__not_in' => array(1596,23512,9), // Excludes the category IDs
                                  'ignore_sticky_posts' => true
                              );
                              
                                $recent_posts1 = new WP_Query($args);
                                while ($recent_posts1->have_posts()) :
                                $recent_posts1->the_post();
                                $permalink = get_the_permalink();
                                $title = get_the_title();
                                ?>

                      <div class="top-story1">
                        <a class="w-100" href="<?php the_permalink(); ?>">
                          <?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 class="d-flex flex-column">
                              <div class="category my-0 my-md-2 my-lg-3">

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

                                  |
                                  <?php the_time( 'd/m/y');?>

                              </div>


                              <p class="h4 mb-3">

                                <?php the_title(); ?>

                              </p>

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

                      <?php endwhile; ?>


                        <?php endif; ?>

                          <?php wp_reset_postdata(); ?>

            </div>

            <?php endwhile; ?>

              <?php endif; ?>

                <?php wp_reset_postdata(); ?>

                  <div class="col-sm-6 col-lg-8 mb-4 landscape">

                    <?php if (have_rows('top_story_2')):

                            while (have_rows('top_story_2')): the_row();

                              $top_story_2 = get_sub_field('choose_top_story_2');
                              $override_heading_top_2 = get_sub_field('override_heading_top_2');
                              $top_story_2_heading = get_sub_field('top_story_2_heading') ;
                              $category_name = get_sub_field('choose_display_category_top_2') ;?>

                      <?php if ($top_story_2): ?>

                        <?php $post = $top_story_2; setup_postdata($post); ?>

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

                              <?php the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' )); ?>

                                <div class="d-flex flex-column">
                                  <div class="category my-0 my-md-2 my-lg-3">

                                    <?php

                                          if ($category_name): ?>

                                      <?php echo esc_html($category_name->name); ?>

                                        <?php endif; ?>

                                          |
                                          <?php the_time( 'd/m/y');?>

                                  </div>
                                  <p class="h4 mb-3">

                                    <?php the_title(); ?>

                                  </p>
                                </div>
                            </a>
                          </div>

                          <?php else: ?>

                            <?php
                                        $args = array(
                                          'posts_per_page'      => 1,
                                          'offset'              => 2, 
                                          'orderby'             => 'date',
                                          'order'               => 'DESC',
                                          'category__not_in' => array(1596,23512,9), // Excludes the category IDs
                                          'ignore_sticky_posts' => true
                                      );
                                      
                                        $recent_posts1 = new WP_Query($args);
                                        while ($recent_posts1->have_posts()) :
                                        $recent_posts1->the_post();
                                        $permalink = get_the_permalink();
                                        $title = get_the_title();
                                        ?>

                              <div class="top-story2">
                                <a class="w-100" href="<?php the_permalink(); ?>">
                                  <?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 class="d-flex flex-column">
                                      <div class="category my-0 my-md-2 my-lg-3">

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

                                          |
                                          <?php the_time( 'd/m/y');?>

                                      </div>

                                      <p class="h4 mb-3">

                                        <?php the_title(); ?>

                                      </p>

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

                              <?php endwhile; ?>

                                <?php endif; ?>

                                  <?php wp_reset_postdata(); ?>



                                    <?php endwhile; ?>

                                      <?php endif; ?>

                                        <?php wp_reset_postdata(); ?>

                  </div>


                  <div class="col-sm-6 col-lg-8 mb-4 landscape">

                    <?php if (have_rows('top_story_3')):

                            while (have_rows('top_story_3')): the_row();

                              $top_story_2 = get_sub_field('choose_top_story_3');
                              $override_heading_top_2 = get_sub_field('override_heading_top_3');
                              $top_story_2_heading = get_sub_field('top_story_3_heading') ;
                              $category_name = get_sub_field('choose_display_category_top_3') ;?>

                      <?php if ($top_story_2): ?>

                        <?php $post = $top_story_2; setup_postdata($post); ?>

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

                              <?php the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' )); ?>

                                <div class="d-flex flex-column">
                                  <div class="category my-0 my-md-2 my-lg-3">

                                    <?php

                                                            if ($category_name): ?>

                                      <?php echo esc_html($category_name->name); ?>

                                        <?php endif; ?>

                                          |
                                          <?php the_time( 'd/m/y');?>

                                  </div>
                                  <p class="h4 mb-3">

                                    <?php the_title(); ?>

                                  </p>
                                </div>
                            </a>
                          </div>

                          <?php else: ?>

                            <?php
                                          $args = array(
                                            'posts_per_page'      => 1,
                                            'offset'              => 4,  // Skips the latest post and fetches the second latest post
                                            'orderby'             => 'date',
                                            'order'               => 'DESC',
                                            'category__not_in' => array(1596,23512,9), // Excludes the category IDs
                                            'ignore_sticky_posts' => true
                                        );
                                        
                                          $recent_posts1 = new WP_Query($args);
                                          while ($recent_posts1->have_posts()) :
                                          $recent_posts1->the_post();
                                          $permalink = get_the_permalink();
                                          $title = get_the_title();
                                          ?>

                              <div class="top-story3">
                                <a class="w-100" href="<?php the_permalink(); ?>">
                                  <?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 class="d-flex flex-column">
                                      <div class="category my-0 my-md-2 my-lg-3">

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

                                          |
                                          <?php the_time( 'd/m/y');?>

                                      </div>
                                      <p class="h4 mb-3">

                                        <?php the_title(); ?>

                                      </p>

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

                              <?php endwhile; ?>

                                <?php endif; ?>

                                  <?php wp_reset_postdata(); ?>

                                    <?php endwhile; ?>

                                      <?php endif; ?>

                                        <?php wp_reset_postdata(); ?>

                  </div>
                  <div class="col-sm-6 col-lg-4 mb-4 portrait masonry">

                    <?php if (have_rows('top_story_4')):

                            while (have_rows('top_story_4')): the_row();

                              $top_story_4 = get_sub_field('choose_top_story_4');
                              $override_heading_top_4 = get_sub_field('override_heading_top_4');
                              $top_story_4_heading = get_sub_field('top_story_4_heading') ;
                              $category_name = get_sub_field('choose_display_category_top_4') ;?>

                      <?php if ($top_story_4): ?>

                        <?php $post = $top_story_4; setup_postdata($post); ?>

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

                              <?php the_post_thumbnail('large', array( 'class' => 'img-fluid w-100' )); ?>

                                <div class="d-flex flex-column">
                                  <div class="category my-0 my-md-2 my-lg-3">

                                    <?php

                                          if ($category_name): ?>

                                      <?php echo esc_html($category_name->name); ?>

                                        <?php endif; ?>

                                          |
                                          <?php the_time( 'd/m/y');?>

                                  </div>

                                  <p class="mb-2 d-none d-lg-block">
                                    <?php the_time( 'd/m/y');?>
                                  </p>

                                  <p class="h5 mb-3">

                                    <?php the_title(); ?>

                                  </p>

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

                          <?php else: ?>

                            <?php
                                          $args = array(
                                            'posts_per_page'      => 1,
                                            'offset'              => 5,  // Skips the latest post and fetches the second latest post
                                            'orderby'             => 'date',
                                            'order'               => 'DESC',
                                            'category__not_in' => array(1596,23512,9), // Excludes the category IDs
                                            'ignore_sticky_posts' => true
                                        );
                                        
                                          $recent_posts1 = new WP_Query($args);
                                          while ($recent_posts1->have_posts()) :
                                          $recent_posts1->the_post();
                                          $permalink = get_the_permalink();
                                          $title = get_the_title();
                                          ?>

                              <div class="top-story4">
                                <a class="w-100" href="<?php the_permalink(); ?>">
                                  <?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 class="d-flex flex-column">
                                      <div class="category my-0 my-md-2 my-lg-3">

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

                                          |
                                          <?php the_time( 'd/m/y');?>

                                      </div>

                                      <p class="h5 mb-3">

                                        <?php the_title(); ?>

                                      </p>

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

                              <?php endwhile; ?>

                                <?php endif; ?>

                                  <?php wp_reset_postdata(); ?>

                                    <?php endwhile; ?>

                                      <?php endif; ?>

                                        <?php wp_reset_postdata(); ?>

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