<?php

$id = "bw-$block[id]";
if(!empty($block['anchor'])) {
  $id = $block['anchor'];
}

$tax = get_field('taxonomy');
if (!isset($tax)) goto end;


if ($tax === 'set') {
	$set = get_field('set_term');
	if (!isset($set)) goto end;
	$taxonomy = $set->taxonomy;
	$term = $set->slug;
} else if ($tax === 'resource_type') {
	$type = get_field('type_term');
	if (!isset($type)) goto end;
	$taxonomy = $type->taxonomy;
	$term = $type->slug;
}

$rows = get_field('rows');
if (!isset($rows)) $rows = 3;

$columns = get_field('columns');
if (!isset($columns) || intval($columns) < 1) $columns = 4;

if (function_exists('resource_grid')) {
	wp_localize_script('bw_resource_grid_script', "$block[id]", resource_grid(
		$term ? $taxonomy : false,
		$term,
		0,
		intval($rows) * intval($columns)
	));
}

ob_start();
?>
<style>
	/* Parameterized Styles */

	#<?php echo $id; ?> {
		--col-width: calc(100% * 1 / <?php echo $columns; ?> );
		--image-ar: calc(100% * 232 / 315);
	}

	#<?php echo $id; ?> .bw-resource-grid-wrap > *:first-child {
		display: none;
	}

	@media screen and (max-width: 1024px) {
		#<?php echo $id; ?> {
			--col-width: 50%;
		}
	}
	@media screen and (max-width: 767px) {
		#<?php echo $id; ?> {
			--col-width: 100%;
		}
	}

</style>
<div
	id="<?php echo $id; ?>"
	class="bw-resource-grid"
	data-taxonomy="<?php echo $taxonomy; ?>"
	data-term="<?php echo $term; ?>"
	data-limit="<?php echo intval($rows) * intval($columns) ?>"
>
	<div class="bw-resource-grid-slide">
		<div class="bw-resource-grid-wrap">

			<div class="bw-resource">
				<div class="bw-resource-header">
					<div class="bw-resource-img"><img data-set-post="image" ></div>
					<a data-set-post="link"><h1 class="entry-title" data-set-post="title"></h1></a>
				</div>
				<div class="bw-resource-content" data-set-post="excerpt"></div>
			</div>

		</div>
		<div class="bw-resource-view-more-container">
			<button class="bw-view-more">Show More</button>
		</div>
	</div>
</div>


<?php
echo ob_get_clean();

end:
