<?php 

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

	$redirecturl = admin_url('admin.php?page=fbbttm_packagelist');

 	global $fbbttm_helperobj, $wpdb;

	 $viewid = $fbbttm_pack_id = $fbbttm_package_name = $fbbttm_fitem_price =  $fbbttm_package_price = $fbbttm_special_price = $fbbttm_total_day = $fbbttm_package_description = $fbbttm_f_id = $fbbttm_package_image = '';
	 // phpcs:disable WordPress.Security.NonceVerification.Recommended
 	if(isset($_REQUEST['view'])){
 		$viewid = sanitize_text_field(wp_unslash($_REQUEST['view']));



 	$fbbttm_resultdataall = 	$fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_TB_PACKAGELIST,'fbbttm_pack_id',$viewid);

 	if(!empty($fbbttm_resultdataall))
 	{
 		foreach($fbbttm_resultdataall as $fbbttm_resultdata){
 			$fbbttm_pack_id = $fbbttm_resultdata->fbbttm_pack_id;
 			$fbbttm_pack_status = $fbbttm_resultdata->fbbttm_pack_status;
 			$fbbttm_package_name = $fbbttm_resultdata->fbbttm_package_name;
 			$fbbttm_fitem_price = $fbbttm_resultdata->fbbttm_fitem_price;
 			$fbbttm_package_image = $fbbttm_resultdata->fbbttm_package_image;
 			$fbbttm_package_price = $fbbttm_resultdata->fbbttm_package_price;
 			$fbbttm_special_price = $fbbttm_resultdata->fbbttm_special_price;
 			$fbbttm_total_day = $fbbttm_resultdata->fbbttm_total_day;
 			$fbbttm_package_description = $fbbttm_resultdata->fbbttm_package_description;
 			$fbbttm_f_id = $fbbttm_resultdata->fbbttm_f_id;
 			}

 	}
   }

?>

<main class="fbbttm-main fbbttm-main-listing-container fbbttm-default-box" id="all-page-main-container">
<div class="fbbttm-sec-top"><h2 class="fbbttm-page-heading"><?php echo esc_html__( 'Add New', 'foodboxbooker' )?></h2></div>
<form method="post" name="packageinsert" id="packageinsert" class="foodboxbooker-form" enctype="multipart/form-data"> 
	<?php
	// Noncename needed to verify where the data originated
	wp_nonce_field( 'fbbttm_meta_box_nonce', 'fbbttm_meta_box_noncename' );
?>
<input type="hidden" name="hiddenajaxurl" id="hiddenajaxurl" value="<?php echo esc_url(FBBTTM_URL)."/main/foodboxbooker-packagelist-insert.php"; ?>">
	<input type="hidden" name="fbbttm_pack_id" id="fbbttm_pack_id" value="<?php echo esc_html($fbbttm_pack_id);?>">
	<input type="hidden" name="redirecturl" id="redirecturl" value="<?php echo esc_url($redirecturl);?>">
	<div id="result" style="display:none"></div>
	<div id="php_error" style="display:none"></div>
	<div class="row">
	<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
	<label for="pname"><?php echo esc_html__( 'Package Name', 'foodboxbooker' )?><strong class="error">*</strong></label> 
	<input type="text" name="fbbttm_package_name" id="fbbttm_package_name" class="form_data" value="<?php echo esc_html($fbbttm_package_name);?>" placeholder='Ex. Meal or more, offer ' >
	<span class="error fbbttm__error packname-error"></span>
	</div> 
<?php
	
	$posts = get_posts([
	  'post_type' => 'fbbttm_fooditems',
	  'post_status' => 'publish',
	  'numberposts' => -1
	  	// 'order'    => 'ASC'
   ]);	
?>
<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
 <label for="category"><?php echo esc_html__( 'Fooditem', 'foodboxbooker' )?></label> 
  <select name="fbbttm_f_id"  id="fbbttm_f_id">
   <option value="" disabled selected><?php echo esc_html__( 'Please add Fooditem', 'foodboxbooker' )?></option>
   <?php
   $price ='';
   	foreach($posts as $post)
   		{

   			$meta_data = get_post_meta( $post->ID, 'themetechmount_fooditems_list_data', true );

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

				$post_meta = get_post_meta($post->ID); 
	    	// put here input or textarea 

				$selected = '';

				if($fbbttm_f_id == $post->ID)
				{
					$selected = "selected";
				}

  		  $price =  (!empty($post_meta['fbbttmprice'][0]))? $post_meta['fbbttmprice'][0] : $price;
  		  
		  	?>
   			<option value="<?php echo esc_html($post->ID);?>" data-price="<?php echo esc_html($price);?>" <?php echo esc_html($selected);?>><?php echo esc_html($post->post_title);?></option>

   		<?php }
   ?>
  </select>
  <span class="error fbbttm__error fooditem-error"></span>
</div> 


<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
<label for="price"><?php echo esc_html__( 'Package Image', 'foodboxbooker' )?><strong class="error">*</strong></label> 
<input type="file" name="packge_image" id="packge_image" accept="image/*">
<?php if($fbbttm_package_image!=''){?>
<img src="<?php echo esc_url($fbbttm_package_image);?>" width='100px' height="100px"/>
<?php }?>
<input type="hidden" name="fbbttm_packge_image_al" id="fbbttm_packge_image_al" class="form_data" value="<?php echo esc_html($fbbttm_package_image);?>" placeholder='' readonly>
<span class="error fbbttm__error image-error"></span>
</div>
</div>
<div class="row">
<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
<label for="price"><?php echo esc_html__( 'Food Item Price : Per day meal price', 'foodboxbooker' )?><strong class="error">*</strong></label> 
<input type="text" name="fbbttm_fitem_price" id="fbbttm_fitem_price" class="form_data" value="<?php echo esc_html($fbbttm_fitem_price);?>" placeholder='' readonly>
<span class="error fbbttm__error pricename-error"></span>
</div>

