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

	$nameErr = $lnameErr = $emailErr = $phone_err = $fbbttm_date_err = "";    

	global $wpdb;
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$view = (isset($_REQUEST['view'])) ? 'viewmode': '';
if(empty($view)){
?>	
	<main class="fbbttm-main fbbttm-main-listing-container fbbttm-default-box" id="all-page-main-container">
		<div class="fbbttm-details-tbl-wraper">
			<div class="row">
				
				<div class="col-lg-12 col-md-12 col-sm-12 fbbttm-details-tbl">
					<h2 class="fbbttm-page-heading"><?php echo esc_html__('Today delivery list', 'foodboxbooker'); ?></h2>
					<?php 
		global $wpdb;
						
						
	$table_foodbooking_iteminfo = FBBTTM_TB_FOODBOOKING_ITEMINFO;
	
	$foodbooking_table  = FBBTTM_TB_FOODBOOKING;
	$foodbookingitem_table  = FBBTTM_TB_FOODBOOKING_ITEMINFO;
	$wpusers_table  =FBBTTM_WP_USER;
	$table_fbbttm_delivery_address  = FBBTTM_DELIVERY_ADDRESS;
	$table_fbbttm_countries = FBBTTM_COUNTRIES;
    $table_fbbttm_states = FBBTTM_STATES;

	$currentdate = gmdate('Y-m-d');
// phpcs:disable WordPress.Security.NonceVerification.Recommended
	$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
    $per_page = 10;
    $offset = ($current_page - 1) * $per_page;						
	


 $query = $wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
 	"SELECT fbi.*,fbt.*,user.*,addrres.*,country.name as counname,state.name as statename FROM $table_foodbooking_iteminfo fbi  JOIN $foodbooking_table fbt ON fbi.fbbttm_fdbid = fbt.fbbttm_fdbid
 JOIN  $wpusers_table user ON user.ID = fbt.fbbttm_user_id
 JOIN  $table_fbbttm_delivery_address addrres ON user.ID = addrres.fbbttm_user_id
 JOIN  $table_fbbttm_countries country ON country.id = addrres.fbbttm_user_country
 JOIN  $table_fbbttm_states state ON state.id = addrres.fbbttm_user_state
 where fbbttm_food_startdate <=  %s AND fbbttm_food_enddate >=%s LIMIT %d OFFSET %d",$currentdate,$currentdate,$per_page ,$offset);

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching  
$fbbttm_result = $wpdb->get_results(
	//phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	$query, ARRAY_A);


$countpage = $wpdb->prepare("SELECT COUNT(*) FROM $table_foodbooking_iteminfo fbi  JOIN $foodbooking_table fbt ON fbi.fbbttm_fdbid = fbt.fbbttm_fdbid
 JOIN  $wpusers_table user ON user.ID = fbt.fbbttm_user_id
 JOIN  $table_fbbttm_delivery_address addrres ON user.ID = addrres.fbbttm_user_id
 JOIN  $table_fbbttm_countries country ON country.id = addrres.fbbttm_user_country
 JOIN  $table_fbbttm_states state ON state.id = addrres.fbbttm_user_state
 where fbbttm_food_startdate <=  %s AND fbbttm_food_enddate >=%s ",$currentdate,$currentdate);
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching  
$total_items = $wpdb->get_var(
	//phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	$countpage);
$total_pages = ceil($total_items / $per_page);

									
					if(count($fbbttm_result) == 0) { ?> 
						<h4><?php esc_html__('No Record Found!', 'foodboxbooker'); ?></h4> <?php  
					}
					else { ?>
						<section class="fbbttm-table-container">
							<div class="fbbttm-table__header-wrapper fbbttm-dashboard" id="fbbttm_user_table">
								<form method="post" name="bulk_action_form">
									<?php
    	// Noncename needed to verify where the data originated
    	wp_nonce_field( 'fbbttm_meta_box_nonce', 'fbbttm_meta_box_noncename' );
	  ?>
									<input id='search-box' placeholder='<?php echo esc_html__('Search here..', 'foodboxbooker'); ?>' class="fbbttm_search">
									<table>
										<thead class="has-gutter">
											<tr>
			
			<th colspan="1" rowspan="1" class="fbbttm-table_1_column_2 is-leaf is-sortable fbbttm-table__cell" id="name" data-order="desc" >
				<div class="cell"><?php echo esc_html__('Full Name', 'foodboxbooker'); ?>
				<span class="caret-wrapper"><i class="sort-caret ascending"></i><i class="sort-caret descending"></i></span>
				</div>
			</th>
			
			<th colspan="1" rowspan="1" class="fbbttm-table_1_column_2 is-leaf is-sortable fbbttm-table__cell" id="phone" data-order="desc" >
				<div class="cell"><?php echo esc_html__('Address', 'foodboxbooker'); ?>
				<span class="caret-wrapper"><i class="sort-caret ascending"></i><i class="sort-caret descending"></i></span>
				</div>
			</th>
			
			<th colspan="1" rowspan="1" class="fbbttm-table_1_column_2 is-leaf is-sortable fbbttm-table__cell" id="phone" data-order="desc" >
				<div class="cell"><?php echo esc_html__('Delivery time', 'foodboxbooker'); ?>
				<span class="caret-wrapper"><i class="sort-caret ascending"></i><i class="sort-caret descending"></i></span>
				</div>
			</th>
			<th colspan="1" rowspan="1" class="fbbttm-table_1_column_2 is-leaf is-sortable fbbttm-table__cell" id="phone" data-order="desc" >
				<div class="cell"><?php echo esc_html__('Delivery day', 'foodboxbooker'); ?>
				<span class="caret-wrapper"><i class="sort-caret ascending"></i><i class="sort-caret descending"></i></span>
				</div>
			</th>
			
			<th colspan="1" rowspan="1" class="fbbttm-table_1_column_2 is-leaf is-sortable fbbttm-table__cell" id="phone" data-order="desc" >
				<div class="cell"><?php echo esc_html__('Item name', 'foodboxbooker'); ?>
				<span class="caret-wrapper"><i class="sort-caret ascending"></i><i class="sort-caret descending"></i></span>
				</div>
			</th>
			<th colspan="1" rowspan="1" class="fbbttm-table_1_column_2 is-leaf is-sortable fbbttm-table__cell" id="phone" data-order="desc" >
				<div class="cell"><?php echo esc_html__('Item details', 'foodboxbooker'); ?>
				<span class="caret-wrapper"><i class="sort-caret ascending"></i><i class="sort-caret descending"></i></span>
				</div>
			</th>


												
											</tr>
										</thead>
										<tbody>
											<?php foreach($fbbttm_result as $data){

												$fname = (!empty($data['fbbttm_first_name'])) ? $data['fbbttm_first_name'] : $data['user_nicename'];
												$lname = (!empty($data['fbbttm_last_name'])) ? $data['fbbttm_first_name'] : '-';
												$fullname = $fname.' '.$lname;

												$address1 = (!empty($data['fbbttm_street_1'])) ? $data['fbbttm_street_1'] : '-';
												$address2 = (!empty($data['fbbttm_street_2'])) ? $data['fbbttm_street_2'] : '-';
												$city = (!empty($data['fbbttm_city'])) ? $data['fbbttm_city'] : '-';
												$pincode = (!empty($data['fbbttm_pincode'])) ? $data['fbbttm_pincode'] : '-';
												$phone = (!empty($data['fbbttm_phone'])) ? $data['fbbttm_phone'] : '--';
												$counname = (!empty($data['counname'])) ? $data['counname'] : '-';
												$statename = (!empty($data['statename'])) ? $data['statename'] : '-';

												$itemname = (!empty($data['fbbttm_food_name'])) ? $data['fbbttm_food_name'] : '-';
												$item_details = (!empty($data['fbbttm_food_details'])) ? $data['fbbttm_food_details'] : '-';
												$delivery_time = (!empty($data['fbbttm_food_time'])) ? $data['fbbttm_food_time'] : '-';
												$delivery_day = (!empty($data['fbbttm_booking_deliveryday'])) ? $data['fbbttm_booking_deliveryday'] : '-';
												?>
											<tr class="fbbttm_disable_appointment">
												
												<td>
												<?php echo esc_html($fullname);?>

												</td>	
												<td>
												<p>
													<?php echo esc_html($address1. ''. $address2);
    
													

													?>
												</p>
												<p><?php echo esc_html($city .', '.$statename.', '. $counname);?></p>
												<p><?php echo esc_html($pincode.','. $phone);?></p>
												</td>
												<td>
													<?php echo esc_html($delivery_time);?>
												</td>
												<td>
													<?php echo esc_html($delivery_day);?>
												</td>
												<td>
													<?php echo esc_html($itemname);?>
												</td>
												<td>
													<?php echo esc_html($item_details);?>
												</td>


											</tr>
										<?php } ?>
										</tbody>
											
				
									</table>
								</form>
							</div>

							<?php

									// Pagination
							        $page_links = paginate_links(array(
							            'base' => add_query_arg('paged', '%#%'),
							            'format' => '',
							            'prev_text' => __('&laquo;','foodboxbooker'),
							            'next_text' => __('&raquo;','foodboxbooker'),
							            'total' => $total_pages,
							            'current' => $current_page
							        ));

							        if ($page_links) {
							            echo '<div class="tablenav"><div class="tablenav-pages">' . esc_url($page_links) . '</div></div>';
							        }
							        
							?>
						</section> 
					<?php 
						}
					?>
				</div>
			</div>
		</div>
	  </main>
	<?php }	?>