<?php $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?>
<?php $subtitle = ! empty( $instance['subtitle'] ) ? $instance['subtitle'] : ''; ?>
<?php $poster = ! empty( $instance['poster'] ) ? $instance['poster'] : ''; ?>
<?php $video_mp4 = ! empty( $instance['video_mp4'] ) ? $instance['video_mp4'] : ''; ?>
<?php $video_ogg = ! empty( $instance['video_ogg'] ) ? $instance['video_ogg'] : ''; ?>
<?php $input_titles = ! empty( $instance['input_titles'] ) ? $instance['input_titles'] : 'labels'; ?>

<?php echo wp_kses( $args['before_widget'], wp_kses_allowed_html( 'post' ) ); ?>

<div id="outer-wrap" class="cover" <?php if ( ! empty( $poster) ) : ?>data-background-image="<?php echo esc_attr( $poster )?>"<?php endif; ?>>
	<div id="video-wrap">
		<?php if ( ! empty( $video_mp4 ) || ! empty( $video_ogg ) ) : ?>
			<video id="cover-video" preload="metadata" autoplay muted loop>
				<?php if ( ! empty( $video_mp4 ) ) : ?>
					<source src="<?php echo esc_attr( $video_mp4 ); ?>" type="video/mp4">
				<?php endif; ?>

				<?php if ( ! empty( $video_ogg ) ) : ?>
					<source src="<?php echo esc_attr( $video_ogg ); ?>" type="video/ogg">
				<?php endif; ?>				
			</video>
		<?php endif; ?>
	</div><!-- /#video-wrap -->
</div><!-- /.cover -->

<?php if ( ! empty( $title ) || ! empty( $subtitle ) ) : ?>
	<div class="cover-title">
		<?php if ( ! empty( $title ) ) : ?>
			<h1><?php echo esc_attr( $title ); ?></h1>
		<?php endif; ?>

		<?php if ( ! empty( $subtitle ) ) :?>
			<h2><?php echo esc_attr( $subtitle ); ?></h2>
		<?php endif; ?>
	</div><!-- /.cover -->
<?php endif; ?>

