<?php
/**
 * Template Name: Home Page
 */
get_header();
?>

<main id="primary">

<?php
$rental_estate_realtor_main_slider_wrap = absint( get_theme_mod( 'rental_estate_realtor_enable_slider', 0 ) );

if ( $rental_estate_realtor_main_slider_wrap === 1 ) : ?>
    <section id="main-slider-wrap">
        <?php
        $rental_estate_realtor_slider_image   = get_theme_mod( 'rental_estate_realtor_slider_image' );
        $rental_estate_realtor_slider_heading = get_theme_mod( 'rental_estate_realtor_slider_heading' );
        $rental_estate_realtor_slider_text    = get_theme_mod( 'rental_estate_realtor_slider_text' );

        if ( $rental_estate_realtor_slider_image ) : ?>
            <div class="main-slider-inner-box">
                <img src="<?php echo esc_url( $rental_estate_realtor_slider_image ); ?>"
                     alt="<?php echo esc_attr( $rental_estate_realtor_slider_heading ? $rental_estate_realtor_slider_heading : __( 'Slider Image', 'rental-estate-realtor' ) ); ?>">

                <div class="main-slider-content-box">
                    <div class="main-inner-text">
                        <?php if ( $rental_estate_realtor_slider_heading ) : ?>
                            <h1><?php echo esc_html( $rental_estate_realtor_slider_heading ); ?></h1>
                        <?php endif; ?>

                        <?php if ( $rental_estate_realtor_slider_text ) : ?>
                            <p class="slider-content"><?php echo esc_html( $rental_estate_realtor_slider_text ); ?></p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        <?php
        $rental_estate_realtor_search_form = absint( get_theme_mod( 'rental_estate_realtor_search_form', 0 ) );

        if ( $rental_estate_realtor_search_form && shortcode_exists( 'ere_property_advanced_search' ) ) : ?>
            <div class="slider-over-search" aria-labelledby="searchform-heading">
                <?php
                // Output Essential Real Estate search form.
                echo do_shortcode(
                    '[ere_property_advanced_search 
                        city_enable="false"
                        type_enable="true"
                        price_enable="true"
                        status_enable="false"
                        title_enable="false"
                        address_enable="true"
                        country_enable="false"
                        state_enable="false"
                        neighborhood_enable="false"
                        bedrooms_enable="false"
                        bathrooms_enable="false"
                        area_enable="false"
                        land_area_enable="false"
                        label_enable="false"
                        garage_enable="false"
                        property_identity_enable="false"
                        other_features_enable="false"
                        layout="vertical"
                        column="1"
                        color_scheme="color-dark"
                    ]'
                );
                ?>
                <div class="popular-search">
                    <h3 id="popular-search-title"><?php esc_html_e( 'Popular Searches', 'rental-estate-realtor' ); ?></h3>
                    <?php
                    $property_types = get_terms(
                        array(
                            'taxonomy'   => 'property-type',
                            'hide_empty' => true,
                        )
                    );

                    if ( ! empty( $property_types ) && ! is_wp_error( $property_types ) ) {
                        $links = array();

                        foreach ( $property_types as $type ) {
                            $term_link = get_term_link( $type );
                            if ( ! is_wp_error( $term_link ) ) {
                                $links[] = '<a href="' . esc_url( $term_link ) . '">' . esc_html( $type->name ) . '</a>';
                            }
                        }

                        echo wp_kses_post( implode( '<span class="comma">, </span>', $links ) );
                    } else {
                        echo esc_html__( 'No property types found', 'rental-estate-realtor' );
                    }
                    ?>
                </div>
            </div>
        <?php endif; ?>
    </section>
<?php endif; ?>

