<?php
//template name: Informação para produto

if ( have_posts() ) while( have_posts() ) : the_post();
	if(get_field("info_opt") == "text_info"){
		get_header(); 
		?>
		<div class="container full page-std">
			<div class="container center title-tab-wrapper">
				<div class="main-col left ">
				</div>
		        <div class="main-col right">
					<div class="title-tab"><h1><?php the_title() ?></h1></div>
				</div>
			</div>
		    <div class="container center page-std-content">
				<?php 
				$current_post_parents = get_post_ancestors();
				$current_id = get_the_ID();
				$post_parent = end($current_post_parents); 
				if($post_parent){
				    $top_level_parent = is_object($post_parent) ? $post_parent->ID : $post_parent;
				    $title = get_the_title($top_level_parent);
				}else{
				    $top_level_parent = $post->ID;
				    $title = $post->post_title;
				    $post_parent_id = $post->ID;
				}
				if($top_level_parent == 4){
					get_sidebar(); 
				}else{
					?>
			    	<div class="main-col left sidebar">
			    		<?php
				    		$current_post_parents = get_post_ancestors();
				    		$current_id = get_the_ID();
				    		$post_parent = end($current_post_parents);
							if($post_parent){
						        $top_level_parent = is_object($post_parent) ? $post_parent->ID : $post_parent;
						        $title = get_the_title($top_level_parent);
						    }else{
						        $top_level_parent = $post->ID;
						        $title = $post->post_title;
						        $post_parent_id = $post->ID;
						    }
						    // WP_Query arguments
						    $args = array (
						        'post_parent'            => $top_level_parent,
						        'post_type'              => 'page',
						        'post_status'            => 'publish',
						        'orderby'                => 'menu_order',
						        'order'                  => 'asc'
						    );

						    // The Query
						    $query = new WP_Query( $args );

						    // The Loop
						    if ( $query->have_posts() ) {
						        ?>

						        <div class="parent-title">
						        	<h3><?php echo $title; ?></h3>
									<svg class="arrow" x="0px" y="0px" width="30.4px" height="17.5px" viewBox="0 0 30.4 17.5" enable-background="new 0 0 30.4 17.5">
										<path fill="#939598" d="M15.2,17.5c0.6,0,1.2-0.2,1.7-0.7L29.8,3.9c0.9-0.9,0.9-2.4,0-3.2s-2.4-0.9-3.2,0l-11.3,11L3.9,0.7
										C3-0.2,1.6-0.2,0.7,0.7c-0.9,0.9-0.9,2.4,0,3.2l12.9,12.9C14,17.3,14.6,17.5,15.2,17.5z"/>
									</svg>
						        </div>
						        <nav class="side-menu">
						            <ul>
						                <?php
						                while ( $query->have_posts() ) {
						                    $query->the_post();
						                    $args_sub = array (
						                        'child_of'               => get_the_ID(),
						                        'parent'				 => get_the_ID(),
						                        'post_type'              => 'page',
						                        'post_status'            => 'publish',
						                        'sort_column'                => 'menu_order',
						                        'order'                  => 'asc'
						                    );
						                    $children = get_pages($args_sub);
					                        ?>
					                        <li id="item_<?php echo get_the_ID(); ?>" class="<?php echo count( $children ) != 0 ? "has-children" : "" ;?> <?php echo in_array(get_the_ID(), $current_post_parents) ? "active-parent" : "";?> <?php echo $current_id == get_the_ID() ? "active" : ""; ?>">
					                            <a href="<?php the_permalink(); ?>"><div class="anim-fast"><?php the_title(); ?></div></a>
					                            <?php
					                            if( count( $children ) != 0 ){
					                                ?>
					                                <ul>
					                                    <?php
					                                    foreach ($children as $child_page) {
					                                    	$args_sub_sub = array (
										                        'child_of'               => $child_page->ID,
						                        				'parent'				 => $child_page->ID,
										                        'post_type'              => 'page',
										                        'post_status'            => 'publish',
										                        'sort_column'                => 'menu_order',
										                        'order'                  => 'asc'
										                    );
										                    $children_sub = get_pages($args_sub_sub);
				                                            ?>
				                                            <li id="item_<?php echo $child_page->ID; ?>" class="<?php echo $current_id == $child_page->ID ? "active" : ""; ?>">
				                                                <a href="<?php echo get_permalink($child_page); ?>"><div class="anim-fast"><?php echo $child_page->post_title; ?></div></a>
				                                                <?php
				                                                if( count( $children_sub ) != 0 ){
				                                               		?>
					                                				<ul>
					                                					<?php
				                                    					foreach ($children_sub as $child_sub_page) {
				                                    						?>
				                                    						<li id="item_<?php echo $child_sub_page->ID; ?>" class="<?php echo $current_id == $child_sub_page->ID ? "active" : ""; ?>">
					                                                			<a href="<?php echo get_permalink($child_sub_page); ?>"><div class="anim-fast"><?php echo $child_sub_page->post_title; ?></div></a>
					                                                		</li>	
					                                                		<?php
				                                    					}
				                                    					?>
																	</ul>
					                                			<?php
				                                                }
				                                                ?>
				                                            </li>
				                                            <?php
					                                    }
					                                    ?>
					                                </ul>
					                                <?php
					                            }
					                            ?>
					                        </li>
					                        <?php  
						                }
						                ?>
						            <ul>
						        </nav>
						        <?php
						    }

						    // Restore original Post Data
						    wp_reset_postdata();
			    		?>
					</div>
					<?php
				}
				?>
		        <div class="main-col right">
		        	<div class="breadcrumbs">
		<?php if(function_exists('bcn_display'))
		    {
		        bcn_display();
		    }?>
		        	</div>
			        <div class="text content">
			            <?php the_content(); ?>
			        </div>
		        </div>
		    </div>
		</div>
		<?php 
		get_footer();

	}elseif(get_field("info_opt") == "pdf_info"){
		$pdf_url = get_field("info_pdf_c");
		wp_redirect($pdf_url);
	}elseif(get_field("info_opt") == "img_info"){
		$img_url = get_field("info_img_c");
		echo '<img src="'.$img_url.'"/>';
	}

endwhile;
?>