<?php if ( ! defined( 'ABSPATH' ) ) { die(); }




add_action('wp_ajax__action_odrin_ajax_bookblock_function', '_action_odrin_ajax_bookblock_function');
add_action('wp_ajax_nopriv__action_odrin_ajax_bookblock_function', '_action_odrin_ajax_bookblock_function');

if(!( function_exists('_action_odrin_ajax_bookblock_function') )){

	function _action_odrin_ajax_bookblock_function(){


	    $post_id = $_POST['id'];
	    $output = '';

	    if ( !empty( $post_id) ) {
	    	$args = array(
	    		'post_type' => 'product',
	    		'posts_per_page' => -1,
	    		'p' => $post_id		
			);
	    }

		$the_query = new WP_Query($args);

		if ( $the_query->have_posts() ) :

		$output .= '
		<div id="book-container" class="book-container section-dark">

			<div class="menu-panel">
				<h3>' . __('Table of Contents', 'odrin') . '</h3>
				<ul id="menu-toc" class="menu-toc is-perfect-scrollbar">';

					if( odrin_have_rows('read_the_book_chapters',  $post_id) ):

						$item_num = 1;

					    while ( odrin_have_rows('read_the_book_chapters',  $post_id) ) : the_row();
 
						    $output .= '<li><a href="#item'. $item_num = 1 . '">' . odrin_get_sub_field('title',  $post_id) .'</a></li>';

							$item_num++;

					    endwhile;

					endif;

				$output .= '</ul>

			</div>

			<div class="bb-custom-wrapper">
				<div id="bb-bookblock" class="bb-bookblock">';

					
					if( odrin_have_rows('read_the_book_chapters',  $post_id) ):

						$item_num = 1;

					    while ( odrin_have_rows('read_the_book_chapters',  $post_id) ) : the_row();
 
						    $output .= '<div class="bb-item" id="item' . $item_num . '">
								<div class="book-content">
									<div class="book-content-inner special-first-letter is-perfect-scrollbar">
										<h1 class="chapter-heading">' . odrin_get_sub_field('title',  $post_id) . '</h1>'
										. odrin_get_sub_field('chapter',  $post_id) .
									'</div>
								</div>
							</div><!-- end bb-item -->';

							$item_num++;

					    endwhile;

					endif;


				$output .= '</div>

				<nav>
					<span id="bb-nav-prev">&larr;</span>
					<span id="bb-nav-next">&rarr;</span>
				</nav>

				<span id="tblcontents" class="menu-button"><i class="icon-search"></i><span>' . __('Show Chapters', 'odrin') . '</span></span>

				<span class="bb-nav-close"><i class="close-icon-color"></i></span>

			</div>

		</div><!-- /container -->';

	endif;
	wp_reset_postdata();

	global $allowedposttags;

	$expanded_allowedtags = $allowedposttags;

	$expanded_allowedtags['input'] = array(
			'class' => true,
			'id'    => true,
			'name'  => true,
			'value' => true,
			'type'  => true,
	);

	echo wp_kses($output, $expanded_allowedtags);
	// echo wp_kses_post($output);

	die();
	}
}