<?php

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

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

$episodes_count     = (int) $attributes['count'];
$columns            = (int) $attributes['columns'];
$player_autoadvance = (int) $attributes['player_autoadvance'];
$player_loop        = (int) $attributes['player_loop'];
$featured_episode   = null;

if ( $aiopod_query->have_posts() && ! empty( $attributes['enable_player'] ) ) {
    foreach ( $aiopod_query->posts as $post ) {
        $has_access = aiopod_current_user_can( 'view_aiopod_podcast', $post->ID );
        if ( $has_access ) {
            $featured_episode = $post;
            break;
        }
    }         
}

if ( ! $featured_episode ) {
    $attributes['enable_player']    = 0;
    $attributes['interactive_mode'] = 0;
}

if ( $episodes_count > 1 && $player_autoadvance && $player_loop ) {
    $attributes['player_loop'] = 0;
}

// Render HTML output
if ( $aiopod_query->have_posts() ) : ?>
    <div class="aiopod aiopod-episodes aiopod-episodes--layout-grid<?php if ( ! empty( $attributes['interactive_mode'] ) ) : ?> aiopod-episodes--interactive<?php endif; ?>">
        <?php 
        if ( ! wp_doing_ajax() ) {
            // Title
            if ( ! empty( $attributes['title'] ) ) {
                echo sprintf(
                    '<div class="aiopod-episodes__title aiopod-text-heading">%s</div>',
                    esc_html( $attributes['title'] )
                );
            }

            // Player
            if ( ! empty( $attributes['enable_player'] ) && $featured_episode ) {
                aiopod_the_player_card( $featured_episode, $attributes );
            }

            // Filters
            if ( ! empty( $attributes['enable_filters'] ) ) {
                aiopod_the_filters_form( $attributes );
            }
        }
        ?>

        <aiopod-episodes<?php if ( $player_autoadvance ) : ?> autoadvance<?php endif; ?><?php if ( $player_loop ) : ?> loop<?php endif; ?>>
            <!-- Count -->
            <?php if ( ! empty( $attributes['show_count'] ) ) : ?>
                <div class="aiopod-episodes__count aiopod-text-muted">
                    <?php echo sprintf( _n( '%s episode found.', '%s episodes found.', $episodes_count, 'aio-podcast-studio' ), number_format_i18n( $episodes_count ) ); ?>
                </div>
            <?php endif; ?>

            <!-- Episodes -->
            <div class="aiopod-episodes__list aiopod-responsive-wrapper">
                <div class="aiopod-grid aiopod-col-<?php echo esc_attr( $columns ); ?> aiopod-dynamic-wrapper">
                    <?php
                    // Start the loop
                    global $post;

                    // The loop          
                    while ( $aiopod_query->have_posts() ) :        
                        $aiopod_query->the_post();  
                        aiopod_the_episode_card( $post, $attributes );       
                    endwhile;
                        
                    // Use reset postdata to restore orginal query
                    wp_reset_postdata();   
                    ?>
                </div>
            </div>

            <!-- Pagination -->
            <?php aiopod_the_pagination( $attributes ); ?>
        </aiopod-episodes>
    </div>
<?php elseif ( 0 === (int) $attributes['hide_if_empty'] ) : ?>
    <div class="aiopod aiopod-episodes aiopod-episodes--layout-grid<?php if ( ! empty( $attributes['interactive_mode'] ) ) : ?> aiopod-episodes--interactive<?php endif; ?>">
        <?php
        if ( ! wp_doing_ajax() ) {
            // Filters
            if ( ! empty( $attributes['enable_filters'] ) ) {
                aiopod_the_filters_form( $attributes );
            }
        }
        ?>

        <aiopod-episodes>
            <div class="aiopod-episodes__empty aiopod-text-muted">
                <?php esc_html_e( 'No episodes found.', 'aio-podcast-studio' ); ?>
            </div>
        </aiopod-episodes>
    </div>
<?php endif;
