<?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 py-5">
    <div class="row">
        <div class="col-lg-8 col-md-8 col-12">
            <?php
                do_action( 'ere_before_main_content' );
            ?>
            <?php
            global $post, $taxonomy_title, $taxonomy_name;

            $real_estate_developer_custom_property_layout_style = ere_get_option('archive_property_layout_style', 'property-grid');
            $real_estate_developer_custom_property_items_amount = ere_get_option('archive_property_items_amount', '6');
            $real_estate_developer_custom_property_image_size = ere_get_option( 'archive_property_image_size', ere_get_loop_property_image_size_default() );
            $real_estate_developer_custom_property_columns = ere_get_option('archive_property_columns', '3');
            $real_estate_developer_custom_property_columns_gap = ere_get_option('archive_property_columns_gap', 'col-gap-30');
            $real_estate_developer_custom_property_items_md = ere_get_option('archive_property_items_md', '3');
            $real_estate_developer_custom_property_items_sm = ere_get_option('archive_property_items_sm', '2');
            $real_estate_developer_custom_property_items_xs = ere_get_option('archive_property_items_xs', '1');
            $real_estate_developer_custom_property_items_mb = ere_get_option('archive_property_items_mb', '1');

            $real_estate_developer_property_item_class = array();
            ERE_Compare::open_session();

            $real_estate_developer_ss_property_view_as = isset($_SESSION["property_view_as"]) ? sanitize_text_field(wp_unslash($_SESSION["property_view_as"])) : '';

            if (in_array($real_estate_developer_ss_property_view_as, array('property-list', 'property-grid'))) {
                $real_estate_developer_custom_property_layout_style = $real_estate_developer_ss_property_view_as;
            }

            $real_estate_developer_wrapper_classes = array(
                'ere-property clearfix',
                $real_estate_developer_custom_property_layout_style,
                $real_estate_developer_custom_property_columns_gap
            );

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

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

            $real_estate_developer_wrapper_classes[] = 'columns-' . $real_estate_developer_custom_property_columns;
            $real_estate_developer_wrapper_classes[] = 'columns-md-' . $real_estate_developer_custom_property_items_md;
            $real_estate_developer_wrapper_classes[] = 'columns-sm-' . $real_estate_developer_custom_property_items_sm;
            $real_estate_developer_wrapper_classes[] = 'columns-xs-' . $real_estate_developer_custom_property_items_xs;
            $real_estate_developer_wrapper_classes[] = 'columns-mb-' . $real_estate_developer_custom_property_items_mb;
            $real_estate_developer_property_item_class[] = 'ere-item-wrap';

            $real_estate_developer__atts =  [
                'item_amount' => $real_estate_developer_custom_property_items_amount
            ];

            $real_estate_developer_query_args = [];
            $real_estate_developer_tax_query = [];

            if (is_tax()) {
                $real_estate_developer_current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
                $taxonomy_title = $real_estate_developer_current_term->name;
                $taxonomy_name = get_query_var('taxonomy');
                if (!empty($taxonomy_name)) {
                    $real_estate_developer_tax_query[] = array(
                        'taxonomy' => $taxonomy_name,
                        'field' => 'slug',
                        'terms' => $real_estate_developer_current_term->slug
                    );
                }
            }
            if (count($real_estate_developer_tax_query) > 0) {
                $real_estate_developer_query_args['tax_query'] = [
                    'relation' => 'AND',
                    $real_estate_developer_tax_query,
                ];
            }

            $real_estate_developer_author_id = $real_estate_developer_agent_id = '';
            $real_estate_developer__user_id = isset($_GET['user_id']) ? ere_clean(wp_unslash($_GET['user_id'])) : '';
            $real_estate_developer__agent_id = isset($_GET['agent_id']) ? ere_clean(wp_unslash($_GET['agent_id'])) : '';
            if (($real_estate_developer__user_id != '' ) || ($real_estate_developer__agent_id != '') ) {
                if ($real_estate_developer__user_id != '') {
                    $real_estate_developer_author_id = $real_estate_developer__user_id;
                    $real_estate_developer_agent_id = get_the_author_meta(ERE_METABOX_PREFIX . 'author_agent_id', $real_estate_developer_author_id);
                }
                if ($real_estate_developer__agent_id != '') {
                    $real_estate_developer_agent_id = $real_estate_developer__agent_id;
                    $real_estate_developer_author_id = get_post_meta($real_estate_developer_agent_id, ERE_METABOX_PREFIX . 'agent_user_id', true);
                }
            }
            $real_estate_developer__atts['author_id'] = $real_estate_developer_author_id;
            $real_estate_developer__atts['agent_id'] = $real_estate_developer_agent_id;
            $real_estate_developer_args = ere_get_property_query_args($real_estate_developer__atts,$real_estate_developer_query_args);
            $real_estate_developer_args = apply_filters('ere_property_archive_query_args',$real_estate_developer_args);
            $real_estate_developer_data = new WP_Query($real_estate_developer_args);
            $real_estate_developer_total_post = $real_estate_developer_data->found_posts;
            ?>
                <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
                            /**
                             * Hook: ere_before_archive_property.
                             *
                             * @hooked ere_template_archive_property_heading - 10
                             * @hooked ere_template_archive_property_action - 15
                             */
                            do_action('ere_before_archive_property', $real_estate_developer_total_post, $taxonomy_title, $real_estate_developer_agent_id, $real_estate_developer_author_id);
                            ?>
                        </div>
                        <div class="<?php echo esc_attr(join(' ', $real_estate_developer_wrapper_classes))?>">
                            <?php if ($real_estate_developer_data->have_posts()) :
                                while ($real_estate_developer_data->have_posts()): $real_estate_developer_data->the_post(); ?>
                                    <?php ere_get_template('content-property.php', array(
                                        'property_item_class' => $real_estate_developer_property_item_class,
                                        'custom_property_image_size' => $real_estate_developer_custom_property_image_size
                                    )); ?>
                                <?php endwhile;
                            else: ?>
                                <?php ere_get_template('loop/content-none.php'); ?>
                            <?php endif; ?>
                        </div>
                        <?php
                        $real_estate_developer_max_num_pages = $real_estate_developer_data->max_num_pages;
                        ere_get_template('global/pagination.php', array('max_num_pages' => $real_estate_developer_max_num_pages));
                        wp_reset_postdata(); ?>
                    </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 class="col-lg-4 col-md-4 col-12">
            <?php /**
             * ere_sidebar_property hook.
             *
             * @hooked ere_sidebar_property - 10
             */
            do_action('ere_sidebar_property'); ?>
        </div>
    </div>
</div>
<?php get_footer('ere');