<?php
/**
 * Template part for displaying the services section
 *
 * @package Estate Brokerage Services
 * @subpackage estate_brokerage_services
 */

$estate_brokerage_services_main_expert_wrap = absint( get_theme_mod( 'estate_brokerage_services_enable_event', 1 ) );
$estate_brokerage_services_property_image   = get_stylesheet_directory_uri() . '/assets/images/property-img1.png';

if ( $estate_brokerage_services_main_expert_wrap == 1 ) :
    ?>
    <section id="category-section">
        <div class="main-top-text my-5">
            <div class="heading-expert-wrap mb-4">
                <?php if ( $estate_brokerage_services_event_text = get_theme_mod( 'estate_brokerage_services_event_text' ) ) : ?>
                    <h2 class="uppercase"><?php echo esc_html( $estate_brokerage_services_event_text ); ?></h2>
                <?php endif; ?>
            </div>
            <?php 
            $estate_brokerage_services_selected_type = get_theme_mod( 'estate_brokerage_services_property_type_filter', 'all' );
            if ( $estate_brokerage_services_selected_type === 'none' ) {
                echo '<p>' . esc_html__( 'Please select a property type in Customizer.', 'estate-brokerage-services' ) . '</p>';
            } else {
                $estate_brokerage_services_tax_query = array();

                if ( $estate_brokerage_services_selected_type !== 'all' ) {
                    $estate_brokerage_services_tax_query[] = array(
                        'taxonomy' => 'property-type',
                        'field'    => 'term_id',
                        'terms'    => intval( $estate_brokerage_services_selected_type )
                    );
                }
                $estate_brokerage_services_args = array(
                    'post_type'      => 'property',
                    'posts_per_page' => 8,
                    'orderby'        => 'date',
                    'order'          => 'DESC',
                    'post_status'    => 'publish'
                );
                if ( ! empty( $estate_brokerage_services_tax_query ) ) {
                    $estate_brokerage_services_args['tax_query'] = $estate_brokerage_services_tax_query;
                }
                $estate_brokerage_services_latestloop = new WP_Query( $estate_brokerage_services_args );
                if ( $estate_brokerage_services_latestloop->have_posts() ) :
                    ?>
                    <div id="property-list">
                        <div class="category-listing-row owl-carousel owl-theme" id="property-carousel">
                            <?php 
                            while ( $estate_brokerage_services_latestloop->have_posts() ) : 
                                $estate_brokerage_services_latestloop->the_post();
                                $estate_brokerage_services_property_id = get_the_ID();

                                $estate_brokerage_services_property_type_terms = get_the_terms( $estate_brokerage_services_property_id, 'property-type' );
                                $estate_brokerage_services_property_type = $estate_brokerage_services_property_type_terms && ! is_wp_error( $estate_brokerage_services_property_type_terms )
                                    ? implode( ', ', wp_list_pluck( $estate_brokerage_services_property_type_terms, 'name' ) )
                                    : __( 'Not specified', 'estate-brokerage-services' );

                                $estate_brokerage_services_property_type_classes = '';
                                if ( $estate_brokerage_services_property_type_terms && ! is_wp_error( $estate_brokerage_services_property_type_terms ) ) {
                                    foreach ( $estate_brokerage_services_property_type_terms as $estate_brokerage_services_term ) {
                                        $estate_brokerage_services_property_type_classes .= ' property-type-' . $estate_brokerage_services_term->term_id;
                                    }
                                }
                                $estate_brokerage_services_bedroom   = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_bedrooms', true );
                                $estate_brokerage_services_bathroom  = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_bathrooms', true );
                                $estate_brokerage_services_size      = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_size', true );
                                $estate_brokerage_services_property_location = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_address', true );
                                $estate_brokerage_services_price_prefix     = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_price_prefix', true );
                                $estate_brokerage_services_price_unit       = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_price_unit', true );
                                $estate_brokerage_services_price_short      = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_price_short', true );
                                $estate_brokerage_services_price_postfix    = get_post_meta( $estate_brokerage_services_property_id, ERE_METABOX_PREFIX . 'property_price_postfix', true );
                                ?>
                                <div class="property-item owl-item<?php echo esc_attr( $estate_brokerage_services_property_type_classes ); ?>">
                                    <div class="property-content">
                                        <div class="img-box">
                                            <?php if ( has_post_thumbnail() ) : ?>
                                                <img src="<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'estate-brokerage-services-slider' ) ); ?>" 
                                                     alt="<?php the_title_attribute(); ?>" />
                                            <?php else : ?>
                                                <img src="<?php echo esc_url( $estate_brokerage_services_property_image ); ?>" 
                                                     alt="<?php esc_attr_e( 'Default Property Image', 'estate-brokerage-services' ); ?>" />
                                            <?php endif; ?>
                                            <p class="property-price mb-0">
                                                <?php echo esc_html( $estate_brokerage_services_price_prefix ) . ' ' . 
                                                      esc_html( ere_get_format_money( $estate_brokerage_services_price_short, $estate_brokerage_services_price_unit ) ) . ' ' . 
                                                      esc_html( $estate_brokerage_services_price_postfix ); ?>
                                            </p>
                                            <div class="bottom-detail">
                                                <p class="property-footer my-0">
                                                    <a href="<?php the_permalink(); ?>" class="view-btn text-uppercase">
                                                        <?php esc_html_e( 'View Property', 'estate-brokerage-services' ); ?>
                                                    </a>
                                                </p>
                                            </div>
                                        </div>
                                        <div class="post-main-detail">
                                            <div class="property-text">
                                                <h3 class="property-title text-uppercase">
                                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                </h3>
                                                <?php if ( ! empty( $estate_brokerage_services_property_location ) ) : ?>
                                                    <p class="property-address text-uppercase">
                                                        <i class="fas fa-map-marker-alt"></i>
                                                        <?php echo esc_html( $estate_brokerage_services_property_location ); ?>
                                                    </p>
                                                <?php endif; ?>
                                                <hr>
                                                <div class="property-meta">
                                                    <?php if ( ! empty( $estate_brokerage_services_bedroom ) ) : ?>
                                                        <div class="room-facility">
                                                            <span><?php esc_html_e( 'Bedrooms', 'estate-brokerage-services' ); ?></span>
                                                            <span><i class="fas fa-bed"></i> <?php echo esc_html( $estate_brokerage_services_bedroom ); ?></span>
                                                        </div>
                                                    <?php endif; ?>

                                                    <?php if ( ! empty( $estate_brokerage_services_bathroom ) ) : ?>
                                                        <div class="room-facility">
                                                            <span><?php esc_html_e( 'Bathrooms', 'estate-brokerage-services' ); ?></span>
                                                            <span><i class="fas fa-bath"></i> <?php echo esc_html( $estate_brokerage_services_bathroom ); ?></span>
                                                        </div>
                                                    <?php endif; ?>

                                                    <?php if ( ! empty( $estate_brokerage_services_size ) ) : ?>
                                                        <div class="room-facility">
                                                            <span><?php esc_html_e( 'Area', 'estate-brokerage-services' ); ?></span>
                                                            <span>
                                                                <?php echo esc_html( $estate_brokerage_services_size ); ?>
                                                                <?php echo wp_kses_post( ere_get_measurement_units_land_area() ); ?>
                                                            </span>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endwhile; 
                            wp_reset_postdata(); ?>
                        </div>
                    </div>
                    <?php
                else : ?>
                    <p><?php esc_html_e( 'No properties found.', 'estate-brokerage-services' ); ?></p>
                <?php endif; 
            } ?>
        </div>
    </section>
<?php endif; ?>
