<?php

/**
 * Server render for audace/production.
 *
 * @var array    $attributes
 * @var string   $content
 * @var WP_Block $block
 */

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

$max_items      = isset($attributes['maxItems'])      ? (int) $attributes['maxItems']      : 0;
$loop_arrows    = ! empty($attributes['loopArrows']);
$download_label = isset($attributes['downloadLabel']) ? (string) $attributes['downloadLabel'] : 'télécharger';

$query = new WP_Query(array(
    'post_type'              => 'production',
    'posts_per_page'         => $max_items > 0 ? $max_items : -1,
    'orderby'                => array('menu_order' => 'ASC', 'date' => 'ASC'),
    'no_found_rows'          => true,
    'update_post_term_cache' => false,
    'ignore_sticky_posts'    => true,
));

if (! $query->have_posts()) {
    if (current_user_can('edit_posts')) {
        $wrapper = get_block_wrapper_attributes(array('class' => 'audace-production is-empty'));
        echo '<section ' . $wrapper . '><p>' . esc_html__('Aucune production publiée.', 'audace') . '</p></section>';
    }
    return;
}

$posts = $query->posts;

$left_fr     = isset($attributes['leftColFr'])    ? (float) $attributes['leftColFr']    : 1;
$right_fr    = isset($attributes['rightColFr'])   ? (float) $attributes['rightColFr']   : 1;
$left_pad_x  = isset($attributes['leftColPadX'])  ? (int)   $attributes['leftColPadX']  : 0;
$left_pad_y  = isset($attributes['leftColPadY'])  ? (int)   $attributes['leftColPadY']  : 0;
$right_pad_x = isset($attributes['rightColPadX']) ? (int)   $attributes['rightColPadX'] : 60;
$right_pad_y = isset($attributes['rightColPadY']) ? (int)   $attributes['rightColPadY'] : 50;

$spacing = function ( $n ) {
    return $n ? 'var(--wp--preset--spacing--' . (int) $n . ')' : '0';
};

$style = sprintf(
    '--audace-prod-left:%sfr;--audace-prod-right:%sfr;--audace-prod-left-pad-x:%s;--audace-prod-left-pad-y:%s;--audace-prod-right-pad-x:%s;--audace-prod-right-pad-y:%s;',
    $left_fr,
    $right_fr,
    $spacing( $left_pad_x ),
    $spacing( $left_pad_y ),
    $spacing( $right_pad_x ),
    $spacing( $right_pad_y )
);

$wrapper_args = array(
    'class'       => 'audace-production is-style-slide',
    'data-loop'   => $loop_arrows ? '1' : '0',
    'data-active' => '0',
    'style'       => $style,
);
if ( ! empty( $attributes['anchor'] ) ) {
    $wrapper_args['id'] = sanitize_title_with_dashes( (string) $attributes['anchor'] );
}
$wrapper = get_block_wrapper_attributes( $wrapper_args );

$arrow_download = esc_url(get_theme_file_uri('assets/images/audace-fleche-telechargement.svg'));
$arrow_prev     = esc_url(get_theme_file_uri('assets/images/audace-fleche-gauche.svg'));
$arrow_next     = esc_url(get_theme_file_uri('assets/images/audace-fleche-droite.svg'));
?>
<section <?php echo $wrapper; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
            ?>>
    <div class="audace-production__stage">
        <?php foreach ($posts as $i => $p) :
            $thumb   = get_the_post_thumbnail($p, 'large', array('class' => 'audace-production__img'));
            $file_id = (int) get_post_meta($p->ID, '_audace_download_file', true);
            $file    = $file_id ? wp_get_attachment_url($file_id) : '';
            $body    = apply_filters('the_content', $p->post_content);
        ?>
            <article
                class="audace-production__slide<?php echo 0 === $i ? ' is-active' : ''; ?>"
                data-index="<?php echo (int) $i; ?>"
                <?php echo 0 === $i ? '' : 'aria-hidden="true"'; ?>>
                <div class="audace-production__media">
                    <?php if ($thumb) : ?>
                        <figure class="audace-production__figure"><?php echo $thumb; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
                                                                    ?></figure>
                    <?php endif; ?>
                </div>
                <div class="audace-production__body">
                    <h2 class="audace-production__title"><?php echo esc_html(get_the_title($p)); ?></h2>
                    <?php if ($body) : ?>
                        <div class="audace-production__content"><?php echo $body; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
                                                                ?></div>
                    <?php endif; ?>
                </div>
                <div class="audace-production__footer">
                    <?php if ($file) : ?>
                        <a class="audace-production__download" href="<?php echo esc_url($file); ?>" download>
                            <span class="audace-production__download-label"><?php echo esc_html($download_label); ?></span>
                            <img src="<?php echo $arrow_download; ?>" alt="" aria-hidden="true" />
                        </a>
                    <?php else : ?>
                        <span></span>
                    <?php endif; ?>
                    <?php if (count($posts) > 1) : ?>
                        <nav class="audace-production__arrows" aria-label="<?php echo esc_attr__('Navigation entre productions', 'audace'); ?>">
                            <button type="button" class="audace-production__arrow audace-production__arrow--prev" aria-label="<?php echo esc_attr__('Précédent', 'audace'); ?>">
                                <img src="<?php echo $arrow_prev; ?>" alt="" aria-hidden="true" />
                            </button>
                            <button type="button" class="audace-production__arrow audace-production__arrow--next" aria-label="<?php echo esc_attr__('Suivant', 'audace'); ?>">
                                <img src="<?php echo $arrow_next; ?>" alt="" aria-hidden="true" />
                            </button>
                        </nav>
                    <?php endif; ?>
                </div>
            </article>
        <?php endforeach; ?>
    </div>
</section>
<?php
wp_reset_postdata();
