<?php
/**
 * Order recieved information 
 * 
*/
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

global $fbbttm_helperobj,$fbbttm_orderinfoobj,$fbbttm_emailobj;


$fbbttmkey = '';
$orderid = '';
$orderinfo = array();
	if ( get_query_var('fbbttmkey') ) {
	   // If so echo the value
	    $fbbttmkey =  get_query_var('fbbttmkey');
	}

	if(!empty($fbbttmkey) || !empty($orderid)){		
		$orderinfo = $fbbttm_orderinfoobj->fbbttm_get_orderinfo_byorder_key($fbbttmkey,$orderid);
	}

	$orderlinkexpire = 0;

	if(!empty($orderinfo)){
		$orderlinkexpire = $orderinfo['orderinfo'][0]->fbbttm_expire_link;
		$orderid = $orderinfo['orderinfo'][0]->fbbttm_fdbid;
	}


	if($orderlinkexpire=='0'){

		if(!empty($fbbttmkey) || !empty($orderid)){	

			$fbbttm_emailobj->fbbttm_new_ordermail($orderinfo);
			$fbbttm_emailobj->fbbttm_new_admin_ordermail($orderinfo);

			$datalarte = array(	'fbbttm_expire_link'=>'1');

			$fbbttm_helperobj->fbbttm_common_updatetable(FBBTTM_TB_FOODBOOKING,$datalarte,'fbbttm_fdbid',$orderid,'fbbttm_update_date');
		}

	}

if (isset($_COOKIE['custom_token_cookie'])) {

	    unset($_COOKIE['custom_token_cookie']); 
	    setcookie('custom_token_cookie', '', -1, '/'); 
		 $_COOKIE['custom_token_cookie'] ='';
	}

$ordernumber = $useremail = $fbbttm_date = $total = $paymentmethod = $grandtotal= $tax=$subtotals='';

?>

<div class="fbbttm-order-recieved-info" id="fbbttm-order-recieved-info">
  <div class="fbbttm-orderinfo">
	  <?php if(!empty($orderinfo) && $orderlinkexpire=='0'){?>
		  <div class="fbbttm-loader hide"></div> 
			 <div class="fbbttm-userdetailsinfo">
				<h3>
					<?php echo esc_html__('Thank you. Your order has been received.','foodboxbooker');?>	</h3>
						<ul>
						<?php foreach($orderinfo['orderinfo'] as $orderdata){?>
							<li>
							<span class="fbbttm-lable"><?php echo esc_html__('Order number:','foodboxbooker');?></span>
							<span class="fbbttm-value"><?php echo esc_html($orderdata->fbbttm_fdbid);?></span>
						</li>
						<li>
						<span class="fbbttm-lable"><?php echo esc_html__('Date:','foodboxbooker');?></span>
						<span class="fbbttm-value">
					<?php

					$fbbttm_date = $orderdata->fbbttm_created_date;
					// phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date 
					$newdate = date('F d, Y',strtotime($fbbttm_date));
					echo esc_html($newdate);
					?>

				</span>

			</li>
			<li>
				<span class="fbbttm-lable"><?php echo esc_html__('Total:','foodboxbooker');?></span>
				<span class="fbbttm-value">
				<?php 
				$total = $orderdata->fbbttm_grand_total;
				$tax = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderdata->fbbttm_tax);
				$subtotal = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderdata->fbbttm_sub_total);

				$grandtotal =  $fbbttm_helperobj->fbbttm_get_price_with_currency($total);

				echo esc_html($grandtotal);
				?>
			</span>
			</li>
			<li>
				<span class="fbbttm-lable"><?php echo esc_html__('Email:','foodboxbooker');?></span>
				<span class="fbbttm-value">
					<?php

					$email = $orderdata->user_email;

					echo esc_html($email);
					?>

				</span>
			</li>
			<li>
				<span class="fbbttm-lable"><?php echo esc_html__('Payment method:','foodboxbooker');?></span>
				<span class="fbbttm-value">
					<?php

					$paymentmethod = $orderdata->fbbttm_payment_method;

					echo esc_html($paymentmethod);
					?>

				</span>
			</li>
		<?php }?>
		</ul>
	</div>


