<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 28/06/18
 * Time: 19.38
 */
//the_content();
//data bella 'date-formatted' => mysql2date( get_option( 'date_format' ), get_sub_field( $event_date_field_name ) ),
$button_label     = 'vai';
$acf_row_selector = 'press_media';
global $scope_media_type;
global $scope_model;
global $scope_model_args;
global $scope_year;
//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';

//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);


// The Query
//$the_query = new WP_Query( $args );
//
//// The Loop
//if ( $the_query->have_posts() ) {
//	echo '<ul>';
//	while ( $the_query->have_posts() ) {
//		$the_query->the_post();
//		echo '<li>' . get_the_title() . '</li>';
//	}
//	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 ( have_model( $acf_row_selector, $scope_model, $scope_model_args ) ) :

//	$sorter = new o66_FieldSorter('press_date');
//	usort($acf_row_selector, array($sorter, "cmp"));

    ?>
    <div class="entry-content entry-content--press-<?php echo $scope_media_type; ?> grid --media-grid">
        <div class="row">
			<?php if ( $has_aside ) : ?>
            <div class="col-xs-12">
				<?php endif; ?>
				<?php while ( have_rows( $acf_row_selector ) ) :
					the_row(); ?>
					<?php $date = mysql2date( get_option( 'date_format' ), get_sub_field( 'press_date' ) );
					 $datePure = get_sub_field( 'press_date' ) ;
					$year = substr( $datePure, 0, 4 );

					?>

					<?php $title = get_sub_field( 'press_title' ); ?>
					<?php $content = get_sub_field( 'press_content' ); ?>
					<?php
					if ( $scope_media_type === 'video' ) {
						$video_id   = get_sub_field( 'press_video' );
						$button_url = 'http://www.youtube.com/watch?v=' . $video_id;
					};
					if ($scope_media_type === 'press') {
						$press_file = get_sub_field( 'press_file' );
						$file_size = $press_file['filesize'];
						$ret_file_size = bytes_to_kb($file_size);
						//var_dump($ret_file_size);

						$button_label = 'PDF ('.($ret_file_size ).' Kb)';
						$button_url = $press_file['url'];


                    }
					?>
					<?php if ( $has_aside ) :
					?>
                    <div class="__row-wrapper" <?php if($scope_year !== $year) {echo 'style="display:none"';}; ?> >
                    <div class="row">
				<?php
				endif;
					if ( $has_aside ) : ?>
                        <div class="col-<?php echo $aside_col; ?>">
                            <aside class="__aside __is-<?php echo $scope_media_type; ?>">
                                <i class="fa fa-file-pdf-o"></i>
                            </aside>
                        </div>
					<?php endif; ?>
                    <div class="col-<?php echo $bootstrap_col; ?>"  <?php if($scope_year !== $year) {echo 'style="display:none"';}; ?>>
                        <article class="__container" <?php if($scope_year !== $year) {echo 'style="display:none"';}; ?>>
                            <div class="__item __is-<?php echo $scope_media_type; ?>">
								<?php if ( $has_figure ) : ?>
                                    <figure class="__thumbnail img-responsive-container">
                                        <a class="popup-youtube"
                                           href="http://www.youtube.com/watch?v=<?php the_sub_field( 'press_video' ); ?>">
                                            <img src="https://img.youtube.com/vi/<?php the_sub_field( 'press_video' ); ?>/sddefault.jpg"/>
                                        </a>
                                    </figure>
								<?php endif; ?>
                                <div class="__date">
                                    <i class="fa fa-calendar-o"></i> <?php echo $date; ?>
                                </div>
                                <div class="__title">
                                    <h3><?php echo $title; ?></h3>
                                </div>
                                <div class="__content">
									<?php echo $content; ?>
                                </div>
                                <div class="__button-container">
									<?php o66fpwp_a_button( $button_url, $button_label,
										'<i class="fa fa-arrow-circle-o-right"></i>',
										false, null ); ?>
                                </div>
                            </div>
                        </article>
                    </div>
					<?php if ( $has_aside ) :
					?>
                    </div>

                    </div>
				<?php
				endif;
				endwhile; ?>
				<?php if (  $has_aside ) : ?>
            </div>
		<?php endif; ?>
        </div>
    </div>
<?php else : ?>
	<?php // no rows found ?>
<?php endif; ?>
<?php

//o66fswp_the_content_block();
//o66fswp_the_section_repeater_block();
?>

