<?php
/**
 * Template part for displaying "convos" and widgets
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Xarxaprod_theme
 */

?>

<section id="convos" class="last-convos ultimes-convocatories">
  <?php
        // Detect plugin. For use on Front End only.
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
        // check for plugin using plugin name
    if ( is_plugin_active( 'xarxaprod-wp-plugin/xarxaprod-wp-plugin.php' ) ) {
        //plugin is activated do
    ?>
    <header>
    <?php //begin any query 
      // begin populate WP_Query if is custom post type xarxaprod-associat
      if ( get_post_type( ) == 'xarxaprod-associat' ){ 
      $slug_espai_associat =get_post_field( 'post_name', get_post() ) ;
      // https://developer.wordpress.org/reference/functions/get_posts/
      // query ACF relationsihp
      // https://wordpress.stackexchange.com/a/213375 
      $convos_query = new WP_Query(
        array(
          'post_type'      => 'xarxaprod-convo',
          'posts_per_page' => '3',
          'order_by'       => 'meta_value', //order by meta value
          'meta_key'       => 'xxp_convo_apply_end', // order by meta key convo apply end
          'order'          => 'ASC', //order ascendent
          'meta_query'     => array(
            'query_convo_apply_end'      =>  array(    //filter results show today and later
              'key'     => 'xxp_convo_apply_end', 
              'value'   => date("Y-m-d"), // consider using date_i18n() https://developer.wordpress.org/reference/functions/date_i18n/
              'compare' => '>=', //show events today and later
              'type'    => 'DATE', 
            ),
            'query_convo_espai_associat' =>  array(  //filter convos with relation to espai associat
              'key'  => 'xxp_convo_espai_associat', // name of the custom field
              'value' => '"' . get_the_ID() . '"', // in ACF select Return format "Post ID"
              'compare'=> 'LIKE', // matches exaclty "123", not just 123. This prevents a match for "1234"
            ),
            'orderby' => array (
               'query_convo_apply_end'   => 'ASC',
            ),
          )
        )
      );
    ?>
      <h2> <a href="<?php echo home_url( '/convos/?xxp_convo_center=' ) . $slug_espai_associat ;?>"><?php echo esc_html__('Últimes convocatòries', 'xarxaprod'); ?> de <?php the_title(); ?></a></h2>
    </header>
	  <main class="xarxaprod-last-convos xarxaprod-ultimes-convocatories">
    <?php } //end populate WP_Query custom post type xarxaprod-associat
      else { // begin populate WP_Query if others
     // https://developer.wordpress.org/reference/functions/get_posts/
     // https://www.advancedcustomfields.com/resources/true-false/
      $convos_query = new WP_Query(
        array(
          'post_type'      => 'xarxaprod-convo',
          'posts_per_page' => '3',
          'order_by'       => 'meta_value', //order by meta value
          'meta_key'       => 'xxp_convo_apply_end', // order by meta key convo apply end
          'order'          => 'ASC', //order ascendent
          'meta_query'     => array(
            array(    //filter results show today and later
              'key'     => 'xxp_convo_apply_end', 
              'value'   => date("Y-m-d"), // consider using date_i18n() https://developer.wordpress.org/reference/functions/date_i18n/
              'compare' => '>=', //show events today and later
              'type'    => 'DATE', 
            ),
          ),
          //https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
          'orderby'       => 'meta_value', //order by meta value
          'order'          => 'ASC', //order ascendent 
        )
      );
      ?>
      <h2> <a href="<?php echo site_url() . '/convocatories' ;?>"><?php echo esc_html__('Últimes convocatòries', 'xarxaprod'); ?></a></h2>
    </header>
	  <main class="xarxaprod-last-convos xarxaprod-ultimes-convocatories">
    <?php }; //end populate WP_Query with others ?>
     <?php //begin loop WP_Query ?> 
     <?php if ( $convos_query->have_posts() ) : ?> 
       <?php while ( $convos_query->have_posts() ) : $convos_query->the_post(); ?>
   
         <div class="xarxaprod-card-each xarxaprod-convo-info-field">
           <header class="entry-header">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark" class="">
             <?php the_title( '<h3 class="entry-title">', '</h3>' ); ?>
              </a>
           </header><!-- .entry-header -->


           <section class="xarxaprod-convo-info-fields">
             <ul>
               <li class="xarxaprod-convo-info-date">
                <?php if( get_field('xxp_convo_apply_begin') || get_field('xxp_convo_apply_end') ): ?>
                 <dt>Termini</dt>
                 <dd class="xarxaprod-label item-xxp_convo_dates">
                   <?php if( get_field('xxp_convo_apply_begin') ): ?>
                   <span class="item-xxp_convo_apply_begin">del <?php the_field('xxp_convo_apply_begin'); ?> </span>
                   <?php endif; ?>
                   <?php  if( get_field('xxp_convo_apply_end') && !get_field('xxp_convo_apply_begin') ){ echo 'fins'; } ?>
                <?php if( get_field('xxp_convo_apply_end') ): ?>
                   <span class="item-xxp_convo_apply_end">al <?php the_field('xxp_convo_apply_end'); ?> </span>
                   <?php endif; ?>
                   <?php if( get_field('xxp_convo_apply_text') ): ?>
                     <span class="item-xxp_convo_apply_text"><?php the_field('xxp_convo_apply_text'); ?></span>
                   <?php endif; ?>
                 </dd>
                <?php endif; ?>
               </li>
              <?php if( ! get_field('xxp_convo_espai_associat') ): ?>
                <?php // read here howto ?>
                <?php // https://www.advancedcustomfields.com/resources/relationship/#display-list-of-posts-with-setuppostdata ?>
                   <?php if( get_field('xxp_convoer_name') ): ?>
                   <li class="xarxaprod-convo-field">
                     <dt></dt>
                     <dd class="xarxaprod-convoer-name"><?php the_field('xxp_convoer_name'); ?></dd>
                   </li>
                   <?php endif; ?>
                <?php else: //exists get_field('xxp_convo_espai_associat')?>
                  <?php $espai_associat_posts = get_field('xxp_convo_espai_associat'); ?>
                  <?php foreach( $espai_associat_posts as $thispost ): ?>
                  <?php  // Setup this post for WP functions (variable must be named $post).?>
                       <li class="xarxaprod-convo-espai-associat">
                         <dt></dt>
                         <dd><a href="<?php echo ($thispost->guid);//the_permalink(); ?>"><?php echo ($thispost->post_title); ?><?php //the_title(); ?></a></dd>
                       </li>
                  <?php endforeach; ?>                                                                  
                  <?php // Reset the global post object so that the rest of the page works correctly.?>
              <?php endif; //end get_field('xxp_convo_espai_associat') ?>
             </ul>     
           </section>


           <section class="excerpt post-excerpt">
              <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark" class="">
              <?php echo wp_trim_words(get_the_excerpt(), 16); // https://developer.wordpress.org/reference/functions/wp_trim_words/?>
             </a>
           </section>
           <aside class="convo-sector convo-field"> 
           <?php get_template_part( 'template-parts/section', 'list-checkbox', array('iterate_field' => 'xxp_convo_field') ); ?>
           </aside>

           <nav class="more-link">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark" class="button button-more">
            Més info
            <span class="hide"><?php the_title(); ?></span>
            </a>
           </nav>

         </div>
   
       <?php endwhile;//end of the loop ?>
     <?php wp_reset_postdata(); ?>
     <?php else : //if no post in query ?>
         <p class="no-content"> No convos </p>
     <?php endif; //end WP_Query ?>
   
    </main>
  <?php  } //closing if is_plugin_active ?>
</section>