<?php
/*****************************************************************************************************************************************************
******************************************************************************************************************************************************

																		 Nav Search
																		
*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function reza_nav_search() {
  	
  	
	$class_align='rd-nav-'.reza_alignment(reza_option('searchform_align'));
  	$class_layout='rd-nav-search-'.reza_option('searchform_layout');
	$class_width='';
	if(reza_option('searchform_layout')=='fixed-text' || reza_option('searchform_layout')=='fixed-icon'){
  		$class_width = 'rd_col_'.str_replace("-","_",reza_option('searchform_width'));
	}
	$category = reza_option('searchform_category');
	$class_woo= function_exists ( "is_woocommerce" ) && $category!=='hide'? 'rd-search-woo':'';
   	$classes = array(
 		'rd-nav-search',
  		$class_align,
  		$class_layout ,
  		$class_width ,
 		$class_woo,
   	);
	  
   ?>
 
	 
	<div class="<?php echo esc_attr(join( ' ', $classes ));?>">   
    
		<li>
		<?php  
		 if(reza_option('searchform_layout')=='dropdown-text' || reza_option('searchform_layout')=='dropdown-icon'  ){?>
			<a></a>
			<ul class="rd-drop">
			<div class="rd-search-warp">
		<?php } ?>
        
			<form method="get" class="rd-search" action="<?php echo esc_url(home_url( '/') );?>">
            
				<?php 
 				if ( function_exists ( "is_woocommerce" ) &&  $category!=='hide'){
					$product_cat =reza_nav_search_product_cat();
					?>
                    
					<select name="product_cat" >
						<?php if(!empty($product_cat) && is_array($product_cat) ){?>
						<?php foreach($product_cat as $key=> $value){ ?>
                                     <option  value="<?php echo esc_attr($key);?>" ><?php echo esc_html($value);?></option>
						<?php } ?>
						<?php } ?>
					</select>
				<input type="hidden" name="post_type" value="product">
				<?php } ?>
                          
                          
 				<input type="text" name="s"  value=""autocapitalize="none" autocomplete="off" autocorrect="off"   placeholder="<?php echo reza_t('searchitem');?>" />
 						
				<?php if(reza_option('searchform_layout')=='dropdown-text' || reza_option('searchform_layout')=='fixed-text'){?>
					<button type="submit" name="btnSubmit"  ><?php echo reza_t('search');?></button>
				<?php }else{?>
					<button type="submit" name="btnSubmit" ></button>
				<?php } ?>
                
				
				<?php if ( function_exists ( "is_woocommerce" )) {?><div class="rd-search-ajax-close"></div><?php }?>

                 	
			</form>
			
			
			<?php if ( function_exists ( "is_woocommerce" ) ) {?><div class="rd-search-ajax rd-search-drop"></div><?php }?>

                
		<?php if(reza_option('searchform_layout')=='dropdown-text' || reza_option('searchform_layout')=='dropdown-icon'  ){ ?>
			</div>	 
			</ul>	 
		<?php }?>
        
        
            
		</li> 
	</div>
<?php   

}

/*****************************************************************************************************************************************************
******************************************************************************************************************************************************

																		 Nav Search Product Cat
																		
*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function reza_nav_search_product_cat() {

 	$options_product = array();
	$options_product_obj = get_categories('taxonomy=product_cat&type=product&hide_empty=0');
	$options_product['']=  reza_t('category');  
	if(!empty($options_product_obj) && is_array($options_product_obj) ){
 	foreach ($options_product_obj as $product) {
    	$options_product[$product->slug] = $product->cat_name;
	}
	}
	return $options_product;
}
/*****************************************************************************************************************************************************
******************************************************************************************************************************************************

																		 Nav Mobile Search
																		
*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function reza_nav_mobile_search() {
	$class_layout='rd-nav-search-'.reza_option('searchform_layout');
  	?>
   	<div class="rd-nav-mobile-search">
		<li><a></a></li>
        
		<?php if(!empty(reza_ismobile())){?>
            <div class="rd-nav-search <?php echo esc_attr($class_layout);?>">
        
                <form method="get" class="rd-search" action="<?php echo esc_url(home_url( '/') );?>">
                             
                    <input type="text" name="s"  value="" autocapitalize="none" autocomplete="off" autocorrect="off"  placeholder="<?php echo reza_t('searchitem');?>" />
                               
					<input type="hidden" name="post_type" value="product">
                              
                    <?php if(reza_option('searchform_layout')=='dropdown-text' || reza_option('searchform_layout')=='fixed-text'){?>
                        <button type="submit" name="btnSubmit"  ><?php echo reza_t('search');?></button>
                    <?php }else{?>
                        <button type="submit"   name="btnSubmit" ></button>
                    <?php } ?>
				
					
					<?php if ( function_exists ( "is_woocommerce" )) {?><div class="rd-search-ajax-close"></div><?php }?>
                 	
			</form>
			
			<?php if ( function_exists ( "is_woocommerce" ) ) {?><div class="rd-search-ajax rd-search-drop"></div><?php }?>
           
           
            </div>
		<?php }?>
	</div>
<?php
}

add_action('wp_ajax_nopriv_reza_search_ajax', 'reza_search_ajax');
add_action('wp_ajax_reza_search_ajax', 'reza_search_ajax');
function reza_search_ajax() {
	if ( function_exists ( "is_woocommerce" ) ) {
		$option=array();
		$option['post_type']='product';
		$option['image_size']='reza_thumbnail';
		$option['search']=reza_data('','search');
		$option['number']='8';
		$layout = 'list';
		$classes = array(
			'rd-el-search-ajax',
  			'woocommerce',
 			'rd-gap-20px',
 			'rd-'.reza_option('product_ratio'),
 			'rd-none',
			'rd_img_width_10',
  	
		);
 		ob_start();
		?>
		
		 <aside  class="<?php echo esc_attr(join( ' ', $classes ));?> "  >
   			<div class="rd-gap-warp">
                <div class="rd-item-list">
				 
					<?php
					$query = reza_query($option);
					if( $query->have_posts() ) : 
					while ( $query->have_posts() ) : $query->the_post();
					global $post;
						  
						  
						  
						$thumbnail= wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'reza_thumbnail' );
						$post_thumbnail =!empty($thumbnail[0])?'':'rd-not-thumb';
				
				  		?>
                       
                        	<div class="rd-item rd-module-1 <?php echo esc_attr($post_thumbnail);?>">
                        	<div class="rd-post-product product" >
					   
									
								<?php  reza_product_thumb($option);?>
									 
									 
                                <div class="rd-details">
                                    <?php
									reza_product_post_title($option);   
                                    reza_product_category_search($option);
                                     ?>
                                </div>
        
                            </div> 
                            </div>   
						  
						  
					<?php	  
					endwhile; 
					else:?>
					<div class="rd-no-results">
                	<?php echo reza_t('noresults');	?>
                    </div>
					
					<?php
					
					endif;
					?>
					
				</div>
  				 
 			</div>
		</aside> 
  <?php
	}
}
/*****************************************************************************************************************************************************
******************************************************************************************************************************************************
 
																	Product Category
 
*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function reza_product_category_search($category_layout=false) {
	$terms = wp_get_post_terms(get_the_ID(),"product_cat");
 	
	$class='';
 
   
	
	
 	$count=0;
 	if(!empty($terms)){
	?>
 	<div class="rd-product-category">
	<?php foreach ( $terms as $term ) { $count++; ?>
		<a href="<?php echo esc_url(get_term_link($term->term_id)); ?>"><?php echo esc_html( $term->name);?></a>
			<?php 
 			if($count == 5) { 
			break;
			}
			
			} ?>
	</div>
  	
<?php  }
}