<?php

//
//   Template Name: Static Styleguide
//

/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default. Please note that
 * this is the WordPress construct of pages: specifically, posts with a post
 * type of `page`.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package ljn
 */

$fields = get_fields();
$disable_link = $fields['disable_link'] ?? false;

if($disable_link) {
	// Redirect to home
	wp_redirect( home_url() );
	exit;
}



get_header();


$slider = $fields['slider'] ?? array();

$realisation_suivant = ljn_get_option('realisation_suivant');
$realisation_precedent = ljn_get_option('realisation_precedent');
$realisation_back = ljn_get_option('realisation_back');
$previous_post_link = false;
$previous_post_title = false;
$next_post_link = false;
$next_post_title = false;

$next_post = get_posts(array(
    'post_type' => 'realisation',
	'meta_query' => array(
		'relation' => 'OR',
		 array(
			 'key'     => 'disable_link',
			 'value'   => 0,
			 'compare' => '='
		 ),
		 array(
			 'key'     => 'disable_link',
			 'compare' => 'NOT EXISTS',
		 ),
	),
    'orderby' => 'post_date',
    'order' => 'ASC',
    'posts_per_page' => 1,
    'date_query' => array(
        'after' => get_post()->post_date // Exclure le post actuel de la requête
    )
));
// Récupérer le post suivant
// $next_post = get_adjacent_post(false, '', false);
if (isset($next_post[0]->ID)) {
	$next_post_link = get_permalink($next_post[0]);
	$next_post_title = get_the_title($next_post[0]);
}

// Récupérer le post précédent
// Requête pour obtenir le post précédent avec la contrainte du champ ACF
$previous_post = get_posts(array(
    'post_type' => 'realisation',
	'meta_query' => array(
		'relation' => 'OR',
		 array(
			 'key'     => 'disable_link',
			 'value'   => 0,
			 'compare' => '='
		 ),
		 array(
			 'key'     => 'disable_link',
			 'compare' => 'NOT EXISTS',
		 ),
	),
    'orderby' => 'post_date',
    'order' => 'DESC',
    'posts_per_page' => 1,
    'date_query' => array(
        'before' => get_post()->post_date // Exclure le post actuel de la requête
    )
));

// $previous_post = get_adjacent_post(false, '', true);
if (isset($previous_post[0]->ID)) {
	$previous_post_link = get_permalink($previous_post[0]);
	$previous_post_title = get_the_title($previous_post[0]);
}

?>


