<?php

	// Loops: Offer
	function loop_offer($postID, $layout = null){
	?>

		<article id="post-<?php the_ID(); ?>" class="offer offerCard">
			<a href="<?php the_permalink(); ?>" class="">
				<div class="thumbOffer">
					<?php if(has_post_thumbnail()): ?>
						<?php the_post_thumbnail( 'thumb-post-medium' ); ?>
					<?php endif; ?>
				</div>
				<div class="contentCardOffer">
					<div class="topCard">
						<div class="storeLogo"><img src="<?php the_field('store_logo'); ?>" alt="<?php the_field('store_name'); ?>" /></div>
						<?php
							$terms = get_the_terms( $postID, 'category_offer' );
						?>
						<span class="category"><?php echo $terms[0]->name; ?></span>
						<span class="boxOff"><?php the_field('offer'); ?>%OFF</span>
						<span class="after">$<?php the_field('value_after'); ?></span>
					</div>
					<header><h2 class="h4"><?php echo countText(get_the_title(), 66) ?></h2></header>
					<p class="btOff">
						<span class="text">I whant this</span>
						<span class="price">$<?php the_field('value_after'); ?></span>
					</p>
				</div>
			</a>
		</article>

		
	<?php  } // loop_joblisting


	// Loops: job / usar na ordem cronológica e no load more
	function loop_joblisting($postID, $layout = null){
		
		$job_company = esc_html(get_post_meta($postID, 'job_company', true));
		$job_article = esc_html(get_post_meta($postID, 'job_article', true));
		$job_position = esc_html(get_post_meta($postID, 'job_position', true));
		$job_expiration = esc_html(get_post_meta($postID, 'job_expiration', true));
		$job_place = wp_get_post_terms($postID, 'location', array('fields' => 'all'));
		$job_place = $job_place[0]->name;
		if(has_post_thumbnail($postID)) $image = get_the_post_thumbnail_url($postID);

		$classlayout = ($layout == 'small') ? '--small' : '';
		$classApplied = (is_appliedjob($postID)) ? '--active' : '';
		$pos_article = ($job_article) ? $job_article . ' ' : ''; ?>

		<div class="career-bar --white <?php echo $classlayout .' '. $classApplied; ?>">
			<div class="career-bar__wrapper container">
				<div class="career-bar__col d-flex align-items-center">
					<a class="career-bar__img" href="<?php the_permalink($postID); ?>" style="background-image:url('<?php echo $image; ?>')"></a>
					<a class="career-bar__description" href="<?php the_permalink($postID); ?>"><strong><?php echo $job_position; ?></strong> - <span><?php echo $job_place; ?></span></a>
				</div>
				<div class="career-bar__col d-none d-md-flex align-items-center">
					<time class="career-bar__date"><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) .' ago'; ?></time>
					<div class="c-social__book is-gray" aria-label="Bookmark">
						<?php the_favorites_button($postID, get_current_blog_id()); ?>
					</div>
					<a href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink($postID); ?>" target="_blank" class="btn-circle --grey"><i class="fab fa-facebook-f"></i></a>
					<a href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink($postID); ?>" target="_blank" class="btn-circle --grey"><i class="fab fa-twitter"></i></a>
				</div>
			</div>
		</div>

		
	<?php  } // loop_joblisting

	// Loops: offer / usar na ordem cronológica e no load more
	function loop_offerlisting($postID, $amout = null){
		
		$offer_place = wp_get_post_terms($postID, 'location', array('fields' => 'all'));
		$offer_place = $offer_place[0]->name;
		$offer_description = esc_html(get_post_meta($postID, 'offer_description', true));
		$offer_fullprice = esc_html(get_post_meta($postID, 'offer_fullprice', true));
		$offer_discount = esc_html(get_post_meta($postID, 'offer_discount', true));
		$offer_price = esc_html(get_post_meta($postID, 'offer_price', true));
		$offer_expiration = esc_html(get_post_meta($postID, 'offer_expiration', true));
		$offer_category = wp_get_post_terms($postID, 'offer-category', array('fields' => 'all'));
		$offer_company = esc_html(get_post_meta($postID, 'offer_company', true));

		if($offer_description == ''){
			$offer_description = get_the_excerpt($postID);
			$offer_description = substr($offer_description, 0, 130) . '...';
		}

		$expiration = remainingTime($offer_expiration);
		if($expiration < 0){
			$expirationText = 'Deal expired';
		} elseif($expiration == 0) {
			$expirationText = 'Deal ends <strong>today</strong>';
		} elseif($expiration > 0) {
			$expirationText = 'Ends within <strong>'. $expiration .' days</strong>';
		}

		if(has_post_thumbnail($postID)){
			$image = get_the_post_thumbnail_url($postID);
		} else {
			$gallery = get_post_meta($postID, 'post_gallery_id', true);
			if($gallery){
				$image = wp_get_attachment_url($gallery[0]);
			} else {
				$image = get_bloginfo('template_directory') . '/img/content/nothing.png';
			}
		}

		$classAmout = ($amout == '3') ? 'col-lg-4' : 'col-lg-3'; ?>

		<div class="col-md-6 <?php echo $classAmout; ?>">
			<div class="offer-card">
				<a href="<?php the_permalink($postID); ?>">
					<div class="offer-card__image">
						<div class="offer-card__image-thumb" style="background-image:url('<?php echo $image; ?>');"></div>
					</div>
					<time class="offer-card__date"><i class="far fa-clock"></i><?php echo $expirationText; ?></time>
					<h3 class="offer-card__title"><?php echo get_the_title($postID); ?></h3>
					<span class="offer-card__location"><?php echo $offer_place; ?></span>
					<p class="offer-card__excerpt"><?php echo $offer_description; ?></p>
					<span class="offer-card__off"><?php echo $offer_discount; ?>% OFF</span>

					<?php /*<span class="offer-card__price">$<?php echo $offer_fullprice; ?></span>
					<span class="offer-card__new-price">$<?php echo $offer_price; ?></span>*/ ?>
				</a>
			</div>
		</div>

		
	<?php } // loop_offerlisting

	function loop_genericcard($postID, $amout = null, $pagina = null){
		foreach((get_the_category($postID)) as $category){
			$parentcategory = $category->name;
			$catslug = $category->slug;
		}

		$subcategory = wp_get_post_terms($postID, 'sub-category', array('fields' => 'all'));
		$subcategoryName = '';
		if($subcategory) $subcategoryName = $subcategory[0]->name;

		$category = ($subcategoryName) ? $subcategoryName : $parentcategory;

		if(has_post_thumbnail($postID)){
			$image = get_the_post_thumbnail_url($postID);
		} else {
			$image = catch_that_image($postID);
		} ?>

		<article class="post-featured-horizontal">
			<div class="row">
				<a href="<?php the_permalink($postID); ?>" class="post-featured-horizontal__thumb">
					<div class="post-featured-horizontal__img">
						<div class="post-featured-horizontal__img-thumb" style="background-image: url(<?php echo $image; ?>)"></div>
					</div>
				</a>
				<div class="post-featured-horizontal__text">
					<a href="<?php the_permalink($postID); ?>" class="label --transparent"><?php echo $category; ?></a>
					<nav class="post-featured-horizontal__nav">
						<div class="c-social__book is-small is-gray" aria-label="Bookmark">
							<?php the_favorites_button($postID, get_current_blog_id()); ?>
						</div>
						<a href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>" target="_blank" class="btn-circle --grey"><i class="fab fa-facebook-f"></i></a>
						<a href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink(); ?>" target="_blank" class="btn-circle --grey"><i class="fab fa-twitter"></i></a>
					</nav>
					<a href="<?php the_permalink($postID); ?>">
						<h3 class="post-featured-horizontal__title"><?php echo get_the_title($postID); ?></h3>
						<p class="post-featured-horizontal__description"><?php echo limit_text(get_the_excerpt($postID), 20); ?></p>
					</a>
					<time class="post-featured-horizontal__date"><?php echo get_the_time('M d', $postID); ?> | BY <strong><?php echo get_the_author_meta('display_name'); ?></strong></time>
				</div>
			</div>
		</article>
	<?php } //loop_genericcard

	function loop_redeemedoffer($postID){
		$offer_place = wp_get_post_terms($postID, 'location', array('fields' => 'all'));
		$offer_place = $offer_place[0]->name;
		$offer_description = esc_html(get_post_meta($postID, 'offer_description', true));
		$offer_code = esc_html(get_post_meta($postID, 'offer_code', true));
		$offer_expiration = esc_html(get_post_meta($postID, 'offer_expiration', true));
		$offer_category = wp_get_post_terms($postID, 'offer-category', array('fields' => 'all'));
		$offer_company = esc_html(get_post_meta($postID, 'offer_company', true));

		if($offer_description == ''){
			$offer_description = get_the_excerpt($postID);
			$offer_description = substr($offer_description, 0, 130) . '...';
		}

		$expiration = remainingTime($offer_expiration);
		$dealExpired = false;
		if($expiration < 0){
			$dealExpired = true;
			$expirationText = 'Deal expired';
		} elseif($expiration == 0) {
			$expirationText = 'Deal ends <strong>today</strong>';
		} elseif($expiration > 0) {
			$expirationText = 'Ends within <strong>'. $expiration .' days</strong>';
		}

		if(has_post_thumbnail($postID)){
			$image = get_the_post_thumbnail_url($postID);
		} else {
			$gallery = get_post_meta($postID, 'post_gallery_id', true);
			if($gallery){
				$image = wp_get_attachment_url($gallery[0]);
			} else {
				$image = get_bloginfo('template_directory') . '/img/content/nothing.png';
			}
		} ?>

		<div class="col-md-6 col-lg-3 mb-5">
			<div class="offer-card">
				<a href="<?php the_permalink($postID); ?>">
					<div class="offer-card__image" style="background-image:url('<?php echo $image; ?>');"></div>
					<time class="offer-card__date"><i class="far fa-clock"></i><?php echo $expirationText; ?></time>
					<h3 class="offer-card__title"><?php echo get_the_title($postID); ?></h3>
					<span class="offer-card__location"><?php echo $offer_place; ?></span>
					<p class="offer-card__excerpt"><?php echo $offer_description; ?></p>
				</a>

				<?php if(!$dealExpired) : ?>
					<div class="btn-code">
						<input type="text" class="btn-code__code" value="<?php echo $offer_code; ?>">
						<span class="btn-code__overlay"></span>
						<span class="btn-code__label">copy</span>
					</div>
				<?php else : ?>
					<div class="btn-code --disable">
						<input type="text" class="btn-code__code" value="<?php echo $offer_code; ?>">
						<span class="btn-code__overlay"></span>
					</div>
				<?php endif; ?>
			</div>
		</div>
	<?php } //loop_redeemedoffer

	// Loops: event / usar na ordem cronológica e no load more
	function loop_eventlisting($postID, $amout = null){
		
		$event_date = esc_html(get_post_meta($postID, 'event_date', true));
		$event_day = date('d', strtotime($event_date));
		$event_month = date('M', strtotime($event_date));

		$event_place = wp_get_post_terms($postID, 'location', array('fields' => 'all'));
		$event_place = $event_place[0]->name;
		$event_description = esc_html(get_post_meta($postID, 'event_description', true));
		$event_category = wp_get_post_terms($postID, 'event-category', array('fields' => 'all'));

		if($event_description == ''){
			$event_description = get_the_excerpt($postID);
			$event_description = substr($event_description, 0, 130) . '...';
		}

		if(has_post_thumbnail($postID)){
			$image = get_the_post_thumbnail_url($postID);
		} else {
			$gallery = get_post_meta($postID, 'post_gallery_id', true);
			if($gallery){
				$image = wp_get_attachment_url($gallery[0]);
			} else {
				$image = get_bloginfo('template_directory') . '/img/content/nothing.png';
			}
		}

		$classAmout = ($amout == '3') ? 'col-lg-4' : 'col-lg-3'; ?>

		<div class="col-md-6 <?php echo $classAmout; ?>">
			<div class="event-card">
				<a href="<?php the_permalink($postID); ?>">
					<div class="event-card__image">
						<div class="event-card__image-thumb" style="background-image:url('<?php echo $image; ?>');"></div>
						<span class="label --transparent"><?php echo $event_category[0]->name; ?></span>
						<time class="event-card__date"><?php echo $event_month; ?> <strong><?php echo $event_day; ?></strong></time>
					</div>
					<h3 class="event-card__title"><?php echo get_the_title($postID); ?></h3>
					<span class="event-card__location"><?php echo $event_place; ?></span>
					<p class="event-card__excerpt"><?php echo $event_description; ?></p>
				</a>
			</div>
			<hr class="d-block d-md-none">
		</div>

		
<?php  } // loop_eventlisting

	// Loops: video / usar na ordem cronológica e no load more
	function loop_videolisting($postID, $listing = null){
		
		foreach((get_the_category($postID)) as $category){
			$parentcategory = $category->name;
			$catslug = $category->slug;
		}

		if(has_post_thumbnail($postID)){
			$image = get_the_post_thumbnail_url($postID);
		} else {
			$image = catch_that_image($postID);
		}

		if($listing == 'yes'){
			$listing_top = '<div class="col-md-6 col-lg-3">';
			$listing_bottom = '</div>';
		} else {
			$listing_top = '';
			$listing_bottom = '';
		}
	?>
		<?php echo $listing_top; ?>
			<div class="post-video">
				<a href="<?php the_permalink($postID); ?>">
					<div class="post-video__thumb">
						<div class="post-video__thumb-img" style="background-image: url(<?php echo $image; ?>)"></div>
						<span class="post-video__icon"><i class="fas fa-play"></i></span>
					</div>
					<span class="label --transparent"><?php echo $parentcategory; ?></span>
					<time class="post-video__time"><?php echo human_time_diff(get_the_time('U', $postID), current_time('timestamp')) .' ago'; ?></time>
					<h3 class="post-video__title"><?php echo get_the_title($postID); ?></h3>
				</a>
			</div>
		<?php echo $listing_bottom; ?>
		<!-- ADS -->
	<?php } // loop_videolisting
?>
