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

use Reach\App\Fields\Groups\FieldGroupStaff;
use Reach\App\Fields\FieldParser;
use Reach\App\Modules\Modules;
use Reach\App\Modules\Animations;

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

// check that we have images set
if ( empty( $data->staff_staff ) ) {
    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 . '_staff' );
$defaults     = FieldGroupStaff::animationDefaults();
$tweenClasses = Animations::getTweenClasses($animations, $defaults);
?>

<div id="<?php echo 'module_' . $index; ?>" 
    class="module module--staff <?php echo $admin_css; ?> 
    <?php echo apply_filters( 'reach/modules/classes/text', ' text--dark', $data->staff_background['text_color'] ); ?>"
    <?php do_action( 'reach/modules/styles', $data );?>
    <?php do_action( 'reach/modules/animation/data', $animations, $defaults ); ?>>
    <?php do_action( 'reach/before_module_content', $filtered_module ) ?>

    <?php if ( ! empty( $data->staff_content ) ) : ?>
        <div class="staff__content">
            <div class="container">
                <div class="entry__content <?php echo $tweenClasses ?>">
                    <?php echo apply_filters( 'the_content', $data->staff_content ); ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <div class="staff">
        <?php
        // loop through the rows of data
        while ( have_rows( $group_key . '_staff_staff', get_the_ID() ) ) : the_row();
            // get sub field values
            $name  = get_sub_field( 'name' );
            $title = get_sub_field( 'title' );
            $image = get_sub_field( 'image' );
            $bio   = get_sub_field( 'bio' );
            ?>
            <a class="staff__item<?php echo ( ! empty( $data->staff_column_count ) ? ' staff__item--' . $data->staff_column_count : '' );?> <?php echo $tweenClasses ?>">
                <?php if ( $image ) : ?>
                    <div class="staff__image">
                        <img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
                             data-src="<?php echo esc_url( $image['sizes']['featured-small'] ); ?>"
                             alt="<?php echo esc_html( $image['alt'] ); ?>">
                    </div>
                <?php endif; ?>
                <div class="staff__info">
                    <?php
                    echo $name ? '<h4 class="staff__name">' . esc_html( $name ) . '</h4>' : '';
                    echo $title ? '<h6 class="staff__title">' . esc_html( $title ) . '</h6>' : '';
                    echo $bio ? '<div class="staff__bio entry__content">' . wpautop( $bio ) . '</div>' : '';
                    ?>
                </div>
            </a>
        <?php endwhile; ?>
    </div>

    <?php do_action( 'reach/after_module_content', $filtered_module ); ?>
</div>
