<?php
if ( ! defined( 'ABSPATH' ) ) exit;

$regions = get_terms( array( 'taxonomy' => 'palace-property-region', 'hide_empty' => true ) );
$property_types = get_terms( array( 'taxonomy' => 'palace-property-type', 'hide_empty' => true ) );
$statuses = get_terms( array( 'taxonomy' => 'palace-property-status', 'hide_empty' => true ) );
?>

<div class="ipp-search" id="ipp-search-bar">
    <form class="ipp-search__form" id="ipp-filter-form" method="get" action="<?php echo esc_url( get_post_type_archive_link( 'palace-properties' ) ); ?>">
        <input type="hidden" name="filter" value="1" />

        <!-- Row 1: Search + Price + Beds/Baths -->
        <div class="ipp-search__row">
            <div class="ipp-search__group ipp-search__group--text">
                <label for="ipp-search-text"><?php esc_html_e( 'Search', 'integrate-palace-properties' ); ?></label>
                <input type="text" id="ipp-search-text" name="search_text" class="ipp-search__input"
                    placeholder="<?php esc_attr_e( 'Address, suburb, keyword...', 'integrate-palace-properties' ); ?>"
                    value="<?php echo isset( $_GET['search_text'] ) ? esc_attr( sanitize_text_field( wp_unslash( $_GET['search_text'] ) ) ) : ''; ?>" />
            </div>

            <div class="ipp-search__group ipp-search__group--price">
                <label><?php esc_html_e( 'Price Range', 'integrate-palace-properties' ); ?></label>
                <div class="ipp-search__price-pair">
                    <input type="number" id="ipp-price-min" name="price_min" class="ipp-search__input" placeholder="<?php esc_attr_e( 'Min', 'integrate-palace-properties' ); ?>"
                        value="<?php echo isset( $_GET['price_min'] ) ? esc_attr( absint( $_GET['price_min'] ) ) : ''; ?>" min="0" step="50" />
                    <span class="ipp-search__separator">&ndash;</span>
                    <input type="number" id="ipp-price-max" name="price_max" class="ipp-search__input" placeholder="<?php esc_attr_e( 'Max', 'integrate-palace-properties' ); ?>"
                        value="<?php echo isset( $_GET['price_max'] ) ? esc_attr( absint( $_GET['price_max'] ) ) : ''; ?>" min="0" step="50" />
                </div>
            </div>

            <div class="ipp-search__group ipp-search__group--select">
                <label for="ipp-bedrooms"><?php esc_html_e( 'Beds', 'integrate-palace-properties' ); ?></label>
                <select id="ipp-bedrooms" name="bedroom" class="ipp-search__select">
                    <option value=""><?php esc_html_e( 'Any', 'integrate-palace-properties' ); ?></option>
                    <?php for ( $i = 1; $i <= 5; $i++ ) : ?>
                        <option value="<?php echo esc_attr( $i ); ?>" <?php selected( isset( $_GET['bedroom'] ) ? absint( $_GET['bedroom'] ) : '', $i ); ?>><?php echo esc_html( $i ); ?>+</option>
                    <?php endfor; ?>
                </select>
            </div>

            <div class="ipp-search__group ipp-search__group--select">
                <label for="ipp-bathrooms"><?php esc_html_e( 'Baths', 'integrate-palace-properties' ); ?></label>
                <select id="ipp-bathrooms" name="bathroom" class="ipp-search__select">
                    <option value=""><?php esc_html_e( 'Any', 'integrate-palace-properties' ); ?></option>
                    <?php for ( $i = 1; $i <= 4; $i++ ) : ?>
                        <option value="<?php echo esc_attr( $i ); ?>" <?php selected( isset( $_GET['bathroom'] ) ? absint( $_GET['bathroom'] ) : '', $i ); ?>><?php echo esc_html( $i ); ?>+</option>
                    <?php endfor; ?>
                </select>
            </div>
        </div>

        <!-- Row 2: Filters + Sort + Actions -->
        <div class="ipp-search__row">
            <?php if ( ! empty( $regions ) && ! is_wp_error( $regions ) ) : ?>
                <div class="ipp-search__group ipp-search__group--select">
                    <label for="ipp-region"><?php esc_html_e( 'Region', 'integrate-palace-properties' ); ?></label>
                    <select id="ipp-region" name="region" class="ipp-search__select">
                        <option value=""><?php esc_html_e( 'All Regions', 'integrate-palace-properties' ); ?></option>
                        <?php foreach ( $regions as $region ) : ?>
                            <option value="<?php echo esc_attr( $region->slug ); ?>" <?php selected( isset( $_GET['region'] ) ? sanitize_text_field( $_GET['region'] ) : '', $region->slug ); ?>>
                                <?php echo esc_html( $region->name ); ?> (<?php echo esc_html( $region->count ); ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>

            <?php if ( ! empty( $property_types ) && ! is_wp_error( $property_types ) ) : ?>
                <div class="ipp-search__group ipp-search__group--select">
                    <label for="ipp-type"><?php esc_html_e( 'Type', 'integrate-palace-properties' ); ?></label>
                    <select id="ipp-type" name="property_type" class="ipp-search__select">
                        <option value=""><?php esc_html_e( 'All Types', 'integrate-palace-properties' ); ?></option>
                        <?php foreach ( $property_types as $type ) : ?>
                            <option value="<?php echo esc_attr( $type->slug ); ?>" <?php selected( isset( $_GET['property_type'] ) ? sanitize_text_field( $_GET['property_type'] ) : '', $type->slug ); ?>>
                                <?php echo esc_html( $type->name ); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>

            <?php if ( ! empty( $statuses ) && ! is_wp_error( $statuses ) ) : ?>
                <div class="ipp-search__group ipp-search__group--select">
                    <label for="ipp-status"><?php esc_html_e( 'Status', 'integrate-palace-properties' ); ?></label>
                    <select id="ipp-status" name="property_status" class="ipp-search__select">
                        <option value=""><?php esc_html_e( 'All', 'integrate-palace-properties' ); ?></option>
                        <?php foreach ( $statuses as $status ) : ?>
                            <option value="<?php echo esc_attr( $status->slug ); ?>" <?php selected( isset( $_GET['property_status'] ) ? sanitize_text_field( $_GET['property_status'] ) : '', $status->slug ); ?>>
                                <?php echo esc_html( $status->name ); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>

            <div class="ipp-search__group ipp-search__group--select">
                <label for="ipp-sort"><?php esc_html_e( 'Sort', 'integrate-palace-properties' ); ?></label>
                <select id="ipp-sort" name="sort_by" class="ipp-search__select">
                    <option value="date_desc"><?php esc_html_e( 'Newest', 'integrate-palace-properties' ); ?></option>
                    <option value="price_asc"><?php esc_html_e( 'Price ↑', 'integrate-palace-properties' ); ?></option>
                    <option value="price_desc"><?php esc_html_e( 'Price ↓', 'integrate-palace-properties' ); ?></option>
                    <option value="bedrooms_desc"><?php esc_html_e( 'Beds ↓', 'integrate-palace-properties' ); ?></option>
                </select>
            </div>

            <div class="ipp-search__group ipp-search__group--actions">
                <label>&nbsp;</label>
                <div class="ipp-search__actions">
                    <button type="submit" class="ipp-btn ipp-btn--primary ipp-btn--search">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
                        <?php esc_html_e( 'Search', 'integrate-palace-properties' ); ?>
                    </button>
                    <a href="<?php echo esc_url( get_post_type_archive_link( 'palace-properties' ) ); ?>" class="ipp-search__clear"><?php esc_html_e( 'Clear', 'integrate-palace-properties' ); ?></a>
                </div>
            </div>
        </div>
    </form>
</div>
