<?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_categories',
    'orderby'      => sanitize_text_field( $attributes['orderby'] ), 
    'order'        => sanitize_text_field( $attributes['order'] ),
    'hide_empty'   => (int) $attributes['hide_empty'],
    'hierarchical' => false
);

if ( ! empty( $attributes['include'] ) ) { // Include category IDs
    $args['include'] = array_map( 'intval', explode( ',', $attributes['include'] ) );
} else {
    $args['child_of']   = (int) $attributes['id'];
    $args['pad_counts'] = true;
}

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

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

// Render HTML output
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) :
    if ( isset( $args['child_of'] ) ) {
        foreach ( $terms as $key => $term ) {
            if ( $term->parent !== $args['child_of'] ) {
                unset( $terms[ $key ] );
            }
        }

        $terms = array_values( $terms );
    }

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

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

        if ( 'none' !== $attributes['pagination'] ) {
            $attributes['count'] = count( $terms );
            $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-categories aiopod-categories--layout-grid">
        <!-- Title -->
        <?php if ( ! empty( $attributes['title'] ) ) : ?>
            <div class="aiopod-categories__title aiopod-text-heading">
                <?php echo esc_html( $attributes['title'] ); ?>
            </div>
        <?php endif; ?>     
        
        <div class="aiopod-categories__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_category_card( $term, $attributes );
                }     
                ?>
            </div>
        </div>

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