<?php 
function pf_itemslider_func( $atts ) {
  extract( shortcode_atts( array(
    'listingtype' => '',
	'itemtype' => '',
	'locationtype' => '',
	'sortby' => 'ASC',
	'orderby' => 'title',
	'count' => 12,
	'posts_in' => '',
	'features'=> '',
	'mode' => 'fadeUp',
	'interval' => 5000,
	'featureditems' =>'',
	'autoplay' =>'',
	'descbox'  => ''
  ), $atts ) );


$setup3_pointposttype_pt4_check = PFSAIssetControl('setup3_pointposttype_pt4_check','','1');
$setup3_pointposttype_pt5_check = PFSAIssetControl('setup3_pointposttype_pt5_check','','1');
$setup3_pointposttype_pt6_check = PFSAIssetControl('setup3_pointposttype_pt6_check','','1');

$gridrandno_orj = PF_generate_random_string_ig();
$gridrandno = 'pf_'.$gridrandno_orj;

$listingtype_x = PFEX_extract_type_ig($listingtype);
$itemtype_x = ($setup3_pointposttype_pt4_check == 1) ? PFEX_extract_type_ig($itemtype) : '' ;
$locationtype_x = ($setup3_pointposttype_pt5_check == 1) ? PFEX_extract_type_ig($locationtype) : '' ;
$features_x = ($setup3_pointposttype_pt6_check == 1) ? PFEX_extract_type_ig($features) : '' ;
		
		
		/* Get admin values */
		$setup3_pointposttype_pt1 = PFSAIssetControl('setup3_pointposttype_pt1','','pfitemfinder');

		
		//Container & show check
		$pfcontainerdiv = 'pflistgridview'.$gridrandno_orj.'';
		$pfcontainershow = 'pflistgridviewshow'.$gridrandno_orj.'';
		
		
		//Defaults
		$pfgetdata = array();
		$pfgetdata['sortby'] = $sortby;
		$pfgetdata['orderby'] = $orderby;
		$pfgetdata['count'] = $count;
		$pfgetdata['posts_in'] = $posts_in;
		$pfgetdata['mode'] = $mode;
		$pfgetdata['interval'] = $interval;
		$pfgetdata['listingtype'] = $listingtype_x;
		$pfgetdata['itemtype'] = $itemtype_x;
		$pfgetdata['locationtype'] = $locationtype_x;
		$pfgetdata['features'] = $features_x;
		$pfgetdata['featureditems'] = $featureditems;		

		if($pfgetdata['count'] == 'All' || $pfgetdata['count'] == 'all' || $pfgetdata['count'] == 'ALL'){$pfgetdata['count'] = -1;}

		$args = array( 'post_type' => $setup3_pointposttype_pt1, 'post_status' => 'publish');
		if($pfgetdata['posts_in']!=''){
			$args['post__in'] = pfstring2BasicArray($pfgetdata['posts_in']);

		}
		
		$setup3_pointposttype_pt4_check = PFSAIssetControl('setup3_pointposttype_pt4_check','','1');
		$setup3_pointposttype_pt5_check = PFSAIssetControl('setup3_pointposttype_pt5_check','','1');
		$setup3_pointposttype_pt6_check = PFSAIssetControl('setup3_pointposttype_pt6_check','','1');


		
		if(is_array($pfgetdata)){

			$args['tax_query'] = array();

			// listing type
			if($pfgetdata['listingtype'] != ''){
				$pfvalue_arr_lt = PFGetArrayValues_ld($pfgetdata['listingtype']);

				$fieldtaxname_lt = 'pointfinderltypes';

				if(count($args['tax_query']) > 0){
					$args['tax_query'][(count($args['tax_query'])-1)]=
					array(
							'taxonomy' => $fieldtaxname_lt,
							'field' => 'id',
							'terms' => $pfvalue_arr_lt,
							'operator' => 'IN'
					);
				}else{
					$args['tax_query']=
					array(
						'relation' => 'AND',
						array(
							'taxonomy' => $fieldtaxname_lt,
							'field' => 'id',
							'terms' => $pfvalue_arr_lt,
							'operator' => 'IN'
						)
					);
				}
			}

			if($setup3_pointposttype_pt4_check == 1){
				// location type
				if($pfgetdata['locationtype'] != ''){
					$pfvalue_arr_loc = PFGetArrayValues_ld($pfgetdata['locationtype']);

					$fieldtaxname_loc = 'pointfinderlocations';

					if(count($args['tax_query']) > 0){
						$args['tax_query'][(count($args['tax_query'])-1)]=
						array(
								'taxonomy' => $fieldtaxname_loc,
								'field' => 'id',
								'terms' => $pfvalue_arr_loc,
								'operator' => 'IN'
						);
					}else{
						$args['tax_query']=
						array(
							'relation' => 'AND',
							array(
								'taxonomy' => $fieldtaxname_loc,
								'field' => 'id',
								'terms' => $pfvalue_arr_loc,
								'operator' => 'IN'
							)
						);
					}
				}
			}

			if($setup3_pointposttype_pt5_check == 1){
				// item type
				if($pfgetdata['itemtype'] != ''){
				$pfvalue_arr_it = PFGetArrayValues_ld($pfgetdata['itemtype']);

				$fieldtaxname_it = 'pointfinderitypes';

				if(count($args['tax_query']) > 0){
					$args['tax_query'][(count($args['tax_query'])-1)]=
					array(
							'taxonomy' => $fieldtaxname_it,
							'field' => 'id',
							'terms' => $pfvalue_arr_it,
							'operator' => 'IN'
					);
				}else{
					$args['tax_query']=
					array(
						'relation' => 'AND',
						array(
							'taxonomy' => $fieldtaxname_it,
							'field' => 'id',
							'terms' => $pfvalue_arr_it,
							'operator' => 'IN'
						)
					);
				}
				}
			}

			if($setup3_pointposttype_pt6_check == 1){
				// features type
				if($pfgetdata['features'] != ''){
				$pfvalue_arr_fe = PFGetArrayValues_ld($pfgetdata['features']);

				$fieldtaxname_fe = 'pointfinderfeatures';

				if(count($args['tax_query']) > 0){
					$args['tax_query'][(count($args['tax_query'])-1)]=
					array(
							'taxonomy' => $fieldtaxname_fe,
							'field' => 'id',
							'terms' => $pfvalue_arr_fe,
							'operator' => 'IN'
					);
				}else{
					$args['tax_query']=
					array(
						'relation' => 'AND',
						array(
							'taxonomy' => $fieldtaxname_fe,
							'field' => 'id',
							'terms' => $pfvalue_arr_fe,
							'operator' => 'IN'
						)
					);
				}
				}
			}

			//Changed values by user
			$args['orderby'] = $pfgetdata['orderby'];
			$args['order'] = $pfgetdata['sortby'];
			$args['posts_per_page'] = $pfgetdata['count'];
			
			//Featured items filter
			if($pfgetdata['featureditems'] == 'yes'){
				
				$args['meta_query'] = array();

				if(count($args['meta_query']) > 0){
					$args['meta_query'][(count($args['meta_query'])-1)] = array(
						'key' => 'webbupointfinder_item_featuredmarker',
						'value' => 1,
						'compare' => '=',
						'type' => 'NUMERIC'
						);
					
				}else{
						$args['meta_query'] = array(
							'relation' => 'AND',
							array(
							'key' => 'webbupointfinder_item_featuredmarker',
							'value' => 1,
							'compare' => '=',
							'type' => 'NUMERIC'
						)
					);

				}
			}		
			
		}
		
		
		
		//Create html codes
		$wpflistdata = "<div class='pfitemslider".$gridrandno_orj."is-container'>";
		$wpflistdata_output = '<ul id="'.$gridrandno_orj.'" class="pf-item-slider">';	
		

		$setup22_searchresults_hide_lt  = PFSAIssetControl('setup22_searchresults_hide_lt','','0');			
							
		$loop = new WP_Query( $args );
			
			if($loop->post_count > 0){
		
				while ( $loop->have_posts() ) : $loop->the_post();
				
				$post_id = get_the_id();
				
									
						$ItemDetailArr = array();
						$pflang = PF_current_language();
						if ($pflang) {
							$pfitemid = PFLangCategoryID_ld($post_id,$pflang,$setup3_pointposttype_pt1);
						}else{
							$pfitemid = $post_id;
						}

						
						$featured_image = '';
						$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $pfitemid ), 'full' );
						$ItemDetailArr['featured_image_org'] = $featured_image[0];
						$ItemDetailArr['featured_image'] = get_post_meta($pfitemid,'webbupointfinder_item_sliderimage',true);


						
						
						if(is_array($ItemDetailArr['featured_image'])){
							if(count($ItemDetailArr['featured_image'])>0){
								$ItemDetailArr['featured_image'] = $ItemDetailArr['featured_image']['url'];
							}else{
								$ItemDetailArr['featured_image'] = $ItemDetailArr['featured_image_org'];
							}
						}else{
							$ItemDetailArr['featured_image'] = $ItemDetailArr['featured_image_org'];
						}
						//Title
						$ItemDetailArr['if_title'] = get_the_title($pfitemid);
						//Exceprty
						$ItemDetailArr['if_excerpt'] = get_the_excerpt();
						//Permalink
						$ItemDetailArr['if_link'] = get_permalink($pfitemid);;
						//Address
						$ItemDetailArr['if_address'] = esc_html(get_post_meta( $pfitemid, 'webbupointfinder_items_address', true ));
						
						$output_data = PFIF_DetailText_ld($pfitemid,$setup22_searchresults_hide_lt);
						if (is_array($output_data)) {
							if (!empty($output_data['ltypes'])) {
								$output_data_ltypes = $output_data['ltypes'];
							} else {
								$output_data_ltypes = '';
							}
							if (!empty($output_data['content'])) {
								$output_data_content = $output_data['content'];
							} else {
								$output_data_content = '';
							}
							if (!empty($output_data['priceval'])) {
								$output_data_priceval = $output_data['priceval'];
							} else {
								$output_data_priceval = '';
							}
						} else {
							$output_data_priceval = '';
							$output_data_content = '';
							$output_data_ltypes = '';
						}
						
						
						$wpflistdata_output .= '<li class="pf-item-slider-items">
						<img src="'.$ItemDetailArr['featured_image'].'" alt="">';
						if($descbox != 'yes'){
						$wpflistdata_output .='
						<div class="pf-item-slider-description-container">
						<div class="pf-item-slider-description">
							<div class="pf-item-slider-title"><a href="'.$ItemDetailArr['if_link'].'">'.$ItemDetailArr['if_title'].'</a></div>
							<div class="pf-item-slider-address"><a href="'.$ItemDetailArr['if_link'].'">'.$ItemDetailArr['if_address'].'</a></div>
							<div class="pf-item-slider-excerpt"><p>'.wp_trim_words( $ItemDetailArr['if_excerpt'], 23, ' ...' ).'</p></div>
						</div>
						<div class="pf-item-slider-ex-container">';
						if(!empty($output_data_priceval)){
							$wpflistdata_output .='<div class="pf-item-slider-price clearfix">'.$output_data_priceval.'</div>';
						}
						$wpflistdata_output .='<div class="pf-item-slider-golink clearfix"><a href="'.get_the_permalink().'">'.esc_html__('Details','pointfindert2d').'</a></div>';
						
						}
						$wpflistdata_output .='
						</div>
						</li>';
					
					
				endwhile;
				wp_reset_postdata();
			}
			$wpflistdata_output .= '</ul>';
            $wpflistdata .= $wpflistdata_output;               
			$wpflistdata .= "</div> ";

			$wpflistdata .= 
			'
			<script type="text/javascript">
			(function($) {
			"use strict"
				$(function() {';
					if($descbox !== 'yes'){
					$wpflistdata .='
					$(window).bind("load resize orientationchange", function(){
						if($("#'.$gridrandno_orj.'").width() < 640){
						
							$("#'.$gridrandno_orj.'").addClass("pfmobile");
						}else{
							
							$("#'.$gridrandno_orj.'").removeClass("pfmobile");
						}
					});
					';

					}
						$wpflistdata .='
						$("#'.$gridrandno_orj.'").owlCarousel({
						items : 1,
						navigation : true,
						paginationNumbers : false,
						pagination : false,';
						if($autoplay == 'yes'){ $wpflistdata .=  'autoPlay : true,stopOnHover : true,';}else{$wpflistdata .=  'autoPlay : false,';}
						$wpflistdata .= '
						slideSpeed:'.$interval.',
						mouseDrag:false,
						touchDrag:true,
						itemSpaceWidth: 0,
						autoHeight : false,
						responsive:true,
						transitionStyle: "'.$mode.'", 
						itemsScaleUp : false,
						navigationText:false,
						theme:"owl-theme",
						singleItem : true,
					});
			});

			})(jQuery);
			</script>
';
return $wpflistdata;
}
add_shortcode( 'pf_itemslider', 'pf_itemslider_func' ); 

?>