<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
get_header('ere'); ?>
<div class="box-image-page">
    <div class="single-page-img"></div>
     <div class="box-text">
        <?php if ( have_posts() ) : ?>
            <div class="page-header">
                <?php
                    the_archive_title( '<h2 class="page-title">', '</h2>' );
                    the_archive_description( '<div class="taxonomy-description">', '</div>' );
                ?>
            </div>
        <?php endif; ?>  
    </div> 
</div>
<div class="container-fluid">
    <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' );
    $estate_brokerage_services_agency = ere_get_option('agent_agency', '');
    $estate_brokerage_services_agent_layout_style = ere_get_option('archive_agent_layout_style', 'agent-grid');
    $estate_brokerage_services_custom_agent_image_size = ere_get_option( 'archive_agent_image_size', '270x340' );
    $estate_brokerage_services_posts_per_page = ere_get_option('archive_agent_item_amount', 12);
    $estate_brokerage_services_column_lg = ere_get_option('archive_agent_column_lg', '4');
    $estate_brokerage_services_column_md = ere_get_option('archive_agent_column_md', '3');
    $estate_brokerage_services_column_sm = ere_get_option('archive_agent_column_sm', '2');
    $estate_brokerage_services_column_xs = ere_get_option('archive_agent_column_xs', '2');
    $estate_brokerage_services_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'))) {
        $estate_brokerage_services_agent_layout_style = ere_clean(wp_unslash($_SESSION["agent_view_as"]));
    }

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

    $estate_brokerage_services_gf_item_wrap = '';

    $estate_brokerage_services_gf_item_wrap = 'ere-item-wrap';
    $estate_brokerage_services_wrapper_classes[] = 'row columns-' . $estate_brokerage_services_column_lg . ' columns-md-' . $estate_brokerage_services_column_md . ' columns-sm-' . $estate_brokerage_services_column_sm . ' columns-xs-' . $estate_brokerage_services_column_xs . ' columns-mb-' . $estate_brokerage_services_column_mb . '';

    $estate_brokerage_services_args = array(
        'posts_per_page' => $estate_brokerage_services_posts_per_page,
        'post_type' => 'agent',
        'orderby'   => array(
            'menu_order'=>'ASC',
            'date' =>'DESC',
        ),
        'offset' => (max(1, get_query_var('paged')) - 1) * $estate_brokerage_services_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') {
            $estate_brokerage_services_args['orderby'] = 'date';
            $estate_brokerage_services_args['order'] = 'ASC';
        } else if ($_GET['sortby'] == 'd_date') {
            $estate_brokerage_services_args['orderby'] = 'date';
            $estate_brokerage_services_args['order'] = 'DESC';
        }else if ($_GET['sortby'] == 'a_name') {
            $estate_brokerage_services_args['orderby'] = 'post_title';
            $estate_brokerage_services_args['order'] = 'ASC';
        }else if ($_GET['sortby'] == 'd_name') {
            $estate_brokerage_services_args['orderby'] = 'post_title';
            $estate_brokerage_services_args['order'] = 'DESC';
        }
    }
    if ($estate_brokerage_services_agency != '') {
        $estate_brokerage_services_args['tax_query'] = array(
            array(
                'taxonomy' => 'agency',
                'field' => 'term_id',
                'terms' => $estate_brokerage_services_agency,
                'operator' => 'IN'
            )
        );
    }
    $estate_brokerage_services_keyword = '';
    if (isset ($_GET['agent_name'])) {
        $estate_brokerage_services_keyword = ere_clean(wp_unslash($_GET['agent_name']));
        if (!empty($estate_brokerage_services_keyword)) {
            $estate_brokerage_services_args['s'] = $estate_brokerage_services_keyword;
        }
    }
    $estate_brokerage_services_data = new WP_Query($estate_brokerage_services_args);
    $estate_brokerage_services_total_post = $estate_brokerage_services_data->found_posts;
    $estate_brokerage_services_wrapper_classes = implode(' ', array_filter($estate_brokerage_services_wrapper_classes));

    $estate_brokerage_services_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');

    $estate_brokerage_services_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' . $estate_brokerage_services_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', $estate_brokerage_services_total_post); ?>
                    <?php do_action('ere_archive_agent_action', $estate_brokerage_services_keyword); ?>
                </div>
                <?php if ($estate_brokerage_services_data->have_posts()): ?>
                    <div class="<?php echo esc_attr($estate_brokerage_services_wrapper_classes) ?>">
                        <?php while ($estate_brokerage_services_data->have_posts()): $estate_brokerage_services_data->the_post(); ?>
                            <?php ere_get_template('content-agent.php', array(
                                'gf_item_wrap' => $estate_brokerage_services_gf_item_wrap,
                                'agent_layout_style' => $estate_brokerage_services_agent_layout_style,
                                'custom_agent_image_size'=>$estate_brokerage_services_custom_agent_image_size
                            )); ?>
                        <?php endwhile; ?>
                    </div>
                <?php else: ?>
                    <div class="item-not-found"><?php esc_html_e('No item found', 'estate-brokerage-services'); ?></div>
                    <?php
                endif; ?>
                <div class="clearfix"></div>
                <?php
                $estate_brokerage_services_max_num_pages = $estate_brokerage_services_data->max_num_pages;
                ere_get_template('global/pagination.php', array('max_num_pages' => $estate_brokerage_services_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');