<?php
/**
 * Module: Testimonials
 *
 * @global $page_meta
 * @global $module_class
 * @global $front_page
 * @global $index
 *
 * @package Reach
 */

use Reach\App\Fields\Groups\FieldGroupTestimonials;
use Reach\App\Fields\FieldParser;
use Reach\App\Modules\Modules;
use Reach\App\Posts\BlogPosts;
use Reach\App\Modules\Animations;

// get module fields and data object values
$fields     = ( new FieldGroupTestimonials( $group_key ) )->getFields();
$data       = ( new FieldParser() )->processFieldData( $fields, $page_meta, $group_key );
$admin_css  = ( new FieldParser() )->prefixCssClasses( "rl", esc_attr($data->testimonials_css_classes) );

// check if the repeater field has rows of data
if ( ! have_rows( $group_key . '_testimonials_testimonials' ) ) {
    return false;
}

/**
 * Animation Variables
 * 
 * -- Directions for Implementation --
 * 
 * 1) Add use Reach\App\Modules\Animations;
 * 
 * 2) Change prefix and $defaults to match the module's field prefix and class
 * 
 * 3) Add do_action( 'reach/modules/animation/data', $animations, $defaults );
 * to the container element to output animation data attributes and
 * set the scene
 * 
 * 4) Add $tweenClasses to the classes of the child content elements
 * you want to animate
 * 
 */
$animations   = ( new Animations() )->getAnimationOptions( $group_key . '_testimonials' );
$defaults     = FieldGroupTestimonials::animationDefaults();
$tweenClasses = Animations::getTweenClasses($animations, $defaults);

$classes = 'testimonials--' . ( 0 !== (int) $data->testimonials_slides_to_show ? 'carousel' : 'list' );
$classes .= ( 1 < (int) $data->testimonials_slides_to_show ? ' testimonials--show-multiple' : '' );
$classes .= ( 'below' === $data->testimonials_arrow_placement ? ' testimonials--arrows-below' : '' );
$classes .= ' ' . $tweenClasses;
$classes .= ' testimonials--arrows-' . $data->testimonials_arrow_color;
$classes .= $data->testimonials_enable_arrow_background === 'off' ? ' testimonials--arrows-no-bg' : ''; 
?>

<div id="<?php echo 'module_' . $index; ?>"
     class="module module--testimonials testimonials--<?php echo $data->testimonials_slides_to_show; ?> <?php echo $module_class . ' ' .  $admin_css; ?> 
     <?php echo apply_filters( 'reach/modules/classes/text', ' text--dark', $data->testimonials_background['text_color'] ); ?>"
     <?php do_action( 'reach/modules/styles', $data ); ?>
     <?php do_action( 'reach/modules/animation/data', $animations, $defaults ); ?>
     >
    <div class="container">
        <div class="testimonials__panel">
            <?php echo $data->testimonials_content ? '<div class="testimonials__content entry__content">' . apply_filters( 'the_content', $data->testimonials_content ) . '</div>' : ''; ?>
            <div class="testimonials <?php echo $classes; ?>"
                 data-autoplay="<?php echo $data->testimonials_autoplay; ?>"
                 data-slides-to-show="<?php echo $data->testimonials_slides_to_show ?: 1; ?>"
                 data-arrow-placement="<?php echo $data->testimonials_arrow_placement; ?>"
                 <?php if ( $data->testimonials_enable_arrow_background === 'on' ) : ?>
                    data-arrows-bg="<?php echo $data->testimonials_arrow_background_color; ?>"
                <?php endif; ?>>
                <?php
                // loop through the rows of data
                while ( have_rows( $group_key . '_testimonials_testimonials' ) ) : the_row();
                    // get sub field values
                    $name    = get_sub_field( 'name' );
                    $date    = get_sub_field( 'date' );
                    $content = get_sub_field( 'content' );
                    $stars   = get_sub_field( 'star_rating' );
                    $avatar  = get_sub_field( 'avatar' );
                    $content_limit = $data->testimonials_enable_content_limit;
                    $content_range = $data->testimonials_content_limit_range;
                    $excerpt = BlogPosts::getContentLimited($content, $data->testimonials_content_limit_range);
                    ?>

                    <div class="testimonial" tabindex="0">
                        <div class="testimonial__icon">
                            <svg class="icon icon-quote">
                                <use xlink:href="#icon-quote"></use>
                            </svg>
                        </div>
                        <?php if ( $stars ) : ?>
                            <ul class="testimonial__star-rating">
                                <?php
                                // loop through and output star icons, checking the star count to determine fill color
                                for ( $i = 1; $i < 6; $i ++ ) {
                                    $filled = $i <= $stars ? 'filled' : '';
                                    echo '<li><svg class="icon icon-star ' . $filled . '"><use xlink:href="#icon-star"></use></svg></li>';
                                }
                                ?>
                            </ul>
                        <?php endif; ?>
                        <div class="testimonial__content <?php echo $content_limit ? 'content-limit' : '' ?>">
                            <?php 
                            if($content_limit && (strlen($content) >= $content_range)) : ?>
                                <div class="testimonial__content--excerpt">
                                    <?php echo wpautop( $excerpt ); ?>
                                    <p class="read-more" 
                                        style="background-color: <?php echo $data->testimonials_background['background_color'] ?>"
                                        >
                                            <a href="javascript:void(0);" class="read-more-link"><?php _e('Read More', 'reach'); ?></a>
                                    </p>
                                </div>
                                <div class="testimonial__content--content">
                                    <?php echo wpautop( $content ); ?>
                                </div>
                            <?php
                            else :
                                echo wpautop( $content );
                            endif; ?>    
                        </div>
                        <div class="testimonial__meta">
                            <?php
                            echo esc_html( $name );
                            echo $date ? ', ' . esc_html( $date ) : '';
                            ?>
                        </div>
                        <?php if ( $avatar ) : ?>
                            <div class="testimonial__avatar">
                                <img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
                                     data-src="<?php echo esc_url( $avatar['sizes']['thumbnail'] ); ?>"
                                     alt="<?php echo $avatar['title']; ?>">
                            </div>
                        <?php endif; ?>
                    </div>

                <?php endwhile; ?>
            </div>

            <?php echo Modules::outputButtonMarkup( $data->testimonials_button, 'testimonials', $data->testimonials_background, '', $data->testimonials_button_alignment ); ?>
        </div>
    </div>
</div>
