<?php
/*
 *  Single fooditems - Top image
 *
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

get_header();
global $fbbttm_helperobj,$fbbttm_cartobj,$fbbttm_allowed_html;

$post_id =  get_the_ID();
$featuredimage = get_the_post_thumbnail(get_the_ID() , 'full' );

if($featuredimage=='')
	{
					$featuredimage = "<img src='".FBBTTM_URL."/assets/images/noimge.jpg'>";
	}

$content = $fbbttm_helperobj->fbbttm_fooditems_description($post_id);

$shoppagelink = FBBTTM_SHOP_PAGE_LINK;

$currenycymbol =  $fbbttm_helperobj->getCurrencySymbol();

?>

<div class="fbbttm-fooditems-single-content-wrapper fbbttm-fooditems-view-full">
	<div class="fbbttm-fooditems-single-content-wrapper-innerbox">
		<div class="fbbttm-fooditems-single-content-area">

			  <div class="fbbttm-product-details">
			  	<div class="fbbttm-product_description">
					<?php 
					echo wp_kses(    $content,$fbbttm_allowed_html);

					?>
				</div>	
				<div class="fbbttm-backtolist">
					<a href="<?php echo esc_url($shoppagelink);?>"><?php echo esc_html__('Back to list','foodboxbooker');?></a>
				</div>
			  </div>
			  <div class="fbbttm-foodboxbooker-post-featured-link-wrapper">
			  	<?php 

			  		echo wp_kses($featuredimage,$fbbttm_allowed_html);

			  	?>
			  </div>
            
		</div><!-- .fbbttm-fooditems-single-content-area -->
		
	</div>
	
</div>

<?php $packagelist = $fbbttm_helperobj->fbbttm_get_packagelist($post_id);?>

<div class="row">
	<!-- load package list for this post -->

		<?php

		if(!empty($packagelist)){

		    foreach($packagelist as $plist){
		
				$packprice =  $plist->fbbttm_package_price;
				$packprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($packprice);
				$packid = $plist->fbbttm_pack_id;
				$packdesc = $plist->fbbttm_package_description;

				$package_image = $plist->fbbttm_package_image;

				if($package_image=='')
				{
					$package_image = FBBTTM_URL.'/assets/images/noimge.jpg';
				}

				$priceinfo = $fbbttm_helperobj->fbbttm_get_packinfo($packid);
				$day = $plist->fbbttm_total_day;


				$packname =  $plist->fbbttm_package_name;

				$totalamount = $priceinfo['pack_price'];
				$fooditemprice = $priceinfo['fooditemprice'];
				$specialprice = $priceinfo['pack_specialprice'];

				$specialpricewithsymbol = $priceinfo['pack_specialprice_withsymbol'];


	 		?>
		<div class="col-md-12"> 

			<div class="row fbbttm-package-box">

				<div class="col-md-8 fbbttm-packaheader">
					<div class="fbbttm-packsimg">
						<img src="<?php echo esc_url($package_image);?>" class="wp-post-image" alt="package-img">
					</div>
					<div class="fbbttm-pack-contentss">
					<div class="fbbttm-packagename">
						<?php echo esc_html($plist->fbbttm_package_name);?> 
					</div>

					<div class="fbbttm-product-details">

			<?php if(!empty($priceinfo)){
				$savemoney = $priceinfo['savemoney'];
				$totalamount = $priceinfo['pack_price'];
			?>
				<div class="fbbttm-priceinfo-section">

					<div class="priceinfo-content">
							<?php 

							$specialprice = $priceinfo['pack_specialprice'];
							$cclas = ($specialprice > 0) ? 'specialprice-available' : '';

							?>
							<span class="fbbttm-price <?php echo esc_html($cclas);?>"><?php echo esc_html($priceinfo['fooditemprice']);?></span>

							<?php if(!empty($cclas)){ ?>
								<span class="fbbttm-special-price">
									<?php echo esc_html($priceinfo['pack_specialprice_withsymbol']);?>
								</span>
							
								<span class="fbbttm-food-mealinfo">
									<?php echo esc_attr__('Per meal','foodboxbooker');?>
								</span>

								<span class="fbbttm-saveprice-info">
									<?php 

									sprintf(
    /* translators: 1: save up to price */
    esc_html__('Save up to %1$s', 'foodboxbooker'),  esc_html($savemoney));
									?>
								</span>
							<?php } ?>

							<div class="fbbttm-totalamount">
								<?php echo esc_attr__('Total price for this package : ','foodboxbooker');?>  <?php echo esc_html($totalamount);?>
								
							</div>
							<div class="fbbttm-totaldays">
								<span><?php echo esc_attr__('Total days for this package : ','foodboxbooker');?>  <?php echo  esc_html($plist->fbbttm_total_day);?></span>
							</div>

						</div>										
				</div>
			
		<?php } ?>
			  <div class="fbbttm-foodboxbooker-content">
					<?php echo esc_html($plist->fbbttm_package_description);?>
			  </div>
	     </div>
						
					</div>
				</div>

				<div class="col-md-4">

					<button class="fbbttm-subscribe" data-name="<?php echo esc_html($packname);?>"  data-id="<?php echo esc_html($packid);?>" data-totalmount="<?php echo esc_html($totalamount);?>" data-price="<?php echo esc_html($fooditemprice);?>" data-specialprice="<?php echo esc_html($specialprice);?>" data-swsymbol="<?php echo esc_html($specialpricewithsymbol);?>" data-day="<?php echo esc_html($day);?>" data-image="<?php echo esc_url($package_image);?>" data-desc="<?php echo esc_html($packdesc);?>"><?php  echo esc_attr__('Subscribe','foodboxbooker');?></button>

				</div>

			</div> 

						

		</div>	
	 <?php 	}
	 } ?>
