<?php
/**
 * @package sparkling
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<?php //the_post_thumbnail( 'sparkling-featured', array( 'class' => 'single-featured' )); ?>
	<div class="post-inner-content">
		<div class="container">
			<section class="custom-product-container custom-category-list">
				<?php 
						// $prodcut_count++;
						// $class= ($prodcut_count%3==0) ? 'row_last' :'';
						// $product_list2.='<div class="mc_product '.$class.'">';
		    //     		$product_list2.='<div class="mc_product_wrapper">';
		    //     		$product_list2.='<a href="'.get_permalink().'">';
		    //     		if ( has_post_thumbnail() ){
		    //     			$imageid = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
						// 	$featimage = $imageid['0'];
		    //     			$product_list2.='<div class="sub_product_img"><img src="'.$featimage.'"/></div>';
		    //     		}else{
		    //     			$product_list2.='<div class="sub_product_img"><img src="'.get_template_directory_uri().'/images/no-image.png"/></div>';
		    //     		}
		        		
		    //     		$product_list2.='<div class="sub_product_info"><div class="sub_product_name">'. get_the_title().'</div>';

		    //     		$short_desc = types_render_field("short-description", array("output"=>"text"));
		    //     		if (!empty($short_desc)){
		    //     			$product_list2.='<div class="product-short-desc">('.$short_desc.')</div>';
		    //     		}
		        		
		    //     		$product_list2.='</div></a>';
		    //     		$product_list2.='</div>';
		    //     		$product_list2.='</div>';
				?>

				<div class="page-left">
					<h3>Product Information</h3>
					<?php echo do_shortcode ('[wpb_category_accordion taxonomy="product-category" orderby="id" order="ASC" show_count="no" hide_empty="no"]'); ?>
					<div style="height: 50px; clear:both;"></div>
				</div>

				<div class="right-content sub-cat-list">
					<h1 class="my-page-title">
						<span class="my-title"><?php the_title();?></span>
						<span class="my-deco"></span>
					</h1>

					<div class="my_product_list">
						<?php //echo $product_list2; ?>
						<div class="my_product">
							<?php
								if ( has_post_thumbnail() ){
				        			$imageid = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
									$featimage = $imageid['0'];
				        			echo '<div class="my_product_img"><img src="'.$featimage.'"/></div>';
				        		}else{
				        			echo '<div class="my_product_img"><img src="'.get_template_directory_uri().'/images/no-image.png"/></div>';
				        		}
							?>
							<div class="my_product_info">
								<?php the_content(); ?>
							</div>
						</div>
						
					</div>

					<div class="home-redirect">
						<a href="#" class="btn btn-link btn-lg">Directions for Use of Products <i class="fa fa-angle-double-right" aria-hidden="true"></i></a>
						<a href="#" class="btn btn-link btn-lg">Product Safety Guide <i class="fa fa-angle-double-right" aria-hidden="true"></i></a>
					</div>

					<div class="my-product-intro">
						<div class="my-box">
							<div class="left-col">
								<span class="my-txt">These are examples of products.<br>To inquire about other products, please click here.</span>
								<span class="left-border"></span>
							</div>
							<div class="right-col">
								<a class="btn btn-default" href="<?php get_template_directory_uri(); ?>/questions"><i class="fa fa-chevron-right" aria-hidden="true"></i> Questions</a>
							</div>
						</div>
					</div>

					<?php
						// $current_id = $_REQUEST["myid"];
						// $myterm = get_term_by('id', $current_id, 'product-category');
						$myterms = get_the_terms( $post->ID, 'product-category' );
						if ( !empty( $myterms ) ){
						    // get the last term
						    $myterm = end( $myterms );
						    
						}else{
							$myterm = "Products";
						}

						$current_id = $myterm->term_id;

						$args = array(
							'post_type' => 'product',
							'$post_count' => '100',
							'orderby' => 'name',
							'tax_query' => array(
						      array(
						      'taxonomy' => 'product-category',
						      'field' => 'id',
						      'terms' => $current_id
						      )
					       	)
						);

						$related_list ="";
						$prodcut_count="";

						$query = new WP_Query( $args ); 
						if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
							$prodcut_count++;
							$class= ($prodcut_count%3==0) ? 'row_last' :'';
							if ($prodcut_count%3==1){
								$related_list.='<div class="related_product_row">';
							}
							$related_list.='<div class="related_product '.$class.'">';
							$related_list.='<div class="sub_product_name" ><a href="'.get_permalink().'">'.get_the_title().'</a></div>';
							$short_desc = types_render_field("short-description", array("output"=>"text"));
			        		if (!empty($short_desc)){
			        			$related_list.='<div class="product-short-desc">('.$short_desc.')</div>';
			        		}
							$related_list.='</div>';

							if ($prodcut_count%3==0){
								$related_list.='</div>';
							}
						endwhile;

						else:
						  echo "no product";
						endif;
					?>

					<div class="my-product-related">
						<h3>Other series of <?php echo $myterm->name; ?></h3>
						<div class="product-related-list">
							<div class="product-related-wrapper">
								<?php echo $related_list; ?>
							</div>
						</div>
					</div>

				</div>

			</section>
		</div>

		<footer class="entry-meta">

	    	<?php if(has_tag()) : ?>
	      <!-- tags -->
	      <div class="tagcloud">

	          <?php
	              $tags = get_the_tags(get_the_ID());
	              foreach($tags as $tag){
	                  echo '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a> ';
	              } ?>

	      </div>
	      <!-- end tags -->
	      <?php endif; ?>

		</footer><!-- .entry-meta -->
	</div>

	<?php if (get_the_author_meta('description')) :  ?>
		<div class="post-inner-content secondary-content-box">
      <!-- author bio -->
      <div class="author-bio content-box-inner">

        <!-- avatar -->
        <div class="avatar">
            <?php echo get_avatar(get_the_author_meta('ID') , '60'); ?>
        </div>
        <!-- end avatar -->

        <!-- user bio -->
        <div class="author-bio-content">

          <h4 class="author-name"><a href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>"><?php echo get_the_author_meta('display_name'); ?></a></h4>
          <p class="author-description">
              <?php echo get_the_author_meta('description'); ?>
          </p>

        </div><!-- end .author-bio-content -->

      </div><!-- end .author-bio  -->

		</div>
		<?php endif; ?>

</article><!-- #post-## -->
