<?php 
add_action( 'add_meta_boxes', 'ayi_add_custom_box', 1  );
function ayi_add_custom_box() {
	global $post;
	global $current_user;
		add_meta_box( 
			'product_search',
			__( 'Add Products', 'wl' ),
			'ayi_table_editor',
			'post',
			'advanced'
		);	

}


function ayi_table_editor(){
	global $post;

	?>
	
	<?php	
	$out .= '
<input type="hidden" id="video_id" value="'.get_post_meta( $post->ID, 'youtube_id', true ).'" /><input type="hidden" id="video_descr" value="'.esc_html( get_post_meta( $post->ID, 'youtube_descr', true ) ).'" />

	
	
<div class="tw-bs">';

	$out .= ayi_check_api();	
	


	$out .= '
	<div class="form-horizontal" method="post" action="">
	'.wp_nonce_field('post_import_action','post_import_nonce', true, false);  
	$config = get_option('api_import'); 
	
	$out .= '
	<fieldset>  		
			<div class="control-group">  
				<label class="control-label" for="optionsCheckbox">Keyword</label>  
				<div class="controls">  
				  <label class="">  
				   <input type="text" class="input-xlarge" name="keyword" id="keyword" value=""> 
				  </label>  
				</div>  
			  </div>
			  <div class="form-actions">  
				<button type="button" onClick="get_products_table()" class="btn btn-primary">Search Products</button> 
<img class="status_bar" src="'.plugins_url('/images/loader.gif', __FILE__ ).'" />				
			  </div>  
			</fieldset>  
	</div>';
	
	


$out .= '
<div class="form-horizontal" method="post" action="">';
echo wp_nonce_field('post_import_list_action','post_import_list_nonce', true, false);  
$config = get_option('api_import'); 



$out .= '

		  <table class="table  table-striped marg_top_40 post_data">  
        <thead>  
          <tr>  
            <th><input type="checkbox" id="check_all"   /></th>  
			<th>Icon</th>
            <th>Title</th>  
          </tr>  
        </thead>  
        <tbody class="table_body">		
        </tbody>  

      </table>  

	<div class="form-horizontal post_data" method="post" action="">

		<fieldset>  		
			 <div class="form-actions">  
			<button type="button"  class="btn btn-primary add_products">Add Products</button> 			
			 </div>  
		</fieldset>  
	</div>

</div>';

	
	
$out .= '	
</div>


<script>
	 function get_products_table(){

		var data = {
  			action: "get_product_action",
			s: jQuery("#keyword").val(),			
  			security: \''.wp_create_nonce("filter_nonce").'\'
  		};
  				  jQuery.ajax({url: \''.get_option('home').'/wp-admin/admin-ajax.php\',
  					  type: "POST",
  					  data: data,            
  					  beforeSend: function(msg){
						jQuery(".status_bar").fadeIn();
  					   },
  						success: function(msg){
							jQuery(".status_bar").fadeOut();
							jQuery(".post_data").fadeIn();
							
							jQuery(".table_body").html( msg );

						} , 
  					  error:  function(msg) {
  						alert("Error Saved!!: " + msg);
  					  }          
  			  });
        }
		</script>

	';	
	echo $out;
}


?>