</div>


<div class="foodboxbooker-main" id="foodboxbooker-model-bg"></div>
	<div class="modal" id="foodboxbooker-model">
		<div class="header">
			<a href="#" class="cancel">X</a>
		</div>
	<div class="content">

		<div class="row packbooking-content">
		 <form name="fbbttm_add_to_cart" id="fbbttm_add_to_cart" method="post">
			<input type="hidden" name="fbbttm-packid" id="hiddenpackid" value=""/>	
<input type="hidden" name="fbbttm-userid" value="<?php echo esc_html(get_current_user_id());?>">
			<?php
				// Noncename needed to verify where the data originated
				wp_nonce_field( 'fbbttm_meta_box_nonce', 'fbbttm_meta_box_noncename' );
			?>
			<div class="fbbttm-packcontent-info-details">
				<div class="fbbttm-pack-header_wrap">
					<div class="fbbttm-pack-img_wraper">
						<img src="" class="wp-post-image" alt="package-img">		
					</div>
					<div class="fbbttm-pack-tit_wraper">
						<div class="fbbttm-packname"></div>
						<div class="fbbttm-productprice">

							<div class="fbbttm-baseprice">
								
							</div>

							<div class="fbbttm-specialprice">
								
							</div>
						</div>
						<div class="fbbttm-pack-info"></div>
					</div>
				</div>

				<div class="fbbttm-pack-day">
					
					
			<div class="totalday"><?php echo esc_html__('Total days of this package:','foodboxbooker');?>
			<span class="pack-day-val fbbttm-titletext">
						
					</span>
		</div>
				</div>

				<div class="fbbttm-totalamount-price-info">

					<div class="pack-day-name"><?php echo esc_html__('Total price of this package:','foodboxbooker');?></div>	

					<div class="fbbttm-totalamount-price"></div>
						

				</div>


			</div>
			<div class="fbbttm-class-stepsbox">
			<div class="fbbttm-class-step-one">
				<span class="fbbttm-titletext"><?php echo esc_attr__('Please choose Lunch, Dinner or Both','foodboxbooker');?></span>
				<span class="fbbttm-step-inpbox">
				<label><input type="checkbox" class="food_time" name="food_time[]" value="lunch"><?php echo esc_attr__('Lunch','foodboxbooker');?>  </label>
				<label><input type="checkbox" class="food_time" name="food_time[]" value="dinner"><?php echo esc_attr__('Dinner','foodboxbooker');?>  </label>
				<div class="fbbttm-error fbbttm-deliverytime"></div>
				</span>	
				<div class="fbbttm-info"><?php echo esc_html__('By default we send one meal per day , if you want lunch and dinner both then your package days will be divided according to it. ','foodboxbooker');?></div>
			</div>
			<span class="fbbttm-titletext"><?php echo esc_attr__('How often do you want your deliveries?','foodboxbooker');?></span>
		<div class="fbbttm-days-wrapper">
			<div class="fbbttm-class-step-two">
					<div class="fbbttm-step-inpbox fbbttm-days">
					<label><input type="radio" name="deliveryday" class="deliveryday"  value="Mon-Sun"> <?php echo esc_html__('Mon-Sun','foodboxbooker');?> </label>
					<label><input type="radio" name="deliveryday" class="deliveryday"  value="Mon-Fri"> <?php echo esc_html__('Mon-Fri','foodboxbooker');?> </label>
					<label><input type="radio" name="deliveryday" class="deliveryday"  value="Mon-Sat"> <?php echo esc_html__('Mon-Sat','foodboxbooker');?> </label>
					<label><input type="radio" name="deliveryday" class="deliveryday"  value="custom"> <?php echo esc_html__('Custom','foodboxbooker');?> </label>
					</div>	
		</div>
		<div class="fbbttm-class-step-three">

			<?php  
			// phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date 
			$enddate = date('Y-m-d', strtotime(date('Y-m-d') .' +1 day')); ?>
			<input type="date" name="startdate"  min="<?php echo esc_html($enddate);?>" />
			
			<input type="hidden" name="datemessge" id="datemessge" value=""/> 
			<input type="hidden" name="datemessge_blank" id="datemessge_blank" value=""/> 
		</div>

	</div>

		<?php

		$dayarray = array('Monday'=>'Monday','Tuesday'=>'Tuesday','Wednesday'=>'Wednesday','Thursday'=>'Thursday','Friday'=>'Friday','Saturday'=>'Saturday','Sunday'=>'Sunday',);

		?>
		<div class="fbbttm-customday-show hide" >
			<?php foreach ($dayarray as $key => $val)
			{
			?>
			<input type="checkbox" name="deliday[]" value="<?php echo esc_html($key);?>" class="customdaycheckbox" checked>
			<label><?php echo esc_html($val);?></label>
		<?php }?>
		</div>
		<div class="fbbttm-error fbbttm-deliveryday"></div>
		<div class="fbbttm-error fbbttm-startdate"></div>
		<div class="fbbttm-error fbbttm-datemessge"></div>
		<div class="fbbttm-error fbbttm-datemessge-blank"></div>

	
