<?php
//print_r($attributes);
extract($attributes);

global $wp_query;
$temp = $wp_query;

if (is_archive() || is_search() || (is_home() && is_front_page()) || is_home()) {
	$my_query = $wp_query;
} else {
	$wp_query = $my_query = Flo_Guten_Blocks_Helper::get_post_query($attributes);
}

Flo_Guten_Blocks_Helper::load_google_fonts([
	$postTitleTypography,
	$postDateTypography,
	$postTaxonomyTypography,
	$paginationArrowsTypography,
	$paginationNumbersTypography,
]);
?>
<div class="
	<?php echo $className; ?>
	flo-block
	<?php echo Flo_Guten_Blocks_Helper::get_additional_class($attributes); ?>
	flo-block-post-cards
	flo-block-post-cards--<?php echo $layout; ?>-<?php echo $attributes['imagePosition']; ?>
	<?php echo !$my_query->have_posts() ? 'flo-block-post-cards--not-found' : ''; ?>
	" style="
	--gap-between-items: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
	$gapBetweenItems
); ?>;
	--image-height: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
													$imageHeight
												); ?>;
	--meta-color: <?php echo Flo_Guten_Blocks_Helper::get_style_color(
										$metaColor
									); ?>;
	--title-color: <?php echo Flo_Guten_Blocks_Helper::get_style_color(
									$titleColor
								); ?>;
	--info-background-color: <?php echo Flo_Guten_Blocks_Helper::get_style_color(
										$infoBackgroundColor
									); ?>;
	--mobile-image-height: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
															$mobileImageHeight
														); ?>;
	--mobile-gap-between-items: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
														$mobileGapBetweenItems
													); ?>;

	--top-margin: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
																$topMargin
															); ?>;
	--bottom-margin: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
									$bottomMargin
								); ?>;
	--mobile-top-margin: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
											$mobileTopMargin
										); ?>;
	--mobile-bottom-margin: <?php echo Flo_Guten_Blocks_Helper::get_em_by_value(
													$mobileBottomMargin
												); ?>;
	<?php echo Flo_Guten_Blocks_Helper::get_style_for_typography(
														'post-title',
														$postTitleTypography
													); ?>
	<?php echo Flo_Guten_Blocks_Helper::get_style_for_typography(
		'post-date',
		$postDateTypography
	); ?>
	<?php echo Flo_Guten_Blocks_Helper::get_style_for_typography(
		'post-taxonomy',
		$postTaxonomyTypography
	); ?>
	 " <?php echo !empty($anchor) ? 'id="' . $anchor . '"' : ''; ?>>
	<?php if ($my_query->have_posts()) { ?>
		<div class="flo-block-post-cards__items">
			<?php while ($my_query->have_posts()) {
		$my_query->the_post();

		$categoryObject = NULL;
		if ($displayPostTaxonomy && !empty($taxonomyType)) {
			$categoryObject = Flo_Guten_Blocks_Helper::getListingCategory(get_the_ID(), $taxonomyType);
		}
		$image_url = '';
		if (has_post_thumbnail()) {
			$thumbnail_id = get_post_thumbnail_id();
			$image_data = wp_get_attachment_image_src($thumbnail_id, 'full');
			$image_url = $image_data[0];
			$src_set = wp_get_attachment_image_srcset($thumbnail_id, 'full');
		} else {
			$image_url = Flo_Guten_Blocks_Helper::get_default_post_image();
			$image_data[1] = 1160;
			$image_data[2] = 760;
			$src_set = '';
		} ?>
				<div class="flo-block-post-cards__item">
					<div class='flo-block-post-cards__image'>
						<a href="<?php the_permalink(); ?>">
							<img width="<?php echo $image_data[1]; ?>" height="<?php echo $image_data[2]; ?>" class="lazyload" data-src="<?php echo $image_url; ?>" data-srcset="<?php echo $src_set; ?>" sizes="(min-width: 768px) 50vw, 100vw" />
						</a>
					</div>
					<div class="flo-block-post-cards__info">
						<div class="flo-block-post-cards__meta">
							<?php if ($displayPostTaxonomy && $categoryObject) { ?>
								<div class="flo-block-post-cards__taxonomy" data-font="<?php echo $postTaxonomyTypography['type'] == 'theme'
																																					? $postTaxonomyTypography['theme']
																																					: ''; ?>">
									<a href="<?php echo get_term_link(
																																						$categoryObject
																																					); ?>"><?php echo $categoryObject->name; ?></a>
								</div>
							<?php } ?>

							<?php if ($displayPostDate) { ?>
								<div class="flo-block-post-cards__date" data-font="<?php echo $postDateTypography['type'] == 'theme'
																																			? $postDateTypography['theme']
																																			: ''; ?>"><?php echo get_the_date(); ?></div>
							<?php } ?>
						</div>
						<<?php echo !empty($titleTagName)
								? $titleTagName
								: 'h3'; ?> class="flo-block-post-cards__title" data-font="<?php echo $postTitleTypography['type'] == 'theme'
																																		? $postTitleTypography['theme']
																																		: ''; ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></<?php echo !empty($titleTagName)
																																							? $titleTagName
																																							: 'h3'; ?>>
						<div class="flo-block-post-cards__button"><a class="flo-generic-button flo-generic-button--<?php echo $buttonColorScheme; ?> in_block" href="<?php the_permalink(); ?>"><?php echo $buttonLabel; ?></a></div>
					</div>
				</div>
			<?php
	} ?>
		</div>
		<?php if ($my_query->max_num_pages > 1 && $attributes['displayPagination']) {
		echo Flo_Guten_Blocks_Helper::get_post_pagination($attributes);
	} ?>
	<?php } else {
		echo Flo_Guten_Blocks_Helper::get_not_found_message();
	} ?>
</div>
<?php $wp_query = $temp;
