<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
$address_enable = $keyword_enable = $title_enable = $city_enable = $type_enable = $status_enable = $rooms_enable = $bedrooms_enable = $bathrooms_enable = $price_enable = $price_is_slider = $size_enable = $size_is_slider = $land_size_enable = $land_size_is_slider = $country_enable = $state_enable = $neighborhood_enable = $label_enable = $garage_enable = $garage_size_enable = $garage_size_is_slider = $property_identity_enable = $features_enable = $color_scheme = '';

$attrs = array(
	'layout'                   => "tab",
	'column'                   => 3,
	'color_scheme'             => "color-dark",
	'status_enable'            => tfre_get_show_hide_field( 'property-status', 'advanced_search_fields' ),
	'type_enable'              => tfre_get_show_hide_field( 'property-type', 'advanced_search_fields' ),
	'keyword_enable'           => tfre_get_show_hide_field( 'keyword', 'advanced_search_fields' ),
	'title_enable'             => tfre_get_show_hide_field( 'property-title', 'advanced_search_fields' ),
	'label_enable'             => tfre_get_show_hide_field( 'property-label', 'advanced_search_fields' ),
	'address_enable'           => tfre_get_show_hide_field( 'property-address', 'advanced_search_fields' ),
	'country_enable'           => tfre_get_show_hide_field( 'property-country', 'advanced_search_fields' ),
	'state_enable'             => tfre_get_show_hide_field( 'province-state', 'advanced_search_fields' ),
	'neighborhood_enable'      => tfre_get_show_hide_field( 'property-neighborhood', 'advanced_search_fields' ),
	'rooms_enable'             => tfre_get_show_hide_field( 'property-rooms', 'advanced_search_fields' ),
	'bedrooms_enable'          => tfre_get_show_hide_field( 'property-bedrooms', 'advanced_search_fields' ),
	'bathrooms_enable'         => tfre_get_show_hide_field( 'property-bathrooms', 'advanced_search_fields' ),
	'price_enable'             => tfre_get_show_hide_field( 'property-price', 'advanced_search_fields' ),
	'price_is_slider'          => ( tfre_get_option( 'price_search_field_type', '' ) == 'slider' ),
	'size_enable'              => tfre_get_show_hide_field( 'property-size', 'advanced_search_fields' ),
	'size_is_slider'           => ( tfre_get_option( 'size_search_field_type', '' ) == 'slider' ),
	'land_size_enable'         => tfre_get_show_hide_field( 'property-land-size', 'advanced_search_fields' ),
	'land_size_is_slider'      => ( tfre_get_option( 'land_size_search_field_type', '' ) == 'slider' ),
	'garage_enable'            => tfre_get_show_hide_field( 'property-garage', 'advanced_search_fields' ),
	'garage_size_enable'       => tfre_get_show_hide_field( 'property-garage-size', 'advanced_search_fields' ),
	'garage_size_is_slider'    => ( tfre_get_option( 'garage_size_search_field_type', '' ) == 'slider' ),
	'property_identity_enable' => 'true',
	'features_enable'          => tfre_get_show_hide_field( 'property-feature', 'advanced_search_fields' ),
);

