<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
get_header('ere'); ?>
<div class="container">
    <div class="main-wrapper">
        <div id="primary" class="site-main ct-post-wrapper lay-width">
    <?php
    /**
     * ere_before_main_content hook.
     *
     * @hooked ere_output_content_wrapper_start - 10 (outputs opening divs for the content)
     */
    do_action( 'ere_before_main_content' );
    $rental_estate_realtor_agency = ere_get_option('agent_agency', '');
    $rental_estate_realtor_agent_layout_style = ere_get_option('archive_agent_layout_style', 'agent-grid');
    $rental_estate_realtor_custom_agent_image_size = ere_get_option( 'archive_agent_image_size', '270x340' );
    $rental_estate_realtor_posts_per_page = ere_get_option('archive_agent_item_amount', 12);
    $rental_estate_realtor_column_lg = ere_get_option('archive_agent_column_lg', '4');
    $rental_estate_realtor_column_md = ere_get_option('archive_agent_column_md', '3');
    $rental_estate_realtor_column_sm = ere_get_option('archive_agent_column_sm', '2');
    $rental_estate_realtor_column_xs = ere_get_option('archive_agent_column_xs', '2');
    $rental_estate_realtor_column_mb = ere_get_option('archive_agent_column_mb', '1');

    if (isset($_SESSION["agent_view_as"]) && !empty($_SESSION["agent_view_as"]) && in_array($_SESSION["agent_view_as"], array('agent-list', 'agent-grid'))) {
        $rental_estate_realtor_agent_layout_style = ere_clean(wp_unslash($_SESSION["agent_view_as"]));
    }

    $rental_estate_realtor_wrapper_classes = array(
        'ere-agent clearfix',
        $rental_estate_realtor_agent_layout_style,
    );
    if ($rental_estate_realtor_agent_layout_style == 'agent-list') {
        $rental_estate_realtor_wrapper_classes[] = 'list-1-column';
    }

    $rental_estate_realtor_gf_item_wrap = '';

    $rental_estate_realtor_gf_item_wrap = 'ere-item-wrap';
    $rental_estate_realtor_wrapper_classes[] = 'row columns-' . $rental_estate_realtor_column_lg . ' columns-md-' . $rental_estate_realtor_column_md . ' columns-sm-' . $rental_estate_realtor_column_sm . ' columns-xs-' . $rental_estate_realtor_column_xs . ' columns-mb-' . $rental_estate_realtor_column_mb . '';

    $rental_estate_realtor_args = array(
        'posts_per_page' => $rental_estate_realtor_posts_per_page,
        'post_type' => 'agent',
        'orderby'   => array(
            'menu_order'=>'ASC',
            'date' =>'DESC',
        ),
        'offset' => (max(1, get_query_var('paged')) - 1) * $rental_estate_realtor_posts_per_page,
        'ignore_sticky_posts' => 1,
        'post_status' => 'publish'
    );
    if (isset($_GET['sortby']) && in_array($_GET['sortby'], array('a_date','d_date','a_name','d_name'))) {
        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 if ($_GET['sortby'] == 'a_name') {
            $rental_estate_realtor_args['orderby'] = 'post_title';
            $rental_estate_realtor_args['order'] = 'ASC';
        }else if ($_GET['sortby'] == 'd_name') {
            $rental_estate_realtor_args['orderby'] = 'post_title';
            $rental_estate_realtor_args['order'] = 'DESC';
        }
    }
    if ($rental_estate_realtor_agency != '') {
        $rental_estate_realtor_args['tax_query'] = array(
            array(
                'taxonomy' => 'agency',
                'field' => 'term_id',
                'terms' => $rental_estate_realtor_agency,
                'operator' => 'IN'
            )
        );
    }
    $rental_estate_realtor_keyword = '';
    if (isset ($_GET['agent_name'])) {
        $rental_estate_realtor_keyword = ere_clean(wp_unslash($_GET['agent_name']));
        if (!empty($rental_estate_realtor_keyword)) {
            $rental_estate_realtor_args['s'] = $rental_estate_realtor_keyword;
        }
    }
    $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_wrapper_classes = implode(' ', array_filter($rental_estate_realtor_wrapper_classes));

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

    $rental_estate_realtor_min_suffix_js = ere_get_option('enable_min_js', 0) == 1 ? '.min' : '';
    wp_enqueue_script(ERE_PLUGIN_PREFIX . 'archive-agent', ERE_PLUGIN_URL . 'public/assets/js/agent/ere-archive-agent' . $rental_estate_realtor_min_suffix_js . '.js', array('jquery'), ERE_PLUGIN_VER, true);
    ?>
        <div class="ere-archive-agent-wrap">
            <?php do_action('ere_archive_agent_before_main_content');?>
            <div class="ere-archive-agent">
                <div class="above-archive-agent mg-bottom-60 sm-mg-bottom-40">
                    <?php do_action('ere_archive_agent_heading', $rental_estate_realtor_total_post); ?>
                    <?php do_action('ere_archive_agent_action', $rental_estate_realtor_keyword); ?>
                </div>
                <?php if ($rental_estate_realtor_data->have_posts()): ?>
                    <div class="<?php echo esc_attr($rental_estate_realtor_wrapper_classes) ?>">
                        <?php while ($rental_estate_realtor_data->have_posts()): $rental_estate_realtor_data->the_post(); ?>
                            <?php ere_get_template('content-agent.php', array(
                                'gf_item_wrap' => $rental_estate_realtor_gf_item_wrap,
                                'agent_layout_style' => $rental_estate_realtor_agent_layout_style,
                                'custom_agent_image_size'=>$rental_estate_realtor_custom_agent_image_size
                            )); ?>
                        <?php endwhile; ?>
                    </div>
                <?php 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>
            <?php do_action('ere_archive_agent_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');