<?php

$id = "bw-$block[id]";
if(!empty($block['anchor'])) {
  $id = $block['anchor'];
}

$rows = get_field('rows');
$rows = empty($rows) ? 40 : intval($rows);

$columns = get_field('columns');
$columns = empty($columns) ? 1 : intval($columns);

$program_exclude = get_field('program_exclude');
if (!is_array($program_exclude)) $program_exclude = array();

$posts_per_page = $rows * $columns;


$selected_college_of_record = isset($_GET['college_of_record']) ? $_GET['college_of_record'] : false;
$selected_content_block = isset($_GET['content_block']) ? $_GET['content_block'] : false;
 
if (function_exists('resource_schedule')) {
	$meta = false;
	$tax = array(
		'program' => array(
			'field' => 'term_id',
			'compare' => 'NOT IN',
			'terms' => $program_exclude
		)
	);
	if ($selected_college_of_record) {
		$tax['college_of_record'] = array(
			'terms' => $selected_college_of_record
		);
	}
	if ($selected_content_block) {
		$meta = array(
			'content_block' => array(
				'value' => "\"$selected_content_block\"",
				'compare' => 'LIKE'
			)
		);
	}

	$obj = resource_schedule($meta, $tax, $posts_per_page, 1);

	if (is_object($obj)) {
		$obj->tax = $tax;
		$obj->vc = valid_courses($program_exclude);
		wp_localize_script('bw_course_schedule_script', "$block[id]", (array) $obj);
	}
}

ob_start();
?>
<div class="bw-course-schedule-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-course-schedule-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-course-schedule layout-course-schedule"
		data-limit="<?php echo $posts_per_page; ?>"
		data-post-type="<?php echo $post_type; ?>"
	>
		
		<div class="bw-course-schedule-slide">
			<div class="bw-filters">
				<div>
					<?php
						$valid_option_ids = function_exists('valid_courses') ? valid_courses($program_exclude) : array();
						$args = array(
							'posts_per_page' => -1,
							'post_type' => 'course',
							'orderby' => 'title',
							'order' => 'ASC'
						);
						$query = new WP_Query($args);
					?>
					<label for="content_block-select">Filter by Name:</label>
					<select name="content_block-select" class="bw-filter" data-filter-meta-compare="LIKE" data-filter-meta="content_block">
						<option value=""> - Any - </option>
						<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
							if (!isset($valid_option_ids[get_the_ID()])) continue;
							?>
							<option <?php
								if ($selected_content_block && $selected_content_block == get_the_ID()) echo 'selected';
							?> value='"<?php the_ID(); ?>"'><?php the_title(); ?></option>
						<?php endwhile; wp_reset_postdata(); endif; ?>
					</select>
				</div>
				<div>
					<?php $terms = get_terms(array('taxonomy' => 'college_of_record', 'hide_empty' => false)); ?>
					<label for="college_of_record-select">Filter by College of Record:</label>
					<select name="college_of_record-select" class="bw-filter" data-filter-tax="college_of_record">
						<option value=""> - Any - </option>
						<?php foreach ($terms as $term) : ?>
							<option <?php
								if ($selected_college_of_record && $selected_college_of_record == $term->slug) echo 'selected';
							?> value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			</div>
			<div class="bw-course-schedule-wrap">


				<div class="bw-post">
					<div class="bw-staff-name">
						<a href="#" data-set-post="link">
							<span data-set-post="title"></span>
						</a>
						&nbsp;
						<span data-set-tax="tribe_events_cat"></span>
					</div>
					<div class="bw-staff-dates">
						<span data-set-meta="_EventStartDate"></span>
						&nbsp;-&nbsp;
						<span data-set-meta="_EventEndDate"></span>
					</div>
					<div class="bw-staff-city">
						<span data-set-meta="city"></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>
	</div>
</div>

<?php
echo ob_get_clean();
