<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>

<?php ob_start(); ?>

<?php if( isset( $_REQUEST['nonce'] ) ) : ?>

	<?php if( wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), 'workshop-warehouse' ) ) : ?>

		<?php
        $paged = 1;
		if( isset( $_REQUEST['entries'] ) )     $paged = intval($_REQUEST['entries']);
		$mode = 'warehouse';
		if( isset( $_REQUEST['mode'] ) )        $mode = sanitize_text_field( wp_unslash( $_REQUEST['mode'] ) );
		$search_title = '';
		if( isset( $_REQUEST['title'] ) )       $search_title = sanitize_text_field( wp_unslash( $_REQUEST['title'] ) );
		$asin = '';
		if( isset( $_REQUEST['asin'] ) )        $asin = sanitize_text_field( wp_unslash( $_REQUEST['asin'] ) );
		$issues = '';
		if( isset( $_REQUEST['issues'] ) )        $issues = sanitize_text_field( wp_unslash( $_REQUEST['issues'] ) );
		$issues_selected = '';
		if($issues == 'Ok') $issues_selected = '!=';
		if($issues == 'Out') $issues_selected = '=';

		$post_per_page = 50;

		$product_filter = array(
			'post_type'   				=> 'amazing_product',
			'posts_per_page'			=> $post_per_page,
			'paged'						=> $paged,
			'no_found_rows' 			=> 'true',
			'update_post_term_cache' 	=> false,  
			'orderby'                   => 'ID',
            'order'                     => 'DESC',
		); 
		$product_filter['tax_query'] = array( 'relation' => 'AND' );
		$i = 1;
		$taxonomy_filters = array();
		foreach(AmazingAffiliates_Admin::$amazingaffiliates_tax_index as $taxonomy){         
			if(empty($_REQUEST[$taxonomy['taxSlug']])) continue;
			$product_filter['tax_query'][$i] = array(
				'taxonomy' => $taxonomy['taxSlug'],
				'field' => 'term_id',
				'terms' => array_map( 'sanitize_key' , wp_unslash( $_REQUEST[$taxonomy['taxSlug']] ) ),
				'include_children' => $taxonomy['taxHierarchical'],
				'operator' => 'AND'
			); 
			$i++;
		}	
		if(!empty($asin)) {
			$product_filter['meta_query'][0] = array(
				'key'     => 'asin',
				'value'   => $asin,
				'compare' => 'LIKE',
			);
		}
		if(!empty($search_title)) {
			$product_filter['meta_query'][1] = array(
				'key'     => 'custom_title',
				'value'   => $search_title,
				'compare' => 'LIKE',
			);
		}
		if(!empty($issues_selected)) {
			$product_filter['meta_query'][2] = array(
				'key'     => 'not_updateable',
				'value'   => 1,
				'compare' => $issues_selected,
			);
		}

		$entries = array();
		$i = 0;

		$product = new WP_Query( $product_filter );

		$product_filter['posts_per_page'] = -1;
		$total_product = new WP_Query( $product_filter );
		
		global $post;
        $product_cache = $post;

		if( $product->have_posts() ) {
			while( $product->have_posts() )	{
				$product->the_post(); 
				$entries[$i++] = get_the_ID();
			}
		}
		else{
			echo 'No results found matching the filters';
		}
		
		$post = $product_cache;
        wp_reset_postdata();
        
		?>

		<div id="latest_products" data-pages="<?php echo intval( ceil( count($total_product->posts)/$post_per_page ) ); ?>" data-paged="<?php echo esc_attr($paged); ?>">
	
			<?php
			if('workshop' == $mode AND ! empty($entries) ) {
				require( AMAZINGAFFILIATES_PLUGIN_URI . '/admin/partials/product_display_workshop.php' );
				require( AMAZINGAFFILIATES_PLUGIN_URI . '/admin/partials/product_display_warehouse.php' );
			}
			if('warehouse' == $mode) require( AMAZINGAFFILIATES_PLUGIN_URI . '/admin/partials/product_display_warehouse.php' );
			if('metabox' == $mode) require( AMAZINGAFFILIATES_PLUGIN_URI . '/admin/partials/product_display_metabox.php' );

			?>
			
		</div>


	<?php endif; ?>

<?php endif; ?>

<?php 

$block = ob_get_clean();

echo wp_json_encode($block);