<?php
/**
 * Theme list Publicação
 *
 * Padrão Governo Federal - Basil functions and definitions
 * Author:Cristiano Augusto Cunha Silva <sem@cristianoaugusto.com.br>
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage CCST-GOV
 * @since 1.1
 * 
 *  Este thema foi desenvolvido para o Centro de Ciências do Sistema Terreste / INPE - BRASIL
 * 
 */
?>



<?php
if( get_post_type() == 'publicacao' ): // Se o post type foi post ?>
	<div id="content" class="internas span9">
		<section id="content-section">
			<h1 class="borderHeading">Publicações</h1>
			<?php echo do_shortcode('[wpdreams_ajaxsearchlite]'); ?>
			<div class="tile-list-1">
				<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
						<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
							<div class="tileItem">
								<div class="span10 tileContent">
									<h2 class="tileHeadline">
										<a href="#modal-pub-<?php the_ID(); ?>" data-toggle="modal" style="color: inherit; text-decoration: none;">
											<?php the_title(); ?>
										</a>
									</h2>
									<span class="description">
										<!-- content -->
										<?php // Mostra os primeiros 150 caracteres
											$excerpt = substr( $post->post_excerpt, 0, 300 );
											// Imprime a variável?>
										<?php  echo $excerpt , '...'; ?>
									</span> 
									<!-- lista de categorias -->
									<span class="keywords">
										<?php //get_template_part( 'template-parts/post/post', 'tags'); ?>
										<!-- botão download de arquivo -->
										<div class="text-right">
											<?php
												$upload_file = get_post_meta($post->ID, 'upload_file', true );
												if ( ! empty( $upload_file ) ) {
											?>
													<a href=" <?php echo $upload_file; ?> " target="_blank"  onClick="ga('send', 'event', 'Publicação', 'Download', '<?php the_title(); ?>');">

														<button type="button" class="btn btn-lg btn-success"><i class="icon-download-alt icon-light"></i> DOWNLOAD </button>
													</a>
													<div class="space"></div>
											<?php } ?>
										</div>	
									</span>
								</div>
								<!-- data da publicação -->
								<div class="span2 tileInfo">
									<ul>
										<li class="hide">por Portal Padrão</li>
										<li class="hide">publicado</li>
										<li><i class="icon-fixed-width icon-calendar"></i> <?php the_time('d/m/Y') ?></li>
										<li><i class="icon-fixed-width icon-time"></i><?php the_time('g:i A') ?></li>
										
									</ul>
								</div>
							</div>
						</article>

						<!-- Modal Structure for Publication -->
						<div id="modal-pub-<?php the_ID(); ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="modal-label-<?php the_ID(); ?>" aria-hidden="true" style="width: 80%; margin-left: -40%;">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
								<h3 id="modal-label-<?php the_ID(); ?>"><?php the_title(); ?></h3>
							</div>
							<div class="modal-body" style="max-height: 60vh; overflow-y: auto;">
								<?php
									// Extrair meta dados
									$autores = get_post_meta($post->ID, '_tp_autores', true);
									$revista = get_post_meta($post->ID, '_tp_revista', true);
									$edicao  = get_post_meta($post->ID, '_tp_edicao', true);
									$doi     = get_post_meta($post->ID, '_tp_doi', true);
									$link    = get_post_meta($post->ID, '_tp_link', true);
									
									if ( $autores || $revista || $edicao || $doi || $link ) {
										echo '<div class="publication-meta" style="background: #f9f9f9; padding: 15px; margin-bottom: 20px; border-radius: 5px; font-size: 0.9em; border: 1px solid #eee;">';
										if ($autores) echo '<p style="margin-bottom: 5px;"><strong>Autores:</strong> ' . esc_html($autores) . '</p>';
										if ($revista) echo '<p style="margin-bottom: 5px;"><strong>Revista/Publicação:</strong> ' . esc_html($revista) . '</p>';
										if ($edicao)  echo '<p style="margin-bottom: 5px;"><strong>Edição/Ano:</strong> ' . esc_html($edicao) . '</p>';
										if ($doi)     echo '<p style="margin-bottom: 5px;"><strong>DOI:</strong> ' . esc_html($doi) . '</p>';
										if ($link)    echo '<p style="margin-bottom: 0;"><strong>Link externo:</strong> <a href="' . esc_url($link) . '" target="_blank">' . esc_html($link) . '</a></p>';
										echo '</div>';
									}

									// Show full content
									the_content();
								?>
							</div>
							<div class="modal-footer">
								<?php if ( ! empty( $upload_file ) ) { ?>
									<a href="<?php echo $upload_file; ?>" target="_blank" class="btn btn-success" onClick="ga('send', 'event', 'Publicação', 'Download', '<?php the_title(); ?>');">
										<i class="icon-download-alt icon-light"></i> DOWNLOAD
									</a>
								<?php } ?>
								<button class="btn" data-dismiss="modal" aria-hidden="true">Fechar</button>
							</div>
						</div>
				<?php
				// End the loop.
				endwhile;
				// If no content, include the "No posts found" template.
				else :
					get_template_part( 'content', 'none' );
				endif;
				?>
			</div>
		</section>
	</div>
<?php endif; ?>
