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

	$redirecturl = admin_url('admin.php?page=fbbttm_booking_list');
	$nameErr = $lnameErr = $emailErr = $phone_err = $fbbttm_date_err = "";    

	global $wpdb, $table_fbbttm_customers, $fbbttm_user_id, $fbbttm_user_firstname, $fbbttm_user_lastname, $fbbttm_user_email, $fbbttm_user_phone, $fbbttm_user_note, $fbbttm_user_dt, $name, $lname , $email , $phone , $note , $fbbttm_date;

// 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__('Booking List', 'foodboxbooker'); ?></h2>
 	<?php 
 	global $wpdb;
						
	$table_foodbooking = FBBTTM_TB_FOODBOOKING;
	$table_fbbttm_delivery_address  = FBBTTM_DELIVERY_ADDRESS;

	$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
    $per_page = 5;
    $offset = ($current_page - 1) * $per_page;	
						
	$fbtbid  = '';
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
	$query = $wpdb->prepare(
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	    "SELECT * FROM $table_foodbooking AS FB LEFT JOIN $table_fbbttm_delivery_address AS da ON da.fbbttm_user_id  = FB.fbbttm_user_id where  FB.fbbttm_fdbid != %d ORDER BY FB.fbbttm_fdbid DESC LIMIT %d OFFSET %d",$fbtbid, $per_page, $offset
	    );
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching 
	$fbbttm_result = $wpdb->get_results(
		//phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$query
		, ARRAY_A);

	$countquery = $wpdb->prepare(
	    "SELECT * FROM $table_foodbooking AS FB LEFT JOIN $table_fbbttm_delivery_address AS da ON da.fbbttm_user_id  = FB.fbbttm_user_id where  FB.fbbttm_fdbid != %d   ORDER BY FB.fbbttm_fdbid DESC",$fbtbid
	    );

	$countpage = $wpdb->get_results($countquery //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		, ARRAY_A);

 	$total_items = count($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_booking" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		 	<input type="hidden" name="action" value="delete_booking">
		  	 <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_1 fbbttm-table-column is-leaf fbbttm-table__cell">
				<input type="checkbox" aria-hidden="false" class="fbbttm-checkbox__original" name="checked" value="" id="select_all">
			</th>
			<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="email" data-order="desc">
				<div class="cell"><?php echo esc_html__('Email', '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__('Grand Total', '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__('Date', '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="pmethod" data-order="desc" >
		<div class="cell"><?php echo esc_html__('Payment Method', '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="pstatus" data-order="desc" >
<div class="cell"><?php echo esc_html__('Payment Status', '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 deleteall" id="delete" data-order="desc" >
													<div class="cell">

													
													<input type="submit" name="deleteall" id="deleteall" value="Delete" class="fbbttm-button" onclick="return delete_confirm();"/>
													</div>
												</th>
											</tr>
										</thead>
										<?php 
										
											
				foreach ( $fbbttm_result as $row ){
					$fbbttm_user_id = $row['fbbttm_user_id'];
					$fbbttm_fdbid  = $row['fbbttm_fdbid'];
					$fbbttm_payment_method  = $row['fbbttm_payment_method'];
					$fbbttm_payment_staus  = $row['fbbttm_payment_status'];

					$fbbttm_payment_staus = (!empty($fbbttm_payment_staus)) ? 'Paid' : 'Cod paid';


					$fbbttm_booking_status = $row['fbbttm_booking_status'];
					$fbbttm_payment_method = $row['fbbttm_payment_method'];
					$fbbttm_grand_total = $row['fbbttm_grand_total'];
					$fbbttm_created_date = gmdate('Y-m-d',strtotime($row['fbbttm_created_date']));
					
					$user_info = get_user_meta($fbbttm_user_id);
					$user_info = get_userdata($fbbttm_user_id);

					
					$first_name = $row['fbbttm_first_name'];
					$username = $row['fbbttm_first_name'];
					$last_name = $row['fbbttm_last_name'];
					//$user_email = $user_info->user_email;
					$user_email = $row['fbbttm_user_email'];
					?>
											
					<tr class="fbbttm_booking_list">
						<td><input type="checkbox" aria-hidden="false" class="fbbttm-checkbox__original checkbox" name="checked_id[]" value="<?php echo esc_attr($fbbttm_fdbid); ?>"></td>
						<td><?php echo esc_attr($username); ?> </td>
						<td><?php echo esc_attr($user_email); ?></td>
						<td><?php echo esc_attr($fbbttm_grand_total); ?></td>
						<td><?php echo esc_attr($fbbttm_created_date); ?></td>
						<td><?php echo esc_attr($fbbttm_payment_method); ?></td>
						<td><?php echo esc_attr($fbbttm_payment_staus); ?></td>
						<td>
							
						<div class="fbbttm_viewlink">
						<a href="?page=fbbttm_booking_list&view=<?php echo esc_attr($fbbttm_fdbid); ?>"><?php echo esc_html__('View order info','foodboxbooker');?></a>
					</div>
					<div class="fbbttm_deletebutton">
							<input type="submit" name="delete" id="delete" value="Delete" class="fbbttm-button" onclick="return delete_confirm();"/><i class="fa fa-sharp fa-light fa-trash"></i><br>
						</div>
									</td>
								</tr>
							 <?php 
							}
						  ?>
					</table>

					<?php
						// Noncename needed to verify where the data originated
						wp_nonce_field( 'fbbttm_meta_box_nonce', 'fbbttm_meta_box_noncename' );
					?>

				</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 

	}

	if(!empty($view))	
	{

		global $fbbttm_orderinfoobj,$wpdb, $fbbttm_helperobj;

		$serverurl = (isset($_SERVER['REQUEST_URI']))?sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])):'';
		$mailurl = explode('&', $serverurl);
		// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
		$url =  sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])).$mailurl[0];

		$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$url";

		$id = sanitize_text_field(wp_unslash($_REQUEST['view']));
		$orderinfo = $fbbttm_orderinfoobj->fbbttm_get_orderinfo_byorder_key('',$id);


		echo '<main class="fbbttm-main fbbttm-main-listing-container fbbttm-default-box" id="all-page-main-container">

		<div class="fbbttm-backto-page"><a href="'.esc_url($actual_link).'">'.esc_html__('Back to Listing','foodboxbooker').'</a></div>
		
		<div class="fbbttm-details-tbl-wraper">

		<div class="container">';
		
		require_once FBBTTM_DIR . 'main/views/foodboxbooker-commonorderinfo.php';

		require_once FBBTTM_DIR . 'main/views/foodboxbooker-iteminfo.php';

		require_once FBBTTM_DIR . 'main/views/foodboxbooker-ordersummary.php';

		echo '</div></div></main>';


	}

	
	
?>
