<?php

/**
 * Layout: Grid.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$columns = (int) $attributes['columns'];

$args = array(			
    'taxonomy'     => 'aiopod_participants',
    'orderby'      => sanitize_text_field( $attributes['orderby'] ), 
    'order'        => sanitize_text_field( $attributes['order'] ),
    'hide_empty'   => false,
    'hierarchical' => false
);

if ( ! empty( $attributes['include'] ) ) { // Include participant IDs
    $args['include'] = array_map( 'intval', explode( ',', $attributes['include'] ) );
}

if ( ! empty( $attributes['exclude'] ) ) { // Exclude participant IDs
    $args['exclude'] = array_map( 'intval', explode( ',', $attributes['exclude'] ) );
}

$args  = apply_filters( 'aiopod_participants_args', $args, $attributes );
$terms = get_terms( $args );			

// Render HTML output
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) :
    $attributes['count'] = count( $terms );

    // Pagination setup
    $attributes['terms_per_page'] = (int) $attributes['limit'];

    if ( $attributes['terms_per_page'] > 0 ) {
        $offset = 0;

        if ( 'none' !== $attributes['pagination'] ) {                
            $attributes['max_num_pages'] = ceil( $attributes['count'] / $attributes['terms_per_page'] );                
            $offset = ( $attributes['paged'] - 1 ) * $attributes['terms_per_page'];
        }
        
        $terms = array_slice( $terms, $offset, $attributes['terms_per_page'] );
    } else {
        $attributes['pagination'] = 'none';
    }
    ?>
    <div class="aiopod aiopod-participants aiopod-participants--layout-grid">
        <!-- Title -->
        <?php if ( ! empty( $attributes['title'] ) ) : ?>
            <div class="aiopod-participants__title aiopod-text-heading">
                <?php echo esc_html( $attributes['title'] ); ?>
            </div>
        <?php endif; ?>

        <!-- Count -->
        <?php if ( ! empty( $attributes['show_count'] ) ) : ?>
            <div class="aiopod-participants__count aiopod-text-muted">
                <?php 
                $participants_count = (int) $attributes['count'];

                echo sprintf( 
                    _n( '%s participant found.', '%s participants found.', $participants_count, 'aio-podcast-studio' ), 
                    number_format_i18n( $participants_count ) 
                );
                ?>
            </div>
        <?php endif; ?>
        
        <!-- Participants -->
        <div class="aiopod-participants__list aiopod-responsive-wrapper">
            <div class="aiopod-grid aiopod-col-<?php echo esc_attr( $columns ); ?> aiopod-dynamic-wrapper">
                <?php
                // The loop
                foreach ( $terms as $term ) {
                    aiopod_the_participant_card( $term, $attributes );
                }     
                ?>
            </div>
        </div>

        <!-- Pagination -->
        <?php aiopod_the_pagination( $attributes ); ?>
    </div>
<?php elseif ( 0 === (int) $attributes['hide_if_empty'] ) : ?>
    <div class="aiopod aiopod-participants aiopod-participants--layout-grid">
        <div class="aiopod-participants__empty aiopod-text-muted">
            <?php esc_html_e( 'No participants found.', 'aio-podcast-studio' ); ?>
        </div>
    </div>
<?php endif;