<?php 
$rental_estate_realtor_main_expert_wrap = absint(get_theme_mod('rental_estate_realtor_enable_event', 0));
if ($rental_estate_realtor_main_expert_wrap == 1): ?>
    <section id="category-section">
        <div class="container">
            <div class="main-top-text">
                <div class="heading-expert-wrap">
                    <?php if ($rental_estate_realtor_small_title = get_theme_mod('rental_estate_realtor_small_title')): ?>
                        <p class="post-title"><?php echo esc_html($rental_estate_realtor_small_title); ?></p>
                    <?php endif; ?>
                    <?php if ($rental_estate_realtor_event_text = get_theme_mod('rental_estate_realtor_event_text')): ?>
                        <h2><?php echo esc_html($rental_estate_realtor_event_text); ?></h2>
                    <?php endif; ?>
                </div>

                <?php
                // Get all terms from the 'property-type' taxonomy
                $rental_estate_realtor_terms = get_terms(array(
                    'taxonomy'   => 'property-type',
                    'hide_empty' => true,
                ));

                // Prepare tabs array
                $rental_estate_realtor_tab_filters = array();

                // Add "All" tab manually
                $rental_estate_realtor_tab_filters['all'] = array(
                    'label'  => esc_html__('All', 'rental-estate-realtor'),
                    'filter' => '*'
                );

                // Add each dynamic term as a tab
                if (!empty($rental_estate_realtor_terms) && !is_wp_error($rental_estate_realtor_terms)) {
                    foreach ($rental_estate_realtor_terms as $term) {
                        $rental_estate_realtor_tab_filters[$term->slug] = array(
                            'label'  => $term->name,
                            'filter' => '.property-type-' . $term->term_id
                        );
                    }
                }
                ?>

                <!-- Tabs -->
                <div class="property-tabs">
                    <ul class="property-tabs__list" role="tablist" aria-label="<?php esc_attr_e('Property type filters', 'rental-estate-realtor'); ?>">
                        <?php foreach ($rental_estate_realtor_tab_filters as $slug => $conf) : ?>
                            <li class="property-tabs__item" role="tab" tabindex="0"
                                data-filter="<?php echo esc_attr($conf['filter']); ?>">
                                <?php echo esc_html($conf['label']); ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>

            <?php if (post_type_exists('property') && defined('ERE_METABOX_PREFIX')) : ?>
                <div id="property-list">
                    <div class="category-listing-row">
                        <?php 
                        $rental_estate_realtor_args = array(
                            'post_type'           => 'property',
                            'ignore_sticky_posts' => true,
                            'posts_per_page'      => 8,
                            'orderby'             => 'date',
                            'order'               => 'DESC',
                            'post_status'         => 'publish',
                        );

                        $rental_estate_realtor_latestloop = new WP_Query($rental_estate_realtor_args);
                        if ($rental_estate_realtor_latestloop->have_posts()) :  
                            while ($rental_estate_realtor_latestloop->have_posts()) : 
                                $rental_estate_realtor_latestloop->the_post();

                                $rental_estate_realtor_property_id = get_the_ID();
                                $rental_estate_realtor_property_type_terms = get_the_terms($rental_estate_realtor_property_id, 'property-type');
                                $rental_estate_realtor_property_type = $rental_estate_realtor_property_type_terms && !is_wp_error($rental_estate_realtor_property_type_terms)
                                    ? implode(', ', wp_list_pluck($rental_estate_realtor_property_type_terms, 'name'))
                                    : __('Not specified', 'rental-estate-realtor');

                                $rental_estate_realtor_property_type_classes = '';
                                if ($rental_estate_realtor_property_type_terms && !is_wp_error($rental_estate_realtor_property_type_terms)) {
                                    foreach ($rental_estate_realtor_property_type_terms as $rental_estate_realtor_term) {
                                        $rental_estate_realtor_property_type_classes .= ' property-type-' . $rental_estate_realtor_term->term_id;
                                    }
                                }

                                $rental_estate_realtor_bedroom  = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_bedrooms', true);
                                $rental_estate_realtor_bathroom = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_bathrooms', true);
                                $rental_estate_realtor_size     = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_size', true);
                                $rental_estate_realtor_property_location = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_address', true);
                                $rental_estate_realtor_price_prefix = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_price_prefix', true);
                                $rental_estate_realtor_price_unit   = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_price_unit', true);
                                $rental_estate_realtor_price_short  = get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_price_short', true);
                                $rental_estate_realtor_price_postfix= get_post_meta($rental_estate_realtor_property_id, ERE_METABOX_PREFIX . 'property_price_postfix', true);
                        ?>          

                        <div class="property-item<?php echo esc_attr($rental_estate_realtor_property_type_classes); ?>">
                            <div class="property-content">
                                <div class="img-box">
                                    <?php if (has_post_thumbnail()) : ?>
                                        <?php the_post_thumbnail('rental-estate-realtor-slider'); ?>
                                    <?php else : ?>
                                        <div class="img-blank"></div>
                                    <?php endif; ?>
                                </div>
                                <div class="post-main-detail">
                                    <div class="type-price">
                                        <div class="property-type">
                                            <p><?php echo esc_html($rental_estate_realtor_property_type); ?></p>
                                        </div>
                                        <p class="property-price">
                                            <?php echo esc_html($rental_estate_realtor_price_prefix) . ' ' . esc_html(ere_get_format_money($rental_estate_realtor_price_short, $rental_estate_realtor_price_unit)) . ' ' . esc_html($rental_estate_realtor_price_postfix); ?>
                                        </p>
                                    </div>
                                    <div class="property-text">
                                        <h3 class="property-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                        <div class="property-meta">
                                            <?php if (!empty($rental_estate_realtor_bedroom)) : ?>
                                                <span><i class="fas fa-bed"></i> <?php echo esc_html($rental_estate_realtor_bedroom); ?> <?php esc_html_e('Bed', 'rental-estate-realtor'); ?></span>
                                            <?php endif; ?>
                                            <?php if (!empty($rental_estate_realtor_bathroom)) : ?>
                                                <span><i class="fas fa-bath"></i> <?php echo esc_html($rental_estate_realtor_bathroom); ?> <?php esc_html_e('Bath', 'rental-estate-realtor'); ?></span>
                                            <?php endif; ?>
                                            <?php if (!empty($rental_estate_realtor_size)) : ?>
                                                <span><i class="fas fa-retweet"></i><?php echo esc_html($rental_estate_realtor_size); ?> <?php echo esc_html(ere_get_measurement_units_land_area()); ?></span>
                                            <?php endif; ?>
                                        </div>
                                        <hr>
                                        <div class="bottom-detail">
                                            <?php if (!empty($rental_estate_realtor_property_location)) : ?>
                                                <p class="property-address">
                                                    <i class="fas fa-map-marker-alt"></i> <?php echo esc_html($rental_estate_realtor_property_location); ?>
                                                </p>
                                            <?php endif; ?>

                                            <p class="property-footer">
                                                <a href="<?php the_permalink(); ?>" class="view-btn">
                                                    <i class="fas fa-arrow-right"></i>
                                                </a>
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php 
                            endwhile;
                            wp_reset_postdata();
                        endif; 
                        ?>
                    </div>
                </div>
            <?php endif; ?>              
        </div>
    </section>
<?php endif; ?>

</main>
<?php
get_footer();
?>