<div class="cover-search-wrapper">
	<div class="cover-search">
		<div class="cover-search-inner">
			<form method="get" action="<?php echo esc_attr( Realia_Filter::get_filter_action() ); ?>">
				<div class="row">
					<div class="col-sm-3">
						<!-- LOCATION -->
						<div class="form-group">
							<?php if ( $input_titles == 'labels' ) : ?>
								<label><?php echo __( 'Location', 'realia' ); ?></label>
							<?php endif; ?>

							<select name="filter-location">
								<option value="">
									<?php if ( $input_titles == 'placeholders' ) : ?>
										<?php echo __( 'Location', 'realia' ); ?>
									<?php else: ?>
										<?php echo __( 'All locations', 'realia' ); ?>
									<?php endif; ?>
								</option>

								<?php $locations = get_terms( 'locations', array(
									'hide_empty' 	=> false,
									'parent'		=> 0
								) ); ?>			

								<?php if ( is_array( $locations ) ) : ?>				
									<?php foreach ( $locations as $location ) : ?>
										<option value="<?php echo esc_attr( $location->term_id ); ?>" <?php if ( ! empty($_GET['filter-location'] ) && $_GET['filter-location'] == $location->term_id ) : ?>selected="selected"<?php endif; ?>><?php echo esc_html( $location->name ); ?></option>

										<?php $sublocations = get_terms( 'locations', array(
											'hide_empty' => false,
											'parent' => $location->term_id,
										) ); ?>

										<?php if ( is_array( $sublocations ) ) : ?>
											<?php foreach ( $sublocations as $sublocation ) : ?>
												<option value="<?php echo esc_attr( $sublocation->term_id ); ?>" <?php if ( ! empty($_GET['filter-location'] ) && $_GET['filter-location'] == $sublocation->term_id ) : ?>selected="selected"<?php endif; ?>>
													&raquo;&nbsp; <?php echo esc_html( $sublocation->name ); ?>
												</option>							

												<?php $subsublocations = get_terms( 'locations', array(
													'hide_empty' 	=> false,
													'parent' 		=> $sublocation->term_id,
												) ); ?>

												<?php if ( is_array( $subsublocations ) ) : ?>
													<?php foreach ( $subsublocations as $subsublocation ) : ?>
														<option value="<?php echo esc_attr( $subsublocation->term_id ); ?>" <?php if ( ! empty($_GET['filter-location'] ) && $_GET['filter-location'] == $subsublocation->term_id ) : ?>selected="selected"<?php endif; ?>>
															&nbsp;&nbsp;&nbsp;&raquo;&nbsp; <?php echo esc_html( $subsublocation->name ); ?>
														</option>							
													<?php endforeach; ?>
												<?php endif; ?>
											<?php endforeach; ?>
										<?php endif; ?>
									<?php endforeach; ?>
								<?php endif; ?>
							</select>
						</div><!-- /.form-group -->
					</div><!-- /.col-* -->

					<div class="col-sm-3">
						<!-- PROPERTY TYPE -->
						<div class="form-group">
							<?php if ( $input_titles == 'labels' ) : ?>
								<label><?php echo __( 'Property type', 'realia' ); ?></label>
							<?php endif; ?>

							<select name="filter-property-type">
								<?php $property_types = get_terms( 'property_types', array( 'hide_empty' => false ) ); ?>
								<option value="">
									<?php if ( $input_titles == 'placeholders' ) : ?>
										<?php echo __( 'Property type', 'realia' ); ?>
									<?php else: ?>
										<?php echo __( 'All property types', 'realia' ); ?>
									<?php endif; ?>
								</option>

								<?php if ( is_array( $property_types ) ) : ?>
									<?php foreach ( $property_types as $property_type ) : ?>
										<option value="<?php echo esc_attr( $property_type->term_id ); ?>" <?php if ( ! empty( $_GET['filter-property-type'] ) && $_GET['filter-property-type'] == $property_type->term_id ) : ?>selected="selected"<?php endif; ?>><?php echo esc_html( $property_type->name); ?></option>
									<?php endforeach ?>
								<?php endif; ?>
							</select>
						</div><!-- /.form-group -->
					</div><!-- /.col-* -->

					<div class="col-sm-3">
						<!-- CONTRACT TYPE-->
						<div class="form-group">
							<?php if ( $input_titles == 'labels' ) : ?>
								<label><?php echo __( 'Contract', 'realia' ); ?></label>
							<?php endif; ?>

							<select name="filter-contract-type">
								<?php $contracts = get_terms( 'contracts', array( 'hide_empty' => false ) ); ?>
								
								<option value="">
									<?php if ( $input_titles == 'placeholders' ) : ?>
										<?php echo __( 'Contract', 'realia' ); ?>
									<?php else: ?>
										<?php echo __( 'All contracts', 'realia' ); ?>
									<?php endif; ?>
								</option>

								<?php if ( is_array( $contracts ) ) : ?>
									<?php foreach ( $contracts as $contract ) : ?>
										<option value="<?php echo esc_attr( $contract->term_id ); ?>" <?php if ( ! empty( $_GET['filter-contract-type'] ) &&  $_GET['filter-contract-type'] == $contract->term_id ) : ?>selected="selected"<?php endif; ?>><?php echo esc_html( $contract->name ); ?></option>
									<?php endforeach ?>
								<?php endif; ?>
							</select>
						</div><!-- /.form-group -->
					</div><!-- /.col-* -->

					<div class="col-sm-3">
						<button type="submit" class="btn btn-lg btn-block"><?php echo __( 'Filter', 'realia' ); ?></button>
					</div><!-- /.col-* -->
				</div><!-- /.row -->
			</form>		
		</div><!-- /.cover-search-inner -->
	</div><!-- /.cover-search -->
</div><!-- /.cover-search-wrapper -->

<?php echo wp_kses( $args['after_widget'], wp_kses_allowed_html( 'post' ) ); ?>