<?php

$data = $args['data'] ?? array();

$realisation_suivant = ljn_get_option('realisation_suivant');
$realisation_precedent = ljn_get_option('realisation_precedent');
$realisation_localisation = ljn_get_option('realisation_localisation');
$realisation_maitrise_ouvrage = ljn_get_option('realisation_maitrise_ouvrage');
$realisation_equipe_maitrise = ljn_get_option('realisation_equipe_maitrise');
$realisation_equipe_ljn = ljn_get_option('realisation_equipe_ljn');
$realisation_type_marche = ljn_get_option('realisation_type_marche');
$realisation_calendrier = ljn_get_option('realisation_calendrier');
$realisation_montant = ljn_get_option('realisation_montant');
$realisation_superficie = ljn_get_option('realisation_superficie');

$class_bg = 'bg-' . ($data['background_color'] ?? 'orange');

$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]);
}

?><section class="section-cover-project  w-full relative md:flex  md:max-h-none  md:flex-col lg:flex-row  ">
	<div class="left md:basis-6/12 md:basis-7/12 2xl:basis-8/12 relative lg:sticky top-0  lg:h-screen ">
		<?php if (isset($data['cover_image']) && $data['cover_image'] > 0) : ?>
			<?php echo wp_get_attachment_image($data['cover_image'], 'full', false, array('loading' => 'lazy', 'class' => 'scroll-appear aspect-square md:aspect-video lg:aspect-auto lg:absolute top-0 left-0 w-full h-full object-cover object-center')); ?>
		<?php endif; ?>
	</div>
	<div class="right relative md:basis-6/12 md:basis-5/12 2xl:basis-4/12 <?php echo $class_bg; ?> p-6  md:p-12 xl:p-24  ">
		<div class="scroll-appear">
			<?php if (isset($data['details_sur_titre']) && !empty($data['details_sur_titre'])) : ?>
				<h2 class="cover-uptitle font-medium">
					<?php echo $data['details_sur_titre']; ?>
				</h2>
			<?php endif; ?>
			<?php if (isset($data['details_titre']) && !empty($data['details_titre'])) : ?>
				<h1 class="mb-10 text-3xl font-serif">
					<?php echo $data['details_titre']; ?>
				</h1>
			<?php endif; ?>
			<div class="mb-14">
				<?php if (isset($data['localisation']) && !empty($data['localisation'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_localisation) : ?>
							<strong class="block  mb-1">
								<?php echo $realisation_localisation; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['localisation']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['maitrise_ouvrage']) && !empty($data['maitrise_ouvrage'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_maitrise_ouvrage) : ?>
							<strong class="block  mb-1">
								<?php echo $realisation_maitrise_ouvrage; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['maitrise_ouvrage']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['equipe_maitrise']) && !empty($data['equipe_maitrise'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_equipe_maitrise) : ?>
							<strong class="block mb-1">
								<?php echo $realisation_equipe_maitrise; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['equipe_maitrise']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['equipe_ljn']) && !empty($data['equipe_ljn'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_equipe_ljn) : ?>
							<strong class="block  mb-1">
								<?php echo $realisation_equipe_ljn; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['equipe_ljn']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['type_marche']) && !empty($data['type_marche'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_type_marche) : ?>
							<strong class="block  mb-1">
								<?php echo $realisation_type_marche; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['type_marche']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['calendrier']) && !empty($data['calendrier'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_calendrier) : ?>
							<strong class="block  mb-1">
								<?php echo $realisation_calendrier; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['calendrier']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['montant']) && !empty($data['montant'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_montant) : ?>
							<strong class="block  mb-1">
								<?php echo $realisation_montant; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['montant']; ?>
					</h3>
				<?php endif; ?>

				<?php if (isset($data['superficie']) && !empty($data['superficie'])) : ?>
					<h3 class="text-sm mb-5">
						<?php if ($realisation_superficie) : ?>
							<strong class="block text-orange mb-1">
								<?php echo $realisation_superficie; ?>
							</strong>
						<?php endif; ?>
						<?php echo $data['superficie']; ?>
					</h3>
				<?php endif; ?>
			</div>

			<div class="flex justify-between text-red">
				<div>
					<?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">
								<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>
				<div>
					<?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">
								<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>

		</div>
	</div>


</section>