extract(
	shortcode_atts(
		array(
			'layout'                   => 'tab',
			'column'                   => '3',
			'color_scheme'             => 'color-light',
			'status_enable'            => 'true',
			'type_enable'              => 'true',
			'keyword_enable'           => 'true',
			'title_enable'             => 'true',
			'address_enable'           => 'true',
			'country_enable'           => '',
			'state_enable'             => '',
			'neighborhood_enable'      => '',
			'rooms_enable'             => '',
			'bedrooms_enable'          => '',
			'bathrooms_enable'         => '',
			'price_enable'             => 'true',
			'price_is_slider'          => '',
			'size_enable'              => '',
			'size_is_slider'           => '',
			'land_size_enable'         => '',
			'land_size_is_slider'      => '',
			'label_enable'             => '',
			'garage_enable'            => '',
			'garage_size_enable'       => '',
			'garage_size_is_slider'    => '',
			'property_identity_enable' => '',
			'features_enable'          => '',
		),
		$attrs
	)
);
$order_by               = isset( $_GET['orderBy'] ) ? sanitize_text_field( wp_unslash( $_GET['orderBy'] ) ) : '';
$status_default         = '';
$value_status           = isset( $_GET['status'] ) ? ( wp_unslash( $_GET['status'] ) ) : $status_default;
$value_keyword          = isset( $_GET['keyword'] ) ? ( wp_unslash( $_GET['keyword'] ) ) : '';
$value_title            = isset( $_GET['title'] ) ? ( wp_unslash( $_GET['title'] ) ) : '';
$value_address          = isset( $_GET['address'] ) ? ( wp_unslash( $_GET['address'] ) ) : '';
$value_type             = isset( $_GET['type'] ) ? ( wp_unslash( $_GET['type'] ) ) : '';
$value_bathrooms        = isset( $_GET['bathrooms'] ) ? ( wp_unslash( $_GET['bathrooms'] ) ) : '';
$value_rooms            = isset( $_GET['rooms'] ) ? ( wp_unslash( $_GET['rooms'] ) ) : '';
$value_bedrooms         = isset( $_GET['bedrooms'] ) ? ( wp_unslash( $_GET['bedrooms'] ) ) : '';
$value_min_price        = isset( $_GET['min-price'] ) ? ( wp_unslash( $_GET['min-price'] ) ) : '';
$value_max_price        = isset( $_GET['max-price'] ) ? ( wp_unslash( $_GET['max-price'] ) ) : '';
$value_min_size         = isset( $_GET['min-size'] ) ? ( wp_unslash( $_GET['min-size'] ) ) : '';
$value_max_size         = isset( $_GET['max-size'] ) ? ( wp_unslash( $_GET['max-size'] ) ) : '';
$value_min_land_size    = isset( $_GET['min-land-size'] ) ? ( wp_unslash( $_GET['min-land-size'] ) ) : '';
$value_max_land_size    = isset( $_GET['max-land-size'] ) ? ( wp_unslash( $_GET['max-land-size'] ) ) : '';
$value_state            = isset( $_GET['state'] ) ? ( wp_unslash( $_GET['state'] ) ) : '';
$value_country          = isset( $_GET['country'] ) ? ( wp_unslash( $_GET['country'] ) ) : '';
$value_neighborhood     = isset( $_GET['neighborhood'] ) ? ( wp_unslash( $_GET['neighborhood'] ) ) : '';
$value_label            = isset( $_GET['label'] ) ? ( wp_unslash( $_GET['label'] ) ) : '';
$value_garage           = isset( $_GET['garage'] ) ? ( wp_unslash( $_GET['garage'] ) ) : '';
$value_min_garage_size  = isset( $_GET['min-garage-size'] ) ? ( wp_unslash( $_GET['min-garage-size'] ) ) : '';
$value_max_garage_size  = isset( $_GET['max-garage-size'] ) ? ( wp_unslash( $_GET['max-garage-size'] ) ) : '';
$enable_search_features = isset( $_GET['enable-search-features'] ) ? ( wp_unslash( $_GET['enable-search-features'] ) ) : '0';
$value_features         = isset( $_GET['features'] ) ? ( wp_unslash( $_GET['features'] ) ) : '';

if ( ! empty( $value_features ) ) {
	$value_features = explode( ',', $value_features );
}

$options              = array(
	'ajax_url' => esc_url( TF_AJAX_URL ),
);
$wrapper_class        = 'tfre-property-advanced-search clearfix';
$wrapper_classes      = array(
	$wrapper_class,
	$layout,
	$color_scheme,
);
$css_class_field      = 'col-xs-12';
$css_class_half_field = 'col-xs-6';

$search_fields        = tfre_get_option( 'advanced_search_fields', array( 'keyword' => 1, 'property-title' => 1, 'property-address' => 1, 'property-status' => 1, 'property-type' => 1, 'property-label' => 1, 'property-country' => 1, 'province-state' => 1, 'property-neighborhood' => 1, 'property-rooms' => 1, 'property-bathrooms' => 1, 'property-bedrooms' => 1, 'property-garage' => 1, 'property-garage-size' => 1, 'property-price' => 1, 'property-size' => 1, 'property-land-size' => 1, 'property-feature' => 1 ) );

$search_fields_desk        = array( 'keyword' => 1, 'property-type' => 1,  'property-neighborhood' => 1, 'property-rooms' => 1, 'property-bathrooms' => 1, 'property-bedrooms' => 1, 'property-price' => 1, 'property-size' => 1 );

$search_fields_desk2        =  array( 'property-title' => 0, 'property-address' => 0, 'property-status' => 0,  'property-label' => 0, 'property-country' => 0, 'province-state' => 0, 'property-garage' => 0, 'property-garage-size' => 0,  'property-land-size' => 0, 'property-feature' => 0  );

