<?php
/**
 * Property Filters Template Part
 *
 * @package HomeProz
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get current filter values from URL
$current_type = isset($_GET['property_type']) ? sanitize_text_field($_GET['property_type']) : '';
$current_status = isset($_GET['property_status']) ? sanitize_text_field($_GET['property_status']) : '';
$current_location = isset($_GET['property_location']) ? sanitize_text_field($_GET['property_location']) : '';
$current_min_price = isset($_GET['min_price']) ? intval($_GET['min_price']) : '';
$current_max_price = isset($_GET['max_price']) ? intval($_GET['max_price']) : '';
$current_beds = isset($_GET['beds']) ? intval($_GET['beds']) : '';
$current_sort = isset($_GET['sort']) ? sanitize_text_field($_GET['sort']) : 'newest';

// Get taxonomy terms
$property_types = get_terms(array('taxonomy' => 'property_type', 'hide_empty' => false));
$property_statuses = get_terms(array('taxonomy' => 'property_status', 'hide_empty' => false));
$property_locations = get_terms(array('taxonomy' => 'property_location', 'hide_empty' => false));
?>

<div class="property-filters" id="property-filters" data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
    <form class="filters-form" method="get" action="<?php echo esc_url(get_post_type_archive_link('property')); ?>">
        <div class="filters-row">
            <!-- Property Type -->
            <div class="filter-group">
                <label for="filter-type" class="filter-label">Property Type</label>
                <select name="property_type" id="filter-type" class="filter-select">
                    <option value="">All Types</option>
                    <?php foreach ($property_types as $type) : ?>
                        <option value="<?php echo esc_attr($type->slug); ?>" <?php selected($current_type, $type->slug); ?>>
                            <?php echo esc_html($type->name); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Property Status -->
            <div class="filter-group">
                <label for="filter-status" class="filter-label">Status</label>
                <select name="property_status" id="filter-status" class="filter-select">
                    <option value="">All Statuses</option>
                    <?php foreach ($property_statuses as $status) : ?>
                        <option value="<?php echo esc_attr($status->slug); ?>" <?php selected($current_status, $status->slug); ?>>
                            <?php echo esc_html($status->name); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Location -->
            <div class="filter-group">
                <label for="filter-location" class="filter-label">Location</label>
                <select name="property_location" id="filter-location" class="filter-select">
                    <option value="">All Locations</option>
                    <?php foreach ($property_locations as $location) : ?>
                        <option value="<?php echo esc_attr($location->slug); ?>" <?php selected($current_location, $location->slug); ?>>
                            <?php echo esc_html($location->name); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Bedrooms -->
            <div class="filter-group">
                <label for="filter-beds" class="filter-label">Bedrooms</label>
                <select name="beds" id="filter-beds" class="filter-select">
                    <option value="">Any</option>
                    <option value="1" <?php selected($current_beds, 1); ?>>1+</option>
                    <option value="2" <?php selected($current_beds, 2); ?>>2+</option>
                    <option value="3" <?php selected($current_beds, 3); ?>>3+</option>
                    <option value="4" <?php selected($current_beds, 4); ?>>4+</option>
                    <option value="5" <?php selected($current_beds, 5); ?>>5+</option>
                </select>
            </div>

            <!-- Price Range -->
            <div class="filter-group filter-group-price">
                <label class="filter-label">Price Range</label>
                <div class="price-inputs">
                    <select name="min_price" id="filter-min-price" class="filter-select">
                        <option value="">Min Price</option>
                        <option value="50000" <?php selected($current_min_price, 50000); ?>>$50,000</option>
                        <option value="100000" <?php selected($current_min_price, 100000); ?>>$100,000</option>
                        <option value="150000" <?php selected($current_min_price, 150000); ?>>$150,000</option>
                        <option value="200000" <?php selected($current_min_price, 200000); ?>>$200,000</option>
                        <option value="250000" <?php selected($current_min_price, 250000); ?>>$250,000</option>
                        <option value="300000" <?php selected($current_min_price, 300000); ?>>$300,000</option>
                        <option value="400000" <?php selected($current_min_price, 400000); ?>>$400,000</option>
                        <option value="500000" <?php selected($current_min_price, 500000); ?>>$500,000</option>
                    </select>
                    <span class="price-separator">-</span>
                    <select name="max_price" id="filter-max-price" class="filter-select">
                        <option value="">Max Price</option>
                        <option value="100000" <?php selected($current_max_price, 100000); ?>>$100,000</option>
                        <option value="150000" <?php selected($current_max_price, 150000); ?>>$150,000</option>
                        <option value="200000" <?php selected($current_max_price, 200000); ?>>$200,000</option>
                        <option value="250000" <?php selected($current_max_price, 250000); ?>>$250,000</option>
                        <option value="300000" <?php selected($current_max_price, 300000); ?>>$300,000</option>
                        <option value="400000" <?php selected($current_max_price, 400000); ?>>$400,000</option>
                        <option value="500000" <?php selected($current_max_price, 500000); ?>>$500,000</option>
                        <option value="750000" <?php selected($current_max_price, 750000); ?>>$750,000</option>
                        <option value="1000000" <?php selected($current_max_price, 1000000); ?>>$1,000,000+</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="filters-actions">
            <!-- Sort -->
            <div class="filter-group filter-group-sort">
                <label for="filter-sort" class="filter-label">Sort By</label>
                <select name="sort" id="filter-sort" class="filter-select">
                    <option value="newest" <?php selected($current_sort, 'newest'); ?>>Newest First</option>
                    <option value="oldest" <?php selected($current_sort, 'oldest'); ?>>Oldest First</option>
                    <option value="price_low" <?php selected($current_sort, 'price_low'); ?>>Price: Low to High</option>
                    <option value="price_high" <?php selected($current_sort, 'price_high'); ?>>Price: High to Low</option>
                </select>
            </div>

            <button type="submit" class="btn btn-primary filters-submit">Search</button>
            <a href="<?php echo esc_url(get_post_type_archive_link('property')); ?>" class="btn btn-secondary filters-reset">Reset</a>
        </div>
    </form>
</div>
