<?php

//[fbbttm-fooditem]

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
function fbbttm_foodboxbooker_fooditem($atts, $content=NULL){
	global $fbbttm_helperobj;
	
	$currenycymbol =  $fbbttm_helperobj->getCurrencySymbol();
	// phpcs:disable WordPress.Security.NonceVerification.Missing
	$custom_orderby = isset($_POST['orderby']) ? sanitize_text_field(wp_unslash($_POST['orderby'])) : "";
	$sorting = ($custom_orderby == 'title') ?  'ASC' : 'DESC';
	$show = 6;
	$args = array(
			'post_type'				=> 'fbbttm_fooditems',
			'posts_per_page'		=> $show,
			'ignore_sticky_posts'	=> true,
			'orderby'=> $custom_orderby,
    	'order'=>$sorting,
			'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
		);

	$posts = new \WP_Query( $args );

	$return = '';

	if ( $posts->have_posts() ) {

		$sortform = '<div class="col-md-4 fbbttm-pagelist-ordery">
		<form method="POST">';
		$sortform .='<select name="orderby" id="orderby"><option value="">'.esc_html__('Please select','foodboxbooker').'</option><option value="date">'.esc_html__('Newest to Oldest','foodboxbooker').'</option><option value="title">'.esc_html__('Alphabetical','foodboxbooker').'</option></select><button type="submit">'.esc_html__('Apply','foodboxbooker').'</button></form></div>';

		$return .='<div class="row">'.$sortform.'</div>';

		$return .='<div class="row"></div>';

		$return .='<div class="row fbbttm-pagelist-fooditems">';
	  		
	  while ( $posts->have_posts() ) {

	  	

		   	$posts->the_post();

		   	$post_id = get_the_ID();
	  	

		   	$meta_data = get_post_meta( get_the_ID(), 'fbbttm_fbbttm_foodboxbooker_fooditems_list_data', true );

				$price ='0';

				$description = get_the_excerpt();
				if (empty($description)) {
				    $description = get_the_content(); // Fallback to full content if the excerpt is not available
				}

				$limited_description = fbbttm_limit_text(wp_strip_all_tags($description), 150);

				if( !empty($meta_data['fbbttm_fbbttm_foodboxbooker_fooditems_price_text']) ){		
						$price = $meta_data['fbbttm_fbbttm_foodboxbooker_fooditems_price_text'];
				}

				$post_meta = get_post_meta(get_the_ID()); 	// put here input or textarea 
  		  $price =  (!empty($post_meta['fbbttmprice'][0])) ? $post_meta['fbbttmprice'][0] : $price;

				$featuredimage = get_the_post_thumbnail(get_the_ID() , 'full' );
				  if($featuredimage=='')
            {
            	$featuredimage ='<img class="attachment-full size-full wp-post-image" src="'.FBBTTM_URL.'/assets/images/noimge.jpg">';
            } 

			 	$return .= '<div class="col-md-4">';
			 				     
		   	$return .= '<div class="fbbttm-foodboxbooker-post-item">
		   		 			    <div class="fbbttm-foodboxbooker-post-item-inner">
								    <div class="fbbttm-foodboxbooker-post-featured-link-wrapper"><a href="'.get_the_permalink().'">'.$featuredimage.'</a></div>';

				$return .= '<div class="fbbttm-foodboxbooker-box-overlay">
								  	<div class="fbbttm-foodboxbooker-box-content">
								 		<h4><a href="'.get_the_permalink().'">'.get_the_Title().'</a></h4>';				

				$return .= '<div class="fbbttm-shortdesc-price">';
				$return .=  $limited_description;
				$return .= '</div>';

				$return .= '<div class="fbbttm-food-orderbtn"><a href="'.get_the_permalink().'">'.esc_html__('Order now!','foodboxbooker').'</a></div>';

				$return .= '</div>';

				$return .= '<div class="fbbttm-fooditems-price"><div class="fbbttm-pricedetail">';
				$return .=  $currenycymbol.number_format(round($price, 2), 2);
				$return .= '</div></div>';
				

				$return .= '</div></div></div></div>';
			}

		$return .='</div>';
	}


	$big = 999999999; // need an unlikely integer
	 $return .= paginate_links( array(
	    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
	    'format' => '?paged=%#%',
	    'current' => max( 1, get_query_var('paged') ),
	    'total' => $posts->max_num_pages
	) );

	return $return;
}


  add_shortcode( 'fbbttm-fooditem', 'fbbttm_foodboxbooker_fooditem' );
?>