<?php
	$url = route('admin.user-events');
?>
<style>
	#recordsFilter label {
		font-weight: bold;
	}

	#recordsFilter .mw-100px {
		min-width: 100px;
	}

	.filter-container {
		display: flex;
		align-items: flex-start;
		flex-wrap: wrap;
	}

	.filter-container>div {
		width: 165px;
	}

	.filter-container>div:not(:last-child) {
		margin-right: 15px;
	}
</style>
<form id="recordsFilter" method="GET" action="{{ $url }}" class="filter-container align-items-end mb-3" v-cloak>
	<div class="sm-section">
		<label class="text-secondary text-sm mb-1">Name:</label>
		<input name="name" class="form-control form-control-sm min-width-unset" v-model="filters.name" />
	</div>
	<div class="sm-section">
		<div class="form-group mb-0">
			<label class="text-secondary text-sm mb-1">Date:</label>
			<select name="created_at_date_category" class="form-control form-control-sm min-width-unset" v-model="filters.created_at_date_category">
				<option value="">All</option>
				<option value="EXACTLY">Exactly</option>
				<option value="LESS_THAN">Less Than</option>
				<option value="GREATER_THAN">Greater Than</option>
				<option value="BETWEEN">Between</option>
				<option value="NOT_BETWEEN">Not Between</option>
			</select>
			<div v-show="filters.created_at_date_category" class="mt-2">
				<div>
					<input  name="created_at_date_value_1" v-model="filters.created_at_date_value_1" type="date" class="form-control form-control-sm min-width-unset" :placeholder="(filters.created_at_date_category === 'BETWEEN' || filters.created_at_date_category === 'NOT_BETWEEN') ? 'From' : 'Last Visit'" />
				</div>
				<div v-show="filters.created_at_date_category === 'BETWEEN' || filters.created_at_date_category === 'NOT_BETWEEN'" class="mt-2">
					<input name="created_at_date_value_2" v-model="filters.created_at_date_value_2" type="date" class="form-control form-control-sm min-width-unset" placeholder="To" />
				</div>
			</div>
		</div>
	</div>

	<div class="">
		<div class="form-group mb-0">
			<label class="text-secondary text-sm mb-1">&nbsp;</label>
			<div class="d-flex align-items-center">
				<button type="submit" v-on:click.prevent="doSubmit()" class="btn btn-primary btn-sm mr-2">Filter</button>
				<a href="#" v-on:click.prevent="fastLoad('{{ $url }}')" class="btn btn-link btn-sm text-danger">Clear Filters</a>
			</div>
		</div>
	</div>
</form>

<?php
$loadedFilters = $filters;
$allFilterKeys = [
	'name',
	'created_at_date_category',
	'created_at_date_value_1',
	'created_at_date_value_2',
];
for ($i = 0; $i < count($allFilterKeys); $i++) {
	if (!isset($loadedFilters[$allFilterKeys[$i]]) || !$loadedFilters[$allFilterKeys[$i]]) {
		$loadedFilters[$allFilterKeys[$i]] = '';
	}
}
?>

<script>
	$(document).ready(function(){
		new Vue({
				el: '#recordsFilter',
				delimiters: ['@{{', '}}'],
				data: {
					filters: <?= json_encode($loadedFilters) ?>
				},
				methods: {
					doSubmit: function() {
						fastLoad('{{ $url }}?' + $('#recordsFilter').serialize());
						return false;
					},
					initSelect2: function() {
						$('[select2]').select2();
					},
					init: function() {
						this.initSelect2();

					}
				},
				mounted: function() {
					this.init();
				},
			});
	});
</script>