<?php
if (!defined('ERE_METABOX_PREFIX')) {
    define('ERE_METABOX_PREFIX', '');
}

// -------------------------
// Custom Search Form 
// -------------------------
function property_dealer_render_search_form() {
    ob_start(); 

    $property_dealer_filter_location = isset($_GET['filter-location']) ? sanitize_text_field($_GET['filter-location']) : '';
    ?>

    <form method="get" action="<?php echo esc_url(get_post_type_archive_link('property')); ?>" class="ere-search-form">
        
        <input type="text" name="address" class="search-input"
           placeholder="<?php echo esc_attr__('Search by Location', 'property-dealer'); ?>"
           value="<?php echo isset($_GET['filter-location']) ? esc_attr($_GET['filter-location']) : ''; ?>" />

        <select class="search-select property-type" readonly>
            <option value=""><?php echo esc_html__('Property Type', 'property-dealer'); ?></option>
            <?php 
            $property_dealer_property_types = get_terms(array(
                'taxonomy'   => 'property-type',
                'hide_empty' => false,
            ));
            if (!empty($property_dealer_property_types) && !is_wp_error($property_dealer_property_types)) {
                foreach ($property_dealer_property_types as $property_dealer_type) {
                    echo '<option>' . esc_html($property_dealer_type->name) . '</option>';
                }
            }
            ?>
        </select>

        <select class="search-select" readonly>
            <option value=""><?php echo esc_html__('Sell or Rent', 'property-dealer'); ?></option>
            <option value="sell"><?php echo esc_html__('Sell', 'property-dealer'); ?></option>
            <option value="rent"><?php echo esc_html__('Rent', 'property-dealer'); ?></option>
        </select>

        <button type="submit" class="search-btn">
            <i class="fas fa-search"></i> <?php echo esc_html__('Search', 'property-dealer'); ?>
        </button>

    </form>

    <?php
    return ob_get_clean();
}

// -------------------------
// Property Cards 
// -------------------------
function property_dealer_render_property_cards() {
    $property_dealer_args = array(
        'post_type'      => 'property',
        'posts_per_page' => -1,
        'orderby'        => 'date',
        'order'          => 'DESC',
        'post_status'    => 'publish',
    );

    // Filter by location
    if (!empty($_GET['filter-location'])) {
        $property_dealer_location = sanitize_text_field($_GET['filter-location']);
        $property_dealer_args['meta_query'] = array(
            array(
                'key'     => ERE_METABOX_PREFIX . 'property_address',
                'value'   => $property_dealer_location,
                'compare' => 'LIKE',
            ),
        );
    }

    $property_dealer_query = new WP_Query($property_dealer_args);

    ob_start();
    ?>

    <div class="property-cards-wrapper">
        <?php if ($property_dealer_query->have_posts()) : ?>
            <div id="property-list" class="property-carousel owl-carousel">
                <?php while ($property_dealer_query->have_posts()) : $property_dealer_query->the_post(); 
                    $property_dealer_property_id   = get_the_ID();
                    $property_dealer_bedrooms      = get_post_meta($property_dealer_property_id, ERE_METABOX_PREFIX . 'property_bedrooms', true);
                    $property_dealer_bathrooms     = get_post_meta($property_dealer_property_id, ERE_METABOX_PREFIX . 'property_bathrooms', true);
                    $property_dealer_garage        = get_post_meta($property_dealer_property_id, ERE_METABOX_PREFIX . 'property_garage', true);
                    $property_dealer_area          = get_post_meta($property_dealer_property_id, ERE_METABOX_PREFIX . 'property_size', true);
                    $property_dealer_address       = get_post_meta($property_dealer_property_id, ERE_METABOX_PREFIX . 'property_address', true);
                    $property_dealer_is_featured   = get_post_meta($property_dealer_property_id, ERE_METABOX_PREFIX . 'property_featured', true);
                    $property_dealer_property_desc = get_the_content();
                ?>
                    <div class="property-card wow zoomIn" data-wow-delay="0.5s" data-wow-duration="2s">
                        <div class="property-content">

                            <?php if ($property_dealer_is_featured) : ?>
                                <div class="badge"><?php echo esc_html__('Featured', 'property-dealer'); ?></div>
                            <?php endif; ?>

                            <?php if (has_post_thumbnail()) : ?>
                                <div class="property-thumb">
                                    <?php the_post_thumbnail('medium', [
                                        'class' => 'img-fluid', 
                                        'alt'   => get_the_title()
                                    ]); ?>
                                </div>
                            <?php endif; ?>

                            <h3 class="property-title">
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>

                            <?php if ($property_dealer_address) : ?>
                                <p class="property-address"><?php echo esc_html($property_dealer_address); ?></p>
                            <?php endif; ?>

                            <div class="property-meta">
                                <div class="property-meta-field">
                                    <div>
                                        <?php if ($property_dealer_bedrooms) : ?><span><i class="fas fa-bed"></i> <?php echo esc_html($property_dealer_bedrooms); ?></span><?php endif; ?>
                                        <?php if ($property_dealer_bathrooms) : ?><span><i class="fas fa-faucet"></i> <?php echo esc_html($property_dealer_bathrooms); ?></span><?php endif; ?>
                                        <?php if ($property_dealer_garage) : ?><span><i class="fas fa-car"></i> <?php echo esc_html($property_dealer_garage); ?></span><?php endif; ?>
                                    </div>
                                    <div>
                                        <?php if ($property_dealer_area) : ?><span class="area"><?php echo esc_html($property_dealer_area); ?><?php echo esc_html__(' Sq Ft', 'property-dealer'); ?></span><?php endif; ?>
                                    </div>
                                </div>

                                <?php if ($property_dealer_property_desc) : ?>
                                    <div class="property-description"><?php echo wp_kses_post($property_dealer_property_desc); ?></div>
                                <?php endif; ?>

                                <div class="property-data">
                                    <span><i class="fas fa-user"></i> <?php the_author(); ?></span>
                                    <span>
                                        <i class="fas fa-paperclip"></i>     
                                        <?php 
                                            echo esc_html( human_time_diff( get_the_modified_time('U'), current_time('timestamp') ) . ' ago' ); 
                                        ?>
                                    </span>
                                </div>

                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        <?php else : ?>
        <p class="text-center">
            <?php if (!empty($property_dealer_filter_location)) : ?>
                <?php echo esc_html__('No properties found for ', 'property-dealer') . esc_html($property_dealer_filter_location); ?>
            <?php else : ?>
                <?php echo esc_html__('No properties found.', 'property-dealer'); ?>
            <?php endif; ?>
        </p>
        <?php endif; wp_reset_postdata(); ?>
    </div>

    <?php
    return ob_get_clean();
}
