<?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;

$heading = get_sub_field('audio_title');
$subheading = get_sub_field('audio_sub_title');
$image = get_sub_field('audio_image');
$link = get_sub_field('audio_link');
?>

<?php if ($link) : ?>

  <section class="audio-story d-flex mt-5">
		<div class="container">
			<div class="row relative">
					<div class="roundel align-self-center grow" data-aos="fade-left">
						<a class="no-transition-link h-100 w-100" href="<?php echo $link['url']; ?>">

						<?php if ( $subheading ) : ?>
							<p class="h5 pt-lg-4">
							<?php echo $subheading; ?>
						</p>
							<?php endif; ?>

								<?php if ( $heading) : ?>
									<h2 class="h1 mt-4">
							<?php echo $heading; ?>
						</h2>
									<?php endif; ?>

								<?php endif ; ?>

								</a>
				</div>
	  <div class="image">

			<?php if ($link) : ?>

			<a class="no-transition-link h-100 w-100" href="<?php echo $link['url']; ?>">

	        <?php if ( $image ) : ?>

	          <img class="card-img-top" src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>">

	          <?php endif; ?>

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

<?php else:

	$args = array(
	'posts_per_page' => 1,
	'category_name'  => 'audio',

);
$feature = new WP_Query($args);
?>

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

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

				<?php
			$permalink = get_the_permalink();
			$title = get_the_title();
			?>

	<section class="feature-story d-flex mt-5">
		<div class="container">
		<div class="row">
				<div class="roundel bg-audio align-self-center grow" data-aos="fade-right" >

					<a class="no-transition-link h-100 w-100" href="<?php echo $permalink ?>">

						<p class="h5 text-white">
					Audio
					</p>

							<?php if ( $title) : ?>
								<h2 class="h1 text-white">
						<?php echo $title; ?>
					</h2>
								<?php endif; ?>

							</a>
			</div>
	<div class="image">
		<a class="no-transition-link w-100" href="<?php echo $permalink ?>">

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

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

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

	<section class="my-5 audio-home">
		<div class="container">
			<div class="d-none d-sm-none d-lg-block">
				<div class="row justify-content-center">
					<div class="col-12 col-md-7">

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

								<?php if( get_row_layout() == 'home_news_grid' ) {

	$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,
	'category_name'  => 'audio',
	);
	$news = new WP_Query($args);
	if ($news->have_posts()) :
	while ($news->have_posts()) : $news->the_post();
	$permalink = get_the_permalink();
	$title = get_the_title();
	?>
									<a class="pt-4" 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 m-auto">
													<p class="px-5 mb-0">
														Audio
													</p>
													<h3 class="h4 px-5">

																	 <?php echo $title; ?>

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

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


										endwhile;

										else :

											$args = array(
												'posts_per_page' => 4,
												'category_name'  => 'audio',
												);
												$news = new WP_Query($args);
												if ($news->have_posts()) :
													while ($news->have_posts()) : $news->the_post();
														$permalink = get_the_permalink();
														$title = get_the_title();
														?>
											<a class="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">
																Audio
															</p>
															<h3 class="h4 px-5">

																																				 <?php echo $title; ?>

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

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

										endif; ?>

					</div>
					<div class="col-12 col-lg-5 text-center">

            <?php $audio_advert_image = get_field('audio_advert_image');
                  $audio_advert_link = get_field('audio_advert_link');  ?>

            <div class="advert relative mt-4">
              <a href="<?php echo $audio_advert_link; ?>" target="_blank">
              <p>Advertisement</p>
              <img class="img-fluid" src="<?php echo $audio_advert_image; ?>" alt="the manc audio ad">
            </a>
            </div>
					</div>
				</div>
			</div>

			<div class="d-block d-lg-none">
				<?php
	$args = array(
	'posts_per_page' => 4,
	'offset' => 1,
	'category_name'  => 'audio',
	);
	$loop = new WP_Query( $args ); ?>

					<div class="row mx-auto my-auto">
						<div class="eats-logo text-center">
							<img class="img-fluid" src="<?php echo esc_url( get_template_directory_uri() . '/src/assets/images/audio-logo.png'); ?>" alt="Audio Logo" />
						</div>
						<div id="audioCarousel" 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();
	$c++;
	if ( $c == 1 ) $class .= ' active';
	else $class = '';
	?>
									<div class="carousel-item <?php echo $class; ?>">

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

											<?php if(has_post_thumbnail()){

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

	} ?>

												<div class="bg-audio pt-5 pb-3 text-center">
													<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="#audioCarousel" 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="#audioCarousel" 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(
	'offset' => 1,
	'posts_per_page' => 4,
	'tax_query' => array(
			array(
					"taxonomy" => "category",
					'field' => 'slug',
					'terms' => 'audio'
			)
	 )
	);
	$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="#audioCarousel" 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>
						<!-- /#eatsCarousel-->

					</div>

					<?php wp_reset_postdata(); ?>

						<?php get_template_part('/template-parts/audio-video'); ?>

            <div class="col-12 col-lg-5 text-center">

              <?php $audio_advert_image = get_field('audio_advert_image');
                    $audio_advert_link = get_field('audio_advert_link');  ?>

              <div class="advert relative mt-4">
                <a href="<?php echo $audio_advert_link; ?>" target="_blank">
                <p>Advertisement</p>
                <img class="img-fluid" src="<?php echo $audio_advert_image; ?>" alt="the manc audio ad">
              </a>
              </div>
            </div>
			</div>
		</div>
	</section>
