<?php

// Get the last published podcast related to the current show.
$args = array(
    'post_type' => 'emission',
    'posts_per_page' => 1,
    'meta_query' => array(
        'show_clause' => array(
            'key' => '_sud_programme',
            'value' => $post->ID,
        ),
        'date_clause' => array(
            'key' => '_sud_date_emission',
            'value' => date_format(date_create(), $date_format),
            'compare' => '<=',
        ),
    ),
    'ignore_sticky_posts' => true,
    'orderby' => 'date_clause',
    'order' => 'DESC',
);
// Pass this podcast in the main query to display it in details.
global $wp_query;

query_posts($args);
$wp_query->is_single = 1;
$wp_query->is_singular = 1;
$wp_query->is_archive = 0;
$idChronique= $post->ID;

//print_r($wp_query);
    get_header();
    get_template_part('phpincludes/menu');
    get_template_part('phpincludes/part-searchbar');

    $right_sidebar_slug = 'post-show-and-podcast-right-sidebar';
    $right_sidebar = is_active_sidebar($right_sidebar_slug);
?>
    <?php
        hm_get_template_part('phpincludes/sud-ads', [
            'area' => 'header',
        ]);
        hm_get_template_part('phpincludes/sud-ads', [
            'area' => 'background',
        ]);
    ?>
    <article id="maincontent" class="qt-main" itemscope itemtype="http://schema.org/RadioSeries">
    <div class="sud-post qt-spacer-s">

        <?php while (have_posts()) : the_post(); ?>
            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="qt-container">
                    <div class="row">
                        <div class="col s12<?php echo $right_sidebar ? ' m9' : ''; ?>">
                            <div class="qt-the-content">
                                <header class="sud-post__header">
                                    <?php echo programme_maitre($idChronique); ?>
                                    <?php the_title('<h1 class="sud-post__title qt-caption" itemprop="name">', '</h1>'); ?>

                                    <div class="sud-post__meta">
                                        <?php if (get_the_terms($post->ID, 'animateur')) : ?>
                                            <div class="sud-post__meta-author sud-author">
                                                <?php
                                                    $author = get_the_term_list($post->ID, 'animateur', 'Par <address itemprop="author">', '</address> et <address itemprop="author">');
                                                    echo strip_tags($author, '<address>');
                                                ?>

                                                <?php
                                                    $guests = get_podcast_guests($post);
                                                    if ($guests) :
                                                ?>
                                                    <span class="sud-post__meta-guests">
                                                        avec <?php echo implode(', ', array_column($guests, '_sud_invite')); ?>
                                                    </span>
                                                <?php endif; ?>
                                            </div>
                                        <?php endif; ?>

                                        <?php
                                            $date = $post->_sud_date_emission;
                                            if ($date) :
                                                $date_frmt = get_option('date_format') ? get_option('date_format') : 'l j F Y';
                                                $friendly_date = wp_date($date_frmt, date_format(date_create($date), 'U'));
                                        ?>
                                            <div class="sud-post__meta-date">
                                                <time datetime="<?php echo $date; ?>" itemprop="datePublished">
                                                    <?php echo 'Émission du '.$friendly_date; ?>
                                                </time>
                                            </div>
                                        <?php endif; ?>

                                        <hr class="qt-spacer-s">

                                        <div class="sud-post__meta-share">
                                            <?php get_template_part('phpincludes/sharepage'); ?>
                                        </div>
                                    </div>
                                </header>

                                <?php if (get_the_excerpt()) : ?>
                                    <blockquote class="sud-post__excerpt sud-excerpt" itemprop="about">
                                        <?php the_excerpt(); ?>
                                    </blockquote>
                                <?php endif; ?>

                                <?php
                                    hm_get_template_part('phpincludes/sud-post/post-video', [
                                        'video_url' => $post->_sud_replayvideo,
                                    ]);
                                ?>

                                <div class="sud-post__content" itemprop="description">
                                    <?php the_content(); ?>
                                    <hr class="qt-spacer-m">
                                </div>

                                <?php
                                    hm_get_template_part('phpincludes/sud-show/show-podcast', [
                                        'podcast' => $post,
                                        'hide_excerpt' => true,
                                        'disable_toggle' => true,
                                    ]);
                                ?>

                                <?php if (isset($guests)) : ?>
                                    <aside class="sud-post__guests qt-spacer-m">
                                        <h5 class="qt-caption-small">
                                            <span>Les invités</span>
                                        </h5>
                                        <table>
                                            <?php foreach ($guests as $guest) : ?>
                                                <tr itemprop="contributor" itemscope itemtype="https://schema.org/Person">
                                                    <th scope="row" itemprop="name"><?php echo $guest['_sud_invite']; ?></th>
                                                    <td itemprop="description"><?php echo $guest['_sud_bio_invite']; ?></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </table>
                                    </aside>
                                <?php endif; ?>

                                <?php
                                    $show = get_post($post->_sud_programme);
                                    if ($show) :
                                ?>
                                    <aside class="sud-post__info qt-spacer-m">
                                        <h5 class="qt-caption-small">
                                            <span>Le programme</span>
                                        </h5>
                                        <?php echo $show->post_content; ?>
                                    </aside>
                                <?php endif; ?>
                            </div>
                            <hr class="qt-spacer-m">
                        </div>

                        <?php if ($right_sidebar) : ?>
                            <div class="qt-sidebar col s12 m3 hide-on-med-and-down">
                                <?php
                                    hm_get_template_part('phpincludes/sidebar', [
                                        'sidebar' => $right_sidebar_slug,
                                    ]);
                                ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endwhile; ?>

        <?php
            hm_get_template_part('phpincludes/sud-post/post-related-shows', [
                'podcast' => $post,
            ]);
        ?>

        <?php get_template_part('phpincludes/sud-scripts'); ?>
    </div>
    </article>

    <?php
        hm_get_template_part('phpincludes/sud-ads', [
            'area' => 'sidebar',
        ]);
        hm_get_template_part('phpincludes/sud-ads', [
            'area' => 'footer',
        ]);
    ?>
<?php
    get_template_part('phpincludes/footerwidgets');
    get_footer();
?>