<div class="fbbttm-orderiteminfocontent">
	<h3><?php echo esc_html__('Order details','foodboxbooker');?></h3>
	<table class="fbbttm-orderitem-list" width="100%">
		
		<tr>
			<th><?php echo esc_html__('Product Name','foodboxbooker');?></th>
			<th><?php echo esc_html__('Package Name','foodboxbooker');?></th>
			<th><?php echo esc_html__('Subtotal'	 ,'foodboxbooker');?></th>
		</tr>
		<tbody>

			
				<?php foreach($orderinfo['orderiteminfo'] as $orderitem){

					$title = get_the_title($orderitem->fbbttm_fd_id);
					$packname = $orderitem->fbbttm_pack_name;
					$packprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderitem->fbbttm_package_price);
					$fooditemprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderitem->fbbttm_fitem_price);
					$packspecialprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderitem->fbbttm_special_price);
					$packday = $orderitem->fbbttm_package_day;
					$packstartdate = $orderitem->fbbttm_food_startdate;
					$packenddate = $orderitem->fbbttm_food_enddate;
					$packdescription = $orderitem->fbbttm_package_description;
					$foodtime = $orderitem->fbbttm_food_time;
					$deliverydays = $orderitem->fbbttm_booking_deliveryday;

					

					$post_meta = get_post_meta($orderitem->fbbttm_fd_id); 
    				$itemdetails =  (!empty($post_meta['fbbttmdetails'][0]))? $post_meta['fbbttmdetails'][0] : '';

					?>
				<tr>
				<td><?php echo esc_html($title);?></td>
				<td>

					   <div><?php echo esc_html__('Package Name : ','foodboxbooker'); echo esc_html($packname);?></div>

						<div><?php echo esc_html__('Package Startdate : ','foodboxbooker'); echo esc_html($packstartdate);?></div>
						<div><?php echo esc_html__('Package enddate :  ','foodboxbooker'); echo esc_html($packenddate);?></div>

						<div><?php echo esc_html__('Delivery Day :  ','foodboxbooker'); echo esc_html($deliverydays);?></div>

						<div><?php echo esc_html__('Delivery Time :  ','foodboxbooker'); echo esc_html($foodtime );?></div>

						<div><?php echo esc_html__('Package info : ','foodboxbooker');  echo esc_html(($packdescription)?$packdescription:'-');?></div>

						<div>
								<?php 
									echo esc_html__('Item info : ','foodboxbooker'); 
									echo esc_html(($itemdetails)?$itemdetails:'-');
									?>
						</div>
				</td>
				<td>

					<?php echo esc_html($packprice);?>
				</td>
					</tr>
			<?php }?>
		
		</tbody>
		<tfoot>
			

			<tr>
				<td colspan="2">
					<?php echo esc_html__('Subtotal','foodboxbooker');?>
				</td>
				<td class="fbbttm_title">
				<?php echo esc_html($subtotal);?>
			 </td>
			</tr>

			<tr>
				<td colspan="2">
					<?php echo esc_html__('Tax','foodboxbooker');?>
				</td>
				<td class="fbbttm_title">
				<?php echo esc_html($tax);?>
			 </td>
			</tr>


			<tr>
				<td colspan="2">
					<?php echo esc_html__('Total','foodboxbooker');?>
				</td>
				<td class="fbbttm_title">
				<?php echo esc_html($grandtotal);?>
			 </td>
			</tr>

		</tfoot>

	</table>

</div>


	</div>

<?php } else{

	$pagelink = FBBTTM_SHOP_PAGE_LINK;	 

	?>

	<p class="fbbttm-message"><a class="link" href="<?php echo esc_url(  $pagelink  );?>"><?php echo esc_html__( 'Click here', 'foodboxbooker' );?></a> <?php echo esc_html__( '  to continue shopping', 'foodboxbooker' );?></p>

	<?php
}?>

</div>