<?php
/**
 * Content Archive Property Template
 * 
 * This template displays the content for the archive page for properties.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/content-archive-property.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/content-archive-property.php
 * 3. Modify the copied file to customize the archive page
 * 
 * @package     Havenlytics
 * @subpackage  Templates/
 * @since       2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $wp_query;

// Get pagination type - handles AJAX setting and shortcode context
$hvnly_pagination_type = function_exists( 'hvnly_get_pagination_type' ) ? hvnly_get_pagination_type() : 'load-more';

// Check if filter sidebar should be shown
$hvnly_show_sidebar = function_exists( 'hvnly_should_show_filter_sidebar' ) ? hvnly_should_show_filter_sidebar() : true;

// Calculate property range for display
$hvnly_total_properties = $wp_query->found_posts;
$hvnly_current_page     = max( 1, get_query_var( 'paged' ) );
$hvnly_per_page         = function_exists( 'hvnly_get_properties_per_page' ) ? hvnly_get_properties_per_page() : 12;
$hvnly_start            = ( ( $hvnly_current_page - 1 ) * $hvnly_per_page ) + 1;
$hvnly_end              = min( $hvnly_current_page * $hvnly_per_page, $hvnly_total_properties );
$hvnly_current_filters  = hvnly_get_current_filters();

// Check if preloader is enabled for each component
$hvnly_show_view_controls_preloader = function_exists( 'hvnly_is_preloader_enabled' ) && hvnly_is_preloader_enabled( 'view_controls' );
$hvnly_show_load_more_preloader     = function_exists( 'hvnly_is_preloader_enabled' ) && hvnly_is_preloader_enabled( 'load_more' );
$hvnly_show_pagination_preloader    = function_exists( 'hvnly_is_preloader_enabled' ) && hvnly_is_preloader_enabled( 'pagination' );

do_action( 'hvnly_before_archive_property' );
?>

<!-- Property Section Title -->
<?php hvnly_get_template_part( 'archive/section', 'title' ); ?>

<!-- Top Search Preloader -->
<?php
if ( function_exists( 'hvnly_is_preloader_enabled' ) && hvnly_is_preloader_enabled( 'top_search' ) ) {
    hvnly_render_preloader( 'top-search' );
}
?>

<!-- Dynamic Search Form -->
<?php hvnly_get_template_part( 'search/search', 'filters' ); ?>

<!-- Dynamic Main Grid Property list -->
<div class="hvnly-with-sidebar__contents__grids">

    <!-- Filter Sidebar -->
    <?php if ( $hvnly_show_sidebar ) : ?>
    <?php hvnly_get_template_part( 'search/filter', 'sidebar' ); ?>
    <?php endif; ?>

    <!-- Property Listings -->
    <section class="hvnly-property--grid--listings">

        <!-- View Controls Preloader -->
        <?php if ( $hvnly_show_view_controls_preloader ) : ?>
        <?php hvnly_render_preloader( 'view-controls' ); ?>
        <?php endif; ?>

        <!-- View Controls -->
        <div
            class="hvnly-property--view--controls <?php echo $hvnly_show_view_controls_preloader ? 'hvnly-original-content' : ''; ?>">
            <?php
            hvnly_get_template_part( 'search/result', 'count', array(
                'total_properties' => $hvnly_total_properties,
                'start'            => $hvnly_start,
                'end'              => $hvnly_end,
                'current_filters'  => $hvnly_current_filters,
            ) );
            ?>
            <?php hvnly_get_template_part( 'archive/view', 'controls' ); ?>
        </div>

        <!-- Dynamic Property Grid -->
        <?php hvnly_get_template_part( 'archive/property', 'loop' ); ?>

        <!-- Load More Preloader (only for load-more mode) -->
        <?php if ( $hvnly_show_load_more_preloader && $hvnly_pagination_type === 'load-more' ) : ?>
        <?php hvnly_render_preloader( 'load-more' ); ?>
        <?php endif; ?>

        <!-- Pagination Preloader (only for traditional pagination mode) -->
        <?php if ( $hvnly_show_pagination_preloader && $hvnly_pagination_type === 'traditional' ) : ?>
        <?php hvnly_render_preloader( 'pagination' ); ?>
        <?php endif; ?>

        <!-- Unified Pagination - Handles both Load More and Traditional based on settings -->
        <?php
        if ( $hvnly_pagination_type === 'traditional' ) {
            $hvnly_pagination_class = $hvnly_show_pagination_preloader ? 'hvnly-original-content' : '';
            ?>
        <div id="hvnly-property-pagination" class="<?php echo esc_attr( $hvnly_pagination_class ); ?>">
            <?php hvnly_get_template_part( 'search/pagination', null, array( 'pagination_type' => 'traditional' ) ); ?>
        </div>
        <?php
        } else {
            $hvnly_loadmore_class = $hvnly_show_load_more_preloader ? 'hvnly-original-content' : '';
            ?>
        <div id="hvnly-load-more-container" class="<?php echo esc_attr( $hvnly_loadmore_class ); ?>">
            <?php hvnly_get_template_part( 'search/ajaxload', 'more', array( 'pagination_type' => 'load-more' ) ); ?>
        </div>
        <?php
        }
        ?>

    </section>
</div>

<?php
do_action( 'hvnly_after_archive_property' );