<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
<label for="price"><?php echo esc_html__( 'Special Price for food item: ', 'foodboxbooker' )?></label> 
<input type="number" name="fbbttm_package_special_price" id="fbbttm_package_special_price" class="form_data" value="<?php echo esc_html($fbbttm_special_price);?>" placeholder=''>
<span class="error fbbttm__error spname-error"></span>
</div>	

<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
<label for="price"><?php echo esc_html__( 'Package Day', 'foodboxbooker' )?><strong class="error">*</strong></label><input type="number" name="fbbttm_total_day" id="fbbttm_total_day" class="form_data" value="<?php echo esc_html($fbbttm_total_day);?>" placeholder='Ex. 10,20,30,40'>
<span class="error fbbttm__error fbbttmday-error"></span>
</div>
</div>
<div class="row">
<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
<label for="price"><?php echo esc_html__( 'Package Price', 'foodboxbooker' )?><strong class="error">*</strong></label> 
<input type="text" name="fbbttm_package_price" id="fbbttm_package_price" class="form_data" value="<?php echo esc_html($fbbttm_package_price);?>" placeholder='' readonly>
<span class="error fbbttm__error packprice-error"></span>
</div>

<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12"> 
	<label for="note"><?php echo esc_html__( 'Descriprion', 'foodboxbooker' )?></label> 
	<textarea name="fbbttm_package_description" id="fbbttm_package_description" rows="6"><?php echo esc_html($fbbttm_package_description);?></textarea> 
</div>

<div class="fbbttm-form-field col-lg-3 col-md-6 col-sm-12">
<input type="submit" name="ssubmit" id="packageinsert_submit" value="<?php echo esc_html__('Save','foodboxbooker');?>" class="fbbttm-button button-top-sp"/> 
</div>

</div>
</form>

<div class="fbbttm-details-tbl-wraper">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 fbbttm-details-tbl" id="mytable">
<h2 class="fbbttm-page-heading"><?php echo esc_html__('Manage Package', 'foodboxbooker'); ?></h2>
<?php
  				
				global $wpdb, $table_fbbttm_packagelist, $fbbttm_pack_id ,$fbbttm_result ;
				
				$table_fbbttm_packagelist = $wpdb->prefix . 'fbbttm_packagelist';
				// phpcs:disable WordPress.Security.NonceVerification.Missing
				if (isset($_POST['fbbttm_pack_id'])) {
					$fbbttm_pack_id = sanitize_text_field(wp_unslash($_POST['fbbttm_pack_id']));
				}

				$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
    		$per_page = 5;
    		$offset = ($current_page - 1) * $per_page;	
    	
				$id='';
		
				$query = $wpdb->prepare(
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
	    "SELECT * FROM {$table_fbbttm_packagelist} where fbbttm_pack_id != %d  ORDER BY fbbttm_pack_id DESC  LIMIT %d OFFSET %d",$id, $per_page, $offset
	    );

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

				
				$countpage = $wpdb->prepare(
					// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
					"SELECT COUNT(*) FROM {$table_fbbttm_packagelist} where  fbbttm_pack_id !=%d",$id);
			  $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" id="fbbttm_service_table">

			<form method="post" name="bulk_action_form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<input type="hidden" name="action" value="delete_package">
		 	<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__('Package Id', '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="name" data-order="desc" >
							<div class="cell"><?php echo esc_html__('Package 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="name" data-order="desc" >
							<div class="cell"><?php echo esc_html__('Food 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="category" data-order="desc">
							<div class="cell"><?php echo  esc_html__('Food Price', '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="category" data-order="desc">
							<div class="cell"><?php echo esc_html__('Package Special Price', '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="category" data-order="desc">
							<div class="cell"><?php echo esc_html__('Package Days', '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="category" data-order="desc">
							<div class="cell"><?php echo esc_html__('Package Price', '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="category" data-order="desc">
							<div class="cell"><?php echo esc_html__('Edit', '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="<?php echo esc_html__('Delete', 'foodboxbooker'); ?>" class="fbbttm-button" onclick="return delete_confirm();"/>
							</div>
						</th>
					</tr>
				</thead>
				<?php 

				foreach ( $fbbttm_result as $row ){
				?>
				
				<tr>
				<td><input type="checkbox" aria-hidden="false" class="fbbttm-checkbox__original checkbox" name="checked_id[]" value="<?php echo esc_html($row['fbbttm_pack_id']); ?>"></td>
				<td><?php echo esc_html($row['fbbttm_pack_id']); ?></td>
				<td><?php echo esc_html($row['fbbttm_package_name']); ?></td>
				<td><?php echo esc_html(get_the_title($row['fbbttm_f_id'])); ?></td>
				<td><?php echo esc_html($row['fbbttm_fitem_price']); ?></td>
				<td><?php echo esc_html($row['fbbttm_special_price']); ?></td>
				<td><?php echo esc_html($row['fbbttm_total_day']); ?></td>
				<td><?php echo esc_html($row['fbbttm_package_price']); ?></td>
				
				<td><a href="?page=fbbttm_packagelist&view=<?php echo esc_html($row['fbbttm_pack_id']); ?>"><?php echo esc_html__('Edit Package','foodboxbooker');?></a></td>


				<td><input type="submit" name="delete" id="delete" value="<?php echo esc_html__('Delete', 'foodboxbooker'); ?>" class="fbbttm-button" onclick="return delete_confirm();" data-id="<?php echo esc_html($row['fbbttm_pack_id']); ?>"/><i class="fa fa-sharp fa-light fa-trash"></i></td>
				</tr>
				
				<?php } ?>
				
			</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>
 