$search_fields_desk3        = tfre_get_option( 'advanced_search_fields', array( 'keyword' => 1, 'property-title' => 0, 'property-address' => 0, 'property-status' => 0, 'property-type' => 1, 'property-label' => 0, 'property-country' => 0, 'province-state' => 0, 'property-neighborhood' => 1, 'property-rooms' => 1, 'property-bathrooms' => 1, 'property-bedrooms' => 1, 'property-garage' => 0, 'property-garage-size' => 0, 'property-price' => 1, 'property-size' => 1, 'property-land-size' => 0, 'property-feature' => 0 ) );

$enable_advanced_search_form = tfre_get_option( 'enable_advanced_search_form', 'y' );
$placeholder_keyword         = tfre_get_option( 'placeholder_keyword_field', esc_attr__( 'Search Keyword', 'tf-real-estate' ) );
?>
<?php if ( $enable_advanced_search_form == 'y' ) : ?>
	<div class="tfre-advanced-search-wrap">
		<div data-options="<?php echo esc_attr( json_encode( $options ) ); ?>"
			class="<?php echo esc_attr( join( ' ', $wrapper_classes ) ) ?>">
			<div class="form-search-wrap">
				<div class="form-search-inner">
					<?php $advanced_search_url = tfre_get_permalink( 'advanced_search_page' ); ?>
					<div data-href="<?php echo esc_url( $advanced_search_url ) ?>" class="search-properties-form">
						<div class="tf-search-form">
							<div class="tf-search-form-top desktop in-sidebar">
								<?php if ( $status_enable == 'true' && $layout == 'tab' ) : ?>
									<div class="tfre-search-status-tab <?php echo esc_attr( $css_class_field ); ?> form-group">
										<input class="search-field" type='hidden' name="status"
											value="<?php echo esc_attr( $value_status ); ?>" data-default-value="" />
										<?php
										$property_status = tfre_get_categories( 'property-status' );
										$i = 1;
										if ( $property_status ) :
											foreach ( $property_status as $status ) : ?>
												<button type="button" data-value="<?php echo esc_attr( $status->slug ) ?>"
													class="btn-status-filter <?php if ( $value_status == $status->slug || $i === 1)
														echo esc_html( " active", 'tf-real-estate' ) ?>"><?php echo esc_html( $status->name ) ?></button>
														
											<?php $i++; endforeach;
										endif;
										?>
									</div>
								<?php endif; ?>
								
								<?php 
								// $search_fields_desk2 = array_diff($search_fields, $search_fields_desk);
								
								render_search_fields( $search_fields_desk, false, $css_class_field ); ?>
							</div>
							<div class="tf-search-form-bottom desktop">
                                <div class="row">
                                    <?php render_search_fields($search_fields_desk2, false, $css_class_field); ?>
                                </div>
                            </div>
							<div class="tf-search-form-top tf-search-form-top-action desktop">
							<div class="<?php echo esc_attr($css_class_field); ?> form-group pull-right">
								<a class="tf-search-more-btn">
									<svg width="28" height="28" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
										<path d="M14.875 12.625V23.125C14.875 23.3571 14.7828 23.5796 14.6187 23.7437C14.4546 23.9078 14.2321 24 14 24C13.7679 24 13.5454 23.9078 13.3813 23.7437C13.2172 23.5796 13.125 23.3571 13.125 23.125V12.625C13.125 12.3929 13.2172 12.1704 13.3813 12.0063C13.5454 11.8422 13.7679 11.75 14 11.75C14.2321 11.75 14.4546 11.8422 14.6187 12.0063C14.7828 12.1704 14.875 12.3929 14.875 12.625ZM21.875 20.5C21.6429 20.5 21.4204 20.5922 21.2563 20.7563C21.0922 20.9204 21 21.1429 21 21.375V23.125C21 23.3571 21.0922 23.5796 21.2563 23.7437C21.4204 23.9078 21.6429 24 21.875 24C22.1071 24 22.3296 23.9078 22.4937 23.7437C22.6578 23.5796 22.75 23.3571 22.75 23.125V21.375C22.75 21.1429 22.6578 20.9204 22.4937 20.7563C22.3296 20.5922 22.1071 20.5 21.875 20.5ZM24.5 17H22.75V3.875C22.75 3.64294 22.6578 3.42038 22.4937 3.25628C22.3296 3.09219 22.1071 3 21.875 3C21.6429 3 21.4204 3.09219 21.2563 3.25628C21.0922 3.42038 21 3.64294 21 3.875V17H19.25C19.0179 17 18.7954 17.0922 18.6313 17.2563C18.4672 17.4204 18.375 17.6429 18.375 17.875C18.375 18.1071 18.4672 18.3296 18.6313 18.4937C18.7954 18.6578 19.0179 18.75 19.25 18.75H24.5C24.7321 18.75 24.9546 18.6578 25.1187 18.4937C25.2828 18.3296 25.375 18.1071 25.375 17.875C25.375 17.6429 25.2828 17.4204 25.1187 17.2563C24.9546 17.0922 24.7321 17 24.5 17ZM6.125 17C5.89294 17 5.67038 17.0922 5.50628 17.2563C5.34219 17.4204 5.25 17.6429 5.25 17.875V23.125C5.25 23.3571 5.34219 23.5796 5.50628 23.7437C5.67038 23.9078 5.89294 24 6.125 24C6.35706 24 6.57962 23.9078 6.74372 23.7437C6.90781 23.5796 7 23.3571 7 23.125V17.875C7 17.6429 6.90781 17.4204 6.74372 17.2563C6.57962 17.0922 6.35706 17 6.125 17ZM8.75 13.5H7V3.875C7 3.64294 6.90781 3.42038 6.74372 3.25628C6.57962 3.09219 6.35706 3 6.125 3C5.89294 3 5.67038 3.09219 5.50628 3.25628C5.34219 3.42038 5.25 3.64294 5.25 3.875V13.5H3.5C3.26794 13.5 3.04538 13.5922 2.88128 13.7563C2.71719 13.9204 2.625 14.1429 2.625 14.375C2.625 14.6071 2.71719 14.8296 2.88128 14.9937C3.04538 15.1578 3.26794 15.25 3.5 15.25H8.75C8.98206 15.25 9.20462 15.1578 9.36872 14.9937C9.53281 14.8296 9.625 14.6071 9.625 14.375C9.625 14.1429 9.53281 13.9204 9.36872 13.7563C9.20462 13.5922 8.98206 13.5 8.75 13.5ZM16.625 8.25H14.875V3.875C14.875 3.64294 14.7828 3.42038 14.6187 3.25628C14.4546 3.09219 14.2321 3 14 3C13.7679 3 13.5454 3.09219 13.3813 3.25628C13.2172 3.42038 13.125 3.64294 13.125 3.875V8.25H11.375C11.1429 8.25 10.9204 8.34219 10.7563 8.50628C10.5922 8.67038 10.5 8.89294 10.5 9.125C10.5 9.35706 10.5922 9.57962 10.7563 9.74372C10.9204 9.90781 11.1429 10 11.375 10H16.625C16.8571 10 17.0796 9.90781 17.2437 9.74372C17.4078 9.57962 17.5 9.35706 17.5 9.125C17.5 8.89294 17.4078 8.67038 17.2437 8.50628C17.0796 8.34219 16.8571 8.25 16.625 8.25Z" fill="currentColor"/>
									</svg>

									<span class="show"><?php esc_html_e( 'Show Avanced', 'tf-real-estate' ) ?></span>
									<span class="hide"><?php esc_html_e( 'Hide Avanced', 'tf-real-estate' ) ?></span>
								</a>
                                </div>
								<div
									class="<?php echo esc_attr( $css_class_field ); ?> form-group submit-search-form pull-right">
									<button type="button" class="tfre-advanced-search-btn">
										<?php esc_html_e( 'Find Properties', 'tf-real-estate' ) ?>
									</button>
								</div>
							</div>
							
							<div class="tf-search-form-top mobile form-inline">
								<div class="form-group input-group w-100">
									<div class="input-group-prepend">
										<button class="input-group-text tf-search-more-btn" data-toggle="collapse"
											aria-expanded="false"><i class="icon-homeya-filter2"></i></button>
									</div>
									<input class="form-control search-field" value="<?php echo esc_attr( $value_keyword ); ?>"
										data-default-value="" name="keyword" type="text"
										placeholder="<?php echo $placeholder_keyword; ?>">
									<div class="input-group-append">
										<button type="button" class="input-group-text tfre-advanced-search-btn"><i
												class="fa fa-search"></i></button>
									</div>
								</div>
							</div>
							<div class="tf-search-form-bottom mobile">
								<div class="row">
									<?php
									$search_fields_bottom = array_filter( $search_fields, function ($k) {
										return $k != 'keyword'; }, ARRAY_FILTER_USE_KEY );
									render_search_fields( $search_fields_bottom );
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
<?php endif; ?>