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



/**
 * @param $post_type
 * @param $meta_key_acf_field
 * @param string $icon_tag
 * @param string $link
 */

function o66fswp_the_posts_acf_filtered_team( $post_type, $meta_key_acf_field, $icon_tag = '<i class="fa fa-arrow-circle-o-down"></i>', $link = 'single' ) {

	$args        = array(
		'posts_per_page'   => 999,
		'offset'           => 0,
		//	'category'         => '',
		//	'category_name'    => '',
		'orderby'          => 'meta_value',
		'order'            => 'ASC',
		'include'          => '',
		'exclude'          => '',
		'meta_key'         => $meta_key_acf_field,
		//'meta_value'       => 'gestionale',
		'post_type'        => $post_type,
		'post_mime_type'   => '',
		'post_parent'      => '',
		'author'           => '',
		'author_name'      => '',
		'post_status'      => 'publish',
		'suppress_filters' => true,
		'fields'           => '',
	);
	$posts_array = get_posts( $args );
	$cat_array   = array();

	foreach ( $posts_array as $item ) {
		array_push( $cat_array, get_field( 'team_category', $item->ID ) );
	}
	$cat_array_unique = array_unique( $cat_array );
	//var_dump($cat_array_unique);
	if ( ! empty( $cat_array_unique ) ) : ?>
        <div class="aside--press">
		<?php
		$globalCurrentPostId = get_the_ID();
	foreach ( $cat_array_unique as $cat_item ) :
		//echo $cat_item;
    //multi orderby
		$args = array(
			'meta_query' => array(
				'relation' => 'AND',
				'team_cat_mq' => array(
					'key' => 'team_category',
					'value' => $cat_item,
				),
				'cognome' => array(
					'key' => 'cognome',
                   // 'value' => 'Mach'

				),
			),
			'orderby' => array(
			//	'city_clause' => 'ASC',
				'cognome' => 'ASC',
			),
			'posts_per_page'   => 999,
			'offset'           => 0,

			'post_type'        => $post_type





		);

		$this_cat_posts = get_posts( $args );
		$sorter = new o66_FieldSorter('cognome');
		//$query = new WP_Query( array( 'post_type' => 'page' ) );
		//usort($this_cat_posts, array($sorter, "cmp"));
		//$sorted_and_reversed = array_reverse($acf_reapeter);
		//var_dump($this_cat_posts[0]->post_title);
		//global $global_post_id;
		$postIndex = get_field('pagine_indice_team', 'option');
		?>

        <div class="aside__media-year-list">
        <div class="media-year-list__heading">
            <h3><?php echo get_field('label_'.$cat_item, $postIndex);?></h3>
        </div>
        <ul>
			<?php foreach ( $this_cat_posts as $post_item ) :
                $stringClassLi = '';
                if ($globalCurrentPostId === $post_item->ID) {$stringClassLi = ' class="current"';}
				if ( $link === 'single' ) {
					$aTagStart = '<a href="' . get_the_permalink( $post_item->ID ) . '">';
					$aTagEnd   = '</a>';
				}
				?>
                <li<?php echo $stringClassLi;?>><?php echo $aTagStart . $icon_tag; ?><?php echo $post_item->post_title . $aTagEnd; ?></li>
			<?php endforeach; ?>
        </ul>
	<?php endforeach; ?>
        </div>
	<?php endif;
}

o66fswp_the_posts_acf_filtered_team( 'membro_team', 'team_category', '<i class="fa fa-user-circle-o"></i>' );