</div>
	<div class="fbbttm-addtocart-button">
		<input type="submit" name="addtocart" id="addtocart" value="Add to Cart"/>
		<div class="fbbttm-loader hide"></div> 
	</div>	

  </form>
</div>

</div>
</div>

	<div class="fbbttm-message fbbttm-packageaddcart-message"></div>

	<?php

	$cartinfo = $fbbttm_cartobj->fbbttm_get_basic_cartinfo();
	$countcart = '('.count($cartinfo).')';
?>
	
	<div class="row fbbttm-package-cartbtn">
	<div class="fbbttm-foodboxbooker-cartinfo-content">
		<?php if(!empty($cartinfo)){ ?>
			<div class="fbbttm-foodboxbooker-cartinfo">
            <a href="<?php echo esc_url(FBBTTM_CART_PAGE_LINK);?>">
            	<?php 
            	echo sprintf(
				    /* translators: 1: cart count */
				    esc_html__('Go to cart %1$s', 'foodboxbooker'),
				    esc_html($countcart)
				);
            	?> 
            	</a>
         </div>
	    <?php } ?>
	</div>
	</div>


	<?php
// Get the current post ID and post type
$current_post_id = get_the_ID();
$current_post_type = get_post_type();

// Define your custom taxonomy (replace 'your_taxonomy' with the actual taxonomy slug)
$taxonomy = 'fbbttm_fooditems_category';

// Get the terms of the current post in the defined taxonomy
$terms = wp_get_post_terms( $current_post_id, $taxonomy );

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    // Get an array of term IDs
    $term_ids = wp_list_pluck( $terms, 'term_id' );

    // Query for related posts
    $related_args = array(
        'post_type'      => $current_post_type,
        'posts_per_page' => 5, // Number of related posts to display
        'post__not_in'   => array( $current_post_id ), // Exclude the current post
        'tax_query'      => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'term_id',
                'terms'    => $term_ids,
            ),
        ),
    );

    $related_posts = new WP_Query( $related_args );

    if ( $related_posts->have_posts() ) {
    	?>
    	<div class="fbbttm-related-product-details">
      	 <div class="fbbttm-related-product-name">
			  		<h2 class="fbbttm-related-name"><?php echo esc_html__('Related product','foodboxbooker');?></h2> 
		 </div>
		 <div class="fbbttm-related-product_description">
       		<div class="fbbttm-related-box">
       	<?php 
       	$price ='0';
        while ( $related_posts->have_posts() ) {
            $related_posts->the_post();
            $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">';
            }

            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;
  		  		$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);
            ?>
            <div class="col-md-4">
            	<div class="fbbttm-foodboxbooker-post-item">
		   		 	<div class="fbbttm-foodboxbooker-post-item-inner">
					    <div class="fbbttm-foodboxbooker-post-featured-link-wrapper">
					    	<a href="<?php the_permalink();?>">
					   		<?php echo wp_kses($featuredimage,$fbbttm_allowed_html);?>
					     	</a>
					    </div>

					<div class="fbbttm-foodboxbooker-box-overlay">
						<div class="fbbttm-foodboxbooker-box-content">		  
		            		<h4><a href="<?php the_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h4>
		            		<div class="fbbttm-shortdesc-price">
		            			<?php echo wp_kses($limited_description,$fbbttm_allowed_html);?>
		            		</div>
		            		<div class="fbbttm-food-orderbtn"><a href="<?php get_the_permalink(get_the_ID()); ?>"><?php echo esc_html__('Order now!','foodboxbooker');?></a>
		            		</div>
		            	</div>
		            	<div class="fbbttm-fooditems-price">
		            		<div class="fbbttm-pricebox">
		            			<?php echo esc_html($currenycymbol.number_format(round($price, 2), 2));?>
		            		</div>
		            	</div>
		            </div>	
		            			            	
            </div>
            </div>
        </div>
            <?php
        }

        ?>
        </div>
        </div>
        </div>
        <?php
       
    }
    // Restore original post data
    wp_reset_postdata();
} ?>	