<?php

$id = "bw-$block[id]";
if(!empty($block['anchor'])) {
  $id = $block['anchor'];
}

$post_type = get_field('post_type');
if (empty($post_type)) $post_type = 'post';

$layout = get_field('layout');
if (empty($layout)) $layout = false;

$rows = get_field('rows');
$rows = empty($rows) ? 4 : intval($rows);

$columns = get_field('columns');
$columns = empty($columns) ? 3 : intval($columns);

$posts_per_page = $rows * $columns;

if ($post_type === 'employee') {
	$order_by = array(
		array(
			'is_meta' => true,
			'key' => 'last_name',
			'order' => 'ASC'
		),
		array(
			'is_meta' => true,
			'key' => 'first_name',
			'order' => 'ASC'
		)
	);
}
if (function_exists('post_grid')) {
	$obj = post_grid($post_type, false, false, $posts_per_page, 1, $order_by);
	if (is_object($obj)) {
		wp_localize_script('bw_post_grid_script', "$block[id]", (array) $obj);
	}
}

ob_start();
?>
<div class="bw-post-grid-block">
	<style>
		/* Parameterized Styles */
		#<?php echo $id; ?> {
			--col-width: calc(100% * 1 / <?php echo $columns; ?>);
			--image-ar: calc(100% * 232 / 315);
		}
		#<?php echo $id; ?> .bw-post-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-post-grid layout-<?php echo $layout; ?>"
		data-limit="<?php echo $posts_per_page; ?>"
		data-post-type="<?php echo $post_type; ?>"
	>
		<?php switch ($layout):
			case 'staff': ?>
				<div class="bw-post-grid-slide">
					<div class="bw-filters">
						<div>
							<?php $terms = get_terms(array('taxonomy' => 'site')); ?>
							<label for="site-select">Site:</label>
							<select name="site-select" class="bw-filter" data-filter-tax="site">
								<option value=""> - Any - </option>
								<?php foreach ($terms as $term) : ?>
									<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
								<?php endforeach; ?>
							</select>
						</div>
						<div>
							<?php $terms = get_terms(array('taxonomy' => 'department')); ?>
							<label for="department-select">Department:</label>
							<select name="department-select" class="bw-filter" data-filter-tax="department">
								<option value=""> - Any - </option>
								<?php foreach ($terms as $term) : ?>
									<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
								<?php endforeach; ?>
							</select>
						</div>
					</div>
					<div class="bw-post-grid-wrap">


						<div class="bw-post">
							<div class="bw-staff-name" data-set-post="linkIfContent">
								<span data-set-meta="last_name"></span>, <span data-set-meta="first_name"></span>
							</div>
							<div class="bw-staff-title">
								<span data-set-meta="title"></span>
							</div>
							<div class="bw-staff-phone">
								<span data-set-meta="work_phone"></span>
							</div>
							<div class="bw-staff-email">
								<span data-set-meta-email="email"></span>
							</div>
						</div>


					</div>
					<div class="bw-post-view-more-container">
						<button class="bw-view-more">Show More</button>
					</div>
					<div class="bw-post-count-total">
						(<span class="bw-count"></span> of <span class="bw-total"></span>)
					</div>
				</div>
				<?php break; ?>
			<?php default: ?>
				Missing Layout
		<?php endswitch;?>
	</div>
</div>


<?php
echo ob_get_clean();
