<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 28/06/18
 * Time: 19.38
 */

//data bella 'date-formatted' => mysql2date( get_option( 'date_format' ), get_sub_field( $event_date_field_name ) ),
$button_label     = 'leggi tutto';
$acf_row_selector = 'press_media';
global $scope_media_type;
global $scope_model;
global $scope_model_args;
global $global_post_id;
//var_dump($scope_model);
//var_dump($scope_media_type);
//var_dump($scope_model_args);
$bootstrap_col = 'md-4';
$button_url    = '';
$has_aside     = false;
$aside_col     = 'md-1';
$has_figure    = true;
//type def
if ( $scope_media_type === 'video' ) {
	$acf_row_selector = 'video_media';
	$has_figure       = true;
	//$is_video     = true;
	$button_label = 'guarda il video';
}
if ( $scope_media_type === 'press' ) {
	$has_figure    = false;
	$button_label  = 'pdf';
	$bootstrap_col = 'xs-11';
	$aside_col     = 'xs-1';
	$has_aside     = true;
}
//var_dump($scope_media_type);

//global $bootstrap_col;
// The Query
$the_query = new WP_Query( $scope_model_args );
//console_log($scope_model_args);
$team_cats      = array();
$conta_team_cat = 0;
if ( $the_query->have_posts() ) {

	while ( $the_query->have_posts() ) {
//var_dump($conta_team_cat);
		$conta_team_cat ++;
		$the_query->the_post();

		array_push( $team_cats, get_field( 'team_category' ) );

	}

}
$team_cats_unique = array_unique( $team_cats );
//var_dump( $team_cats_unique );
//echo'conta';
//console_log($team_cats_unique);

// The Loop
//if ( $the_query->have_posts() ) {
//	echo '<ul>';

//	echo '</ul>';
//	/* Restore original Post Data */
//	wp_reset_postdata();
//} else {
//	// no posts found
//}


//TODO MOLTO MOLTO MOLTO MOTLO BELLO CONTINUARE DA QUI
//function have_model($the_model_value, $the_model_source, $wp_query_args ){
//    if (empty($the_model_source)) {
//	    return have_rows( $the_model_value );
//    }
//	if (!empty($the_model_source)) {
//		$the_query = new WP_Query( $wp_query_args );
//		return $the_query->have_posts();
//	}
//
//}


?>
<?php if ( $team_cats_unique ) : ?>
	<?php foreach ( $team_cats_unique as $item_cat ) :
		//echo $item_cat;
		$conta_team_item = 0;
		?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header entry-header--page">
                <div class="entry-header__title-container"><h2 class="__is-capitalize"><?php echo get_field('label_'.$item_cat, $global_post_id);?></h2>
                </div>
            </header>
        </article>
		<?php if ( $the_query->have_posts() ) : ?>
        <div class="entry-content entry-content--press-team grid --media-grid ">
            <div class="row">
				<?php
				$conta_team_item = 0;
				while ( $the_query->have_posts() ) :
					$the_query->the_post(); ?>
					<?php //$date = mysql2date( get_option( 'date_format' ), get_sub_field( 'press_date' ) );
					?>
					<?php $title = get_the_title(); ?>
					<?php $content = get_the_excerpt(); ?>
					<?php $button_url = get_the_permalink();
					if ( get_field( 'team_category' ) == $item_cat ): ?>

                        <div class="col-<?php echo $bootstrap_col; ?>">

							<?php if ( $conta_team_item === 0 ) : ?>
                                <article class="__container hidden-md hidden-lg">
                                    <div class="__item __is-team-mobile-text">
										<?php the_field( 'testo_' . $item_cat, $global_post_id ); ?>
                                    </div>
                                </article>
							<?php endif; ?>

                            <article class="__container ">
                                <div class="__item __is-<?php echo $scope_media_type; ?>">
									<?php if ( $has_figure ) : ?>
                                        <img class="__thumbnail img-responsive-container"
                                             src="<?php echo get_the_post_thumbnail_url( get_the_ID(), o66_image_sizes::rect ); ?>"/>
									<?php endif; ?>

                                    <div class="__title">
                                        <h3><?php echo $title; ?></h3>
                                    </div>
                                    <div class="__content __is-team js-team">
                                        <p style="padding-bottom: 15px"><?php echo $content; ?></p>
                                    </div>
                                    <div class="__button-container" >
										<?php o66fpwp_a_button( $button_url, $button_label,
											'<i class="fa fa-file-pdf-o"></i>',
											true, null ); ?>
                                    </div>
                                </div>
                            </article>
                        </div>
						<?php $conta_team_item ++;
						//echo $conta_team_item;
					endif; ?>
					<?php ?>
				<?php endwhile; ?>
            </div>
        </div>
	<?php endif; ?>
	<?php endforeach; ?>
<?php endif; ?>

