<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
get_header('ere');
/**
 * ere_before_main_content hook.
 *
 * @hooked ere_output_content_wrapper_start - 10 (outputs opening divs for the content)
 */
?>
<div class="container">
    <div class="main-wrapper">
        <div id="primary" class="site-main ct-post-wrapper lay-width">
        <?php
        do_action( 'ere_before_main_content' );
        ?>
        <?php
        global $post, $rental_estate_realtor_taxonomy_title, $rental_estate_realtor_taxonomy_name;

        $rental_estate_realtor_custom_property_layout_style = ere_get_option('archive_property_layout_style', 'property-grid');

        $rental_estate_realtor_custom_property_items_amount = ere_get_option('archive_property_items_amount', '6');
        $rental_estate_realtor_custom_property_image_size = ere_get_option( 'archive_property_image_size', '330x180' );
        $rental_estate_realtor_custom_property_columns = ere_get_option('archive_property_columns', '3');
        $rental_estate_realtor_custom_property_columns_gap = ere_get_option('archive_property_columns_gap', 'col-gap-30');
        $rental_estate_realtor_custom_property_items_md = ere_get_option('archive_property_items_md', '3');
        $rental_estate_realtor_custom_property_items_sm = ere_get_option('archive_property_items_sm', '2');
        $rental_estate_realtor_custom_property_items_xs = ere_get_option('archive_property_items_xs', '1');
        $rental_estate_realtor_custom_property_items_mb = ere_get_option('archive_property_items_mb', '1');

        $rental_estate_realtor_property_item_class = array();

        if (isset($_SESSION["property_view_as"]) && !empty($_SESSION["property_view_as"]) && in_array($_SESSION["property_view_as"], array('property-list', 'property-grid'))) {
            $rental_estate_realtor_custom_property_layout_style = $_SESSION["property_view_as"];
        }

        $rental_estate_realtor_wrapper_classes = array(
            'ere-property clearfix',
            $rental_estate_realtor_custom_property_layout_style,
            $rental_estate_realtor_custom_property_columns_gap
        );

        if ($rental_estate_realtor_custom_property_layout_style == 'property-list') {
            $rental_estate_realtor_wrapper_classes[] = 'list-1-column';
        }

        if ($rental_estate_realtor_custom_property_columns_gap == 'col-gap-30') {
            $rental_estate_realtor_property_item_class[] = 'mg-bottom-30';
        } elseif ($rental_estate_realtor_custom_property_columns_gap == 'col-gap-20') {
            $rental_estate_realtor_property_item_class[] = 'mg-bottom-20';
        } elseif ($rental_estate_realtor_custom_property_columns_gap == 'col-gap-10') {
            $rental_estate_realtor_property_item_class[] = 'mg-bottom-10';
        }

        $rental_estate_realtor_wrapper_classes[] = 'columns-' . $rental_estate_realtor_custom_property_columns;
        $rental_estate_realtor_wrapper_classes[] = 'columns-md-' . $rental_estate_realtor_custom_property_items_md;
        $rental_estate_realtor_wrapper_classes[] = 'columns-sm-' . $rental_estate_realtor_custom_property_items_sm;
        $rental_estate_realtor_wrapper_classes[] = 'columns-xs-' . $rental_estate_realtor_custom_property_items_xs;
        $rental_estate_realtor_wrapper_classes[] = 'columns-mb-' . $rental_estate_realtor_custom_property_items_mb;
        $rental_estate_realtor_property_item_class[] = 'ere-item-wrap';
        $rental_estate_realtor_args = array(
            'posts_per_page' => $rental_estate_realtor_custom_property_items_amount,
            'post_type' => 'property',
            'orderby'   => array(
                'menu_order'=>'ASC',
                'date' =>'DESC',
            ),
            'offset' => (max(1, get_query_var('paged')) - 1) * $rental_estate_realtor_custom_property_items_amount,
            'ignore_sticky_posts' => 1,
            'post_status' => 'publish'
        );
        if (isset($_GET['sortby']) && in_array($_GET['sortby'], array('a_price', 'd_price', 'a_date', 'd_date', 'featured', 'most_viewed'))) {
            if ($_GET['sortby'] == 'a_price') {
                $rental_estate_realtor_args['orderby'] = 'meta_value_num';
                $rental_estate_realtor_args['meta_key'] = ERE_METABOX_PREFIX . 'property_price';
                $rental_estate_realtor_args['order'] = 'ASC';
            } else if ($_GET['sortby'] == 'd_price') {
                $rental_estate_realtor_args['orderby'] = 'meta_value_num';
                $rental_estate_realtor_args['meta_key'] = ERE_METABOX_PREFIX . 'property_price';
                $rental_estate_realtor_args['order'] = 'DESC';
            } else if ($_GET['sortby'] == 'featured') {
                $rental_estate_realtor_args['orderby'] = array(
                    'meta_value_num' => 'DESC',
                    'date' => 'DESC',
                );
                $rental_estate_realtor_args['meta_key'] = ERE_METABOX_PREFIX . 'property_featured';
            }
            else if ($_GET['sortby'] == 'most_viewed') {
                $rental_estate_realtor_args['orderby'] = 'meta_value_num';
                $rental_estate_realtor_args['meta_key'] = ERE_METABOX_PREFIX . 'property_views_count';
                $rental_estate_realtor_args['order'] = 'DESC';
            }
            else if ($_GET['sortby'] == 'a_date') {
                $rental_estate_realtor_args['orderby'] = 'date';
                $rental_estate_realtor_args['order'] = 'ASC';
            } else if ($_GET['sortby'] == 'd_date') {
                $rental_estate_realtor_args['orderby'] = 'date';
                $rental_estate_realtor_args['order'] = 'DESC';
            }
        }
        else{
            $rental_estate_realtor_featured_toplist = ere_get_option('featured_toplist', 1);
            if($rental_estate_realtor_featured_toplist!=0)
            {
                $rental_estate_realtor_args['orderby'] = array(
                    'menu_order'=>'ASC',
                    'meta_value_num' => 'DESC',
                    'date' => 'DESC',
                );
                $rental_estate_realtor_args['meta_key'] = ERE_METABOX_PREFIX . 'property_featured';
            }
        }
        $rental_estate_realtor_property_status=ere_get_property_status_search();
        $rental_estate_realtor_property_status_arr = array();
        if ($rental_estate_realtor_property_status) {
            foreach ($rental_estate_realtor_property_status as $property_stt) {
                $rental_estate_realtor_property_status_arr[] = $property_stt->slug;
            }
        }
        $rental_estate_realtor_tax_query = array();
        if (isset($_GET['status']) && in_array($_GET['status'], $rental_estate_realtor_property_status_arr) && $rental_estate_realtor_taxonomy_name != 'property-status') {
            $rental_estate_realtor_tax_query[] = array(
                'taxonomy' => 'property-status',
                'field' => 'slug',
                'terms' => explode(',', ere_clean(wp_unslash($_GET['status']))),
                'operator' => 'IN'
            );
        }
        if (is_tax()) {
            $rental_estate_realtor_current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
            $rental_estate_realtor_taxonomy_title = $rental_estate_realtor_current_term->name;
            $rental_estate_realtor_taxonomy_name = get_query_var('taxonomy');
            if (!empty($rental_estate_realtor_taxonomy_name)) {
                $rental_estate_realtor_tax_query[] = array(
                    'taxonomy' => $rental_estate_realtor_taxonomy_name,
                    'field' => 'slug',
                    'terms' => $rental_estate_realtor_current_term->slug
                );
            }
        }

        $rental_estate_realtor_tax_count = count($rental_estate_realtor_tax_query);
        if ($rental_estate_realtor_tax_count > 0) {
            $rental_estate_realtor_args['tax_query'] = array(
                'relation' => 'AND',
                $rental_estate_realtor_tax_query
            );
        }
        $rental_estate_realtor_author_id = $rental_estate_realtor_agent_id = '';
        if (isset($_GET['user_id']) ||isset($_GET['agent_id']) ) {
            if (isset($_GET['user_id'])) {
                $rental_estate_realtor_author_id = ere_clean(wp_unslash($_GET['user_id']));
                $rental_estate_realtor_agent_id = get_the_author_meta(ERE_METABOX_PREFIX . 'author_agent_id', $rental_estate_realtor_author_id);
            }
            if (isset($_GET['agent_id'])) {
                $rental_estate_realtor_agent_id = ere_clean(wp_unslash($_GET['agent_id']));
                $rental_estate_realtor_author_id = get_post_meta($rental_estate_realtor_agent_id, ERE_METABOX_PREFIX . 'agent_user_id', true);
            }
            if (!empty($rental_estate_realtor_author_id) && $rental_estate_realtor_author_id > 0 && !empty($rental_estate_realtor_agent_id) && $rental_estate_realtor_agent_id > 0) {
                $rental_estate_realtor_args['meta_query'] = array(
                    'relation' => 'OR',
                    array(
                        'key' => ERE_METABOX_PREFIX . 'property_agent',
                        'value' => $rental_estate_realtor_agent_id,
                        'compare' => '='
                    ),
                    array(
                        'key' => ERE_METABOX_PREFIX . 'property_author',
                        'value' => $rental_estate_realtor_author_id,
                        'compare' => '='
                    )
                );
            } else {
                if (!empty($rental_estate_realtor_author_id) && $rental_estate_realtor_author_id > 0) {
                    $rental_estate_realtor_args['author'] = $rental_estate_realtor_author_id;
                } else if (!empty($rental_estate_realtor_agent_id) && $rental_estate_realtor_agent_id > 0) {
                    $rental_estate_realtor_args['meta_query'] = array(
                        array(
                            'key' => ERE_METABOX_PREFIX . 'property_agent',
                            'value' => $rental_estate_realtor_agent_id,
                            'compare' => '='
                        )
                    );
                }
            }
        }
        $rental_estate_realtor_data = new WP_Query($rental_estate_realtor_args);
        $rental_estate_realtor_total_post = $rental_estate_realtor_data->found_posts;

        $rental_estate_realtor_min_suffix = ere_get_option('enable_min_css', 0) == 1 ? '.min' : '';
        wp_print_styles( ERE_PLUGIN_PREFIX . 'property');
        wp_print_styles( ERE_PLUGIN_PREFIX . 'archive-property');

        $rental_estate_realtor_min_suffix_js = ere_get_option('enable_min_js', 0) == 1 ? '.min' : '';
        wp_enqueue_script(ERE_PLUGIN_PREFIX . 'archive-property', ERE_PLUGIN_URL . 'public/assets/js/property/ere-archive-property' . $rental_estate_realtor_min_suffix_js . '.js', array('jquery'), ERE_PLUGIN_VER, true);
        ?>
            <div class="ere-archive-property-wrap ere-property-wrap">
                <?php do_action('ere_archive_property_before_main_content'); ?>
                <div class="ere-archive-property archive-property">
                    <div class="above-archive-property">
                        <?php do_action('ere_archive_property_heading', $rental_estate_realtor_total_post, $rental_estate_realtor_taxonomy_title, $rental_estate_realtor_agent_id, $rental_estate_realtor_author_id); ?>
                        <?php do_action('ere_archive_property_action', $rental_estate_realtor_taxonomy_name); ?>
                    </div>
                    <div class="<?php echo join(' ', $rental_estate_realtor_wrapper_classes) ?>">
                        <?php if ($rental_estate_realtor_data->have_posts()) :
                            while ($rental_estate_realtor_data->have_posts()): $rental_estate_realtor_data->the_post(); ?>
                                <?php ere_get_template('content-property.php', array(
                                    'property_item_class' => $rental_estate_realtor_property_item_class,
                                    'custom_property_image_size' => $rental_estate_realtor_custom_property_image_size
                                )); ?>


                            <?php endwhile;
                        else: ?>
                            <div class="item-not-found"><?php esc_html_e('No item found', 'rental-estate-realtor'); ?></div>
                        <?php endif; ?>
                        <div class="clearfix"></div>
                        <?php
                        $rental_estate_realtor_max_num_pages = $rental_estate_realtor_data->max_num_pages;
                        ere_get_template('global/pagination.php', array('max_num_pages' => $rental_estate_realtor_max_num_pages));
                        wp_reset_postdata(); ?>
                    </div>
                </div>
                <?php do_action('ere_archive_property_after_main_content'); ?>
            </div>
        <?php
        /**
         * ere_after_main_content hook.
         *
         * @hooked ere_output_content_wrapper_end - 10 (outputs closing divs for the content)
         */
        do_action( 'ere_after_main_content' ); ?>
        </div>
    </div>
</div>
<?php get_footer('ere');