<main id="main relative">
	<?php get_template_part('template-parts/sections/section', 'cover-project', array('data' => $fields)); ?>


	<article class="post hidden md:block">
		<div class="container max-w-6xl">

			<?php if ((isset($fields['bloc_titre']) && !empty($fields['bloc_titre'])) || (isset($fields['bloc_texte']) && !empty($fields['bloc_texte']))) : ?>
				<section class="section-post-title-text py-20 md:pt-[170px] max-w-3xl mx-auto scroll-appear">

					<?php if (isset($fields['bloc_titre']) && !empty($fields['bloc_titre'])) : ?>
						<h3 class="section-title liseret liseret__lg mb-8">
							<?php echo $fields['bloc_titre']; ?>
						</h3>
					<?php endif; ?>
					<?php if (isset($fields['bloc_texte']) && !empty($fields['bloc_texte'])) : ?>
						<p class="font-medium font-body text-green">
							<?php echo $fields['bloc_texte']; ?>
						</p>
					<?php endif; ?>
				</section>
			<?php endif; ?>

			<?php if (isset($fields['blocs']) && is_array($fields['blocs']) && sizeof($fields['blocs']) > 0) : ?>
				<section class="section-img-text md:pt-[170px] scroll-appear">

					<div class="card-img-text-list flex mx-auto gap-20 md:gap-[170px]">
						<?php foreach ($fields['blocs'] as $bloc) : ?>

							<?php


							if (empty($bloc['titre_orange']) && (empty($bloc["texte"])) ) : ?>
								<div class=" scroll-appear scroll-appear-fade-up">
									<?php if($bloc['media_select'] == 'image' && $bloc['image'] > 0){
										echo wp_get_attachment_image($bloc['image'], 'full', false, array('loading' => 'lazy', 'class' => ''));
									}elseif($bloc['media_select'] == 'video' && $bloc['video'] > 0){
										echo "<video src='".$bloc['video']['url']."' autoplay muted loop playsinline></video>";
									}
									?>

								</div>

							<?php else : ?>

								<div class="card-img_txt md:flex gap-12 lg:gap-24 mx-auto w-full ">
									<div class="card-img md:basis-1/2 lg:basis-auto mb-4 md:mb-0">
										<?php if($bloc['media_select'] == 'image' && $bloc['image'] > 0){
											echo wp_get_attachment_image($bloc['image'], 'full', false, array('loading' => 'lazy', 'class' => ''));
										}else{
											echo "<video src='".$bloc['video']['url']."' autoplay muted loop playsinline></video>";
										}
										?>
									</div>
									<div class="card-body  ">
										<div class="content md:max-w-[280px]">
											<?php if (!empty($bloc['titre_orange'])) : ?>
												<p>
													<strong>
														<?php echo $bloc['titre_orange']; ?>
													</strong>
												</p>
											<?php endif; ?>
											<?php if (!empty($bloc['texte'])) : ?>
												<p>
													<?php echo $bloc['texte']; ?>
												</p>
											<?php endif; ?>
										</div>
									</div>
								</div>
							<?php endif; ?>

						<?php endforeach; ?>
					</div>
				</section>
			<?php endif; ?>

			<?php get_template_part('template-parts/sections/section', 'slider', array('data' => $slider)); ?>
		</div>
	</article>

	<div class="container max-w-6xl hidden md:block  scroll-appear md:pt-[170px]">
		<div class="flex justify-between text-red">
			<div class="basis-4/12 flex justify-start ">
				<?php if ($previous_post_link) : ?>
					<a href="<?php echo $previous_post_link; ?>" class="btn-prev flex items-center gap-1">

						<svg width="20" height="11" viewBox="0 0 20 11" fill="none" xmlns="http://www.w3.org/2000/svg" style="margin-top:3px">
							<path d="M9.366 0L5.3 4.069H20V6.792H5.3L9.366 10.86H5.43L0 5.43L5.43 0H9.366Z" fill="#E26349" />
						</svg>
						<?php if ($realisation_precedent) : ?>
							<?php echo $realisation_precedent; ?>
						<?php endif; ?>

					</a>
				<?php endif; ?>
			</div>
			<?php if (isset($realisation_back['title']) &&  !empty($realisation_back['title'])) : ?>
				<div class="basis-4/12 flex justify-center">
					<a href="<?php echo $realisation_back['url']; ?>" class="card-link btn-primary ">
						<span><?php echo lnj_load_inline_svg('circle-left.svg'); ?></span>
						<span>
							<?php echo $realisation_back['title']; ?>
						</span>
						<div class="icon">
							<?php echo lnj_load_inline_svg('plus.svg'); ?>
							<?php echo lnj_load_inline_svg('arrow-right.svg'); ?>
						</div>
					</a>
				</div>
			<?php endif; ?>
			<div class="basis-4/12 flex justify-end">
				<?php if ($next_post_link) : ?>
					<a href="<?php echo $next_post_link; ?>" class="btn-next flex items-center  gap-1">
						<?php if ($realisation_suivant) : ?>
							<?php echo $realisation_suivant; ?>
						<?php endif; ?>
						<svg width="20" height="11" viewBox="0 0 20 11" fill="none" xmlns="http://www.w3.org/2000/svg" style="margin-top:5px">
							<path d="M10.634 10.86L14.7 6.79099L3.55723e-07 6.79098L5.93775e-07 4.06798L14.7 4.06799L10.634 -1.51239e-05L14.57 -1.47798e-05L20 5.42999L14.57 10.86L10.634 10.86Z" fill="#E26349" />
						</svg>
					</a>
				<?php endif; ?>
			</div>
		</div>
</main><!-- #main -->






<?php
get_footer();
