<?php
/**
 * Advanced Search Template
 *
 * @package Immowp_Gestion_Immobiliere
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $wp;
$immowp_currnet_url = home_url( $wp->request );
$immowp_pattern     = '/page(\/)*([0-9\/])*/i';
$immowp_current_url = preg_replace( $immowp_pattern, '', $immowp_currnet_url );

$immowp_localisations = get_categories( 'taxonomy=immowp_localisation&type=immowp_gestion_immo' );
$immowp_offres        = get_categories( 'taxonomy=immowp_offre&type=immowp_gestion_immo&hide_empty=false' );
$immowp_types         = get_categories( 'taxonomy=immowp_type&type=immowp_gestion_immo' );

// Initialize variables for form values (single values only)
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$immowp_offre_selected_value = filter_input( INPUT_GET, 'offre', FILTER_UNSAFE_RAW );
$immowp_offre_selected_value = ( null !== $immowp_offre_selected_value && '' !== $immowp_offre_selected_value ) ? sanitize_text_field( $immowp_offre_selected_value ) : '';

$immowp_type_selected_value = filter_input( INPUT_GET, 'type_bien', FILTER_UNSAFE_RAW );
$immowp_type_selected_value = ( null !== $immowp_type_selected_value && '' !== $immowp_type_selected_value ) ? sanitize_text_field( $immowp_type_selected_value ) : '';

$immowp_localisation_selected_value = filter_input( INPUT_GET, 'localisation', FILTER_UNSAFE_RAW );
$immowp_localisation_selected_value = ( null !== $immowp_localisation_selected_value && '' !== $immowp_localisation_selected_value ) ? sanitize_text_field( $immowp_localisation_selected_value ) : '';

$immowp_reference_raw = filter_input( INPUT_GET, 'reference', FILTER_UNSAFE_RAW );
$immowp_reference     = ( null !== $immowp_reference_raw && false !== $immowp_reference_raw ) ? sanitize_text_field( $immowp_reference_raw ) : '';

if ( ! isset( $immowp_sort ) ) {
	$immowp_sort = '';
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
?>

<div class="iwp__searchbar">
    <form id="iwp_advancedsearch_form" action="<?php echo esc_url( ! empty( $immowp_redirect ) ? $immowp_redirect : $immowp_current_url ); ?>" method="get" class="iwp__search-form">
        <input type="hidden" name="sort" value="<?php echo esc_attr( $immowp_sort ); ?>" />

        <div class="iwp__search-top-bar">
            <div class="iwp__search-main">
                <?php if ( empty( $a['offre'] ) ) : ?>
                    <div class="iwp__filter-group">
                        <label for="iwp-offre"><?php esc_html_e( "Type d'offre", 'immowp-gestion-immobiliere' ); ?></label>
                        <div class="iwp__select-wrapper">
                            <select class="iwp__input" id="iwp-offre" name="offre">
                                <option value=""><?php esc_html_e( 'Sélectionner', 'immowp-gestion-immobiliere' ); ?></option>
                                <?php foreach ( $immowp_offres as $immowp_field ) : ?>
                                    <option value="<?php echo esc_attr( $immowp_field->{'slug'} ); ?>" <?php selected( $immowp_offre_selected_value, $immowp_field->{'slug'} ); ?>><?php echo esc_html( ucfirst( $immowp_field->{'name'} ) ); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ( empty( $a['type'] ) ) : ?>
                    <div class="iwp__filter-group">
                        <label for="iwp-type"><?php esc_html_e( 'Type de bien', 'immowp-gestion-immobiliere' ); ?></label>
                        <div class="iwp__select-wrapper">
                            <select class="iwp__input" id="iwp-type" name="type_bien">
                                <option value=""><?php esc_html_e( 'Sélectionner', 'immowp-gestion-immobiliere' ); ?></option>
                                <?php foreach ( $immowp_types as $immowp_field ) : ?>
                                    <option value="<?php echo esc_attr( $immowp_field->{'slug'} ); ?>" <?php selected( $immowp_type_selected_value, $immowp_field->{'slug'} ); ?>><?php echo esc_html( ucfirst( $immowp_field->{'name'} ) ); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ( empty( $a['localisation'] ) ) : ?>
                    <div class="iwp__filter-group">
                        <label for="iwp-localisation"><?php esc_html_e( 'Localisation', 'immowp-gestion-immobiliere' ); ?></label>
                        <div class="iwp__select-wrapper">
                            <select class="iwp__input" id="iwp-localisation" name="localisation">
                                <option value=""><?php esc_html_e( 'Sélectionner', 'immowp-gestion-immobiliere' ); ?></option>
                                <?php foreach ( $immowp_localisations as $immowp_field ) : ?>
                                    <option value="<?php echo esc_attr( $immowp_field->{'slug'} ); ?>" <?php selected( $immowp_localisation_selected_value, $immowp_field->{'slug'} ); ?>><?php echo esc_html( ucfirst( $immowp_field->{'name'} ) ); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
                
                <div class="iwp__filter-group iwp__search-ref">
                    <label for="reference"><?php esc_html_e( 'Référence', 'immowp-gestion-immobiliere' ); ?></label>
                    <input type="text" class="iwp__input" id="reference" name="reference" value="<?php echo esc_attr( $immowp_reference ); ?>" placeholder="<?php esc_attr_e( 'Ex: 12345', 'immowp-gestion-immobiliere' ); ?>">
                </div>

                 <div class="iwp__filter-actions">
                    <div class="iwp__filter-group iwp__filter-actions-spacer">
                        <label>&nbsp;</label>
                        <div class="iwp__action-buttons">
                            <button type="submit" class="iwp__btn-search" name="submit">
                                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                                </svg>
                                <span><?php esc_html_e( 'Rechercher', 'immowp-gestion-immobiliere' ); ?></span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
