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

echo 'yo';

$repeater_field_name = 'press_zone'; $press_title_field_name = 'press_title';
	$press_date_field_name = 'press_date'; $press_content_field_name = 'press_content'; $post_id = null;

	global $post;

	if ( $post_id === null ) {
		$post_id = $post->ID;
	}
	if ( class_exists( 'acf' ) ) {
		if ( have_rows( $repeater_field_name ) ) : ?>
			<?php
			//Events Array Model
			$eventArray        = array();
			$yearsArray        = array();
			$datesArray        = array();
			$eventsByYearArray = array();

			//TODO:done PROCEDERE DA QUI FUNCTIONAL/MIDDLEWARE E GERETAI GLI ARRAY

			while ( have_rows( $repeater_field_name ) ) : the_row();
				// singlebyte strings
				//$result = substr($myStr, 0, 5);
				//setlocale(LC_TIME, 'it_IT.UTF8');
				$date          = get_sub_field( $press_date_field_name );
				$year          = substr( $date, 0, 4 );
				$eventArrayRow = array(
					'title'          => get_sub_field( $press_date_field_name ),
					'date'           => get_sub_field( $press_content_field_name ),
					// 'date-it' => date('d F Y',get_sub_field($event_date_field_name)),
					// $the_date = mysql2date( get_option( 'date_format' ), $post->post_date )
					'date-formatted' => mysql2date( get_option( 'date_format' ), get_sub_field( $event_date_field_name ) ),
					'content'        => get_sub_field( $press_content_field_name ),
					'year'           => $year
				);
				array_push( $eventArray, $eventArrayRow );
				array_push( $yearsArray, $year );
				array_push( $datesArray, $date );
			endwhile;
			$yearsArrayUnique = array_unique( $yearsArray );
			rsort( $yearsArrayUnique );
			//array_multisort($vc_array_value, SORT_DESC, $vc_array_order, SORT_DESC, $vc_array_name, SORT_ASC, $vc_array);
			array_multisort( $datesArray, SORT_DESC, $eventArray );
			array_multisort( $yearsArray, SORT_DESC, $eventArray );
			$totalEvents = count( $eventArray );
			function filter_events_by_year_array( $item ) {

			}


			//var_dump( $yearsArrayUnique );
			//TODO sistemare titoli a sinistra lunghezza scale ecc. come mobile magari
			?>
			<section class="entry-content__section entry-content__section--events-timeline-container">
				<div class="entry-content__section__header"><h2><?php echo 'La Storia'; ?></h2></div>
				<div class="blok">
					<?php
					$yearCount = 0;
					foreach ( $yearsArrayUnique as $year ) :
						//$thisYearsArray = array_fill( 0, $totalEvents, $year );
						$thisYearEventsArray = array_filter( $eventArray, function ( $item ) use ( $year ) {
							if ( $item['year'] === $year ) {
								return $item;
							} else {
								return null;
							}
						} );

						//	$eventsCurrentYear = array();


						?>
						<div class="entry-content__section__row">
							<div class="row">
								<div class="col-md-3">
									<div class="events-timeline__year-title">
										<h4><i class="fa fa-calendar-o"></i> <?php echo $year; ?></h4>
									</div>
								</div>
								<div class="col-md-9">
									<?php
									//TODO SPOSTAMI NELLA MIA FUNZIONE APPENA FINISCI TIMELINE
									//accordion default a tipo load more
									//collegato con il js o66fs-template-tags.js
									//valutare css, magari da js
									//accordion-always
									//accordion-more
									//accordion-action-trigger che avrà un attributo opened, default false
									$accordionCount  = 0;
									$totalYearEvents = count( $thisYearEventsArray );
									foreach ( $thisYearEventsArray as $itemEvent ) :
										if ( $accordionCount === 0 ) {
											$data_accordion_always = 'true';
										}
										if ( $accordionCount > 0 ) {
											$data_accordion_always = 'false';
										}


										?>
										<div class="events-timeline__event_container"
										     data-accordion-content="events-timeline-<?php echo $year; ?>"
										     data-accordion-always="<?php echo $data_accordion_always; ?>">
											<p class="events-timeline_event-date"><i
													class="fa fa-square"></i> <?php echo $itemEvent['date-formatted']; ?>
											</p>
											<h3 class="events-timeline_event-title"><?php echo $itemEvent['title']; ?></h3>
											<?php echo $itemEvent['content']; ?>
											<?php // var_dump( $itemEvent );
											?>
										</div>
										<?php $accordionCount ++; endforeach; ?>
									<div class="events-timeline__open-accordion-button-container">
										<?php
										$isOpened = false;
										if ( $yearCount === 0 ) {
											$isOpened = true;
										}
										if ( $yearCount !== 0 ) {
											$isOpened = false;
										}
										if ( $totalYearEvents > 1 ) {
											o66fpwp_a_button_accordion_action_trigger( 'Apri ' . $year, 'Chiudi ' . $year,
												'<i class="fa fa-arrow-circle-o-down"></i>', '<i class="fa fa-arrow-circle-o-up"></i>', false,
												'events-timeline-' . $year, $isOpened );
										}
										?>
									</div>
								</div>
							</div>
						</div>
						<?php $yearCount ++; endforeach; ?>
				</div>
			</section>
		<?php endif; }?>