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

global $fbbttm_orderinfoobj, $fbbttm_helperobj,$fbbttm_emailobj,$fbbttm_allowed_html;
$currentuserid = get_current_user_id();

$orderdatabygroup = $fbbttm_orderinfoobj->fbbttm_get_orderlist_byuserid_groupby($currentuserid);

$orderdata = $fbbttm_orderinfoobj->fbbttm_get_orderlist_byuserid($currentuserid);

if(empty($orderdata)) 
{
   ?>
   <div class="fbbttm-orderinfo fbbttm-myaccountcontent">
        <p><?php echo esc_html__('There is no order ','foodboxbooker');?></p>
    </div>
   <?php 
}
else{
$userorder = $orderdata['orderinfo'];

$orderiteminfo = $orderdata['orderiteminfo'];
?>
<div class="fbbttm-orderinfo fbbttm-myaccountcontent">
    <?php $orinfocountpack = $orderdatabygroup['orderiteminfo']['packid'];
        if(!empty($orinfocountpack)){
            for($i=0;$i<count($orinfocountpack);$i++){
               $packcount = $orderdatabygroup['orderiteminfo']['packwise'][$orinfocountpack[$i]];
          ?>
         <div class="fbbttmpackinfo groupbypack">
            <div><h3><?php echo esc_html__('Package information','foodboxbooker');?></h3>

                <table class="fbbttm-packinfo-table">
                     <thead>
                            <tr>
                                <th><?php echo esc_html__('Package Status','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Package Name','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Food Item Name','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Food Time','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Package Day','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Food Item Price','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Special Price on Package','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Total Package Price','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Package Start Date','foodboxbooker');?></th>
                                <th><?php echo esc_html__('Package end Date','foodboxbooker');?></th>
                            </tr>
                        </thead>
                    <tbody>
                    <?php 

                    $packcal = count($packcount);
                    $lastval = $packcal -1;

    for($j=0;$j<count($packcount);$j++)
      {

            $fdbid =  $packcount[$j]->fbbttm_fdbid;
            $packid =  $packcount[$j]->fbbttm_pack_id;

            $fbitemid = $packcount[$j]->fbbttm_fbitem_id;

            $packname =  $packcount[$j]->fbbttm_pack_name;

            $foodname =  get_the_title($packcount[$j]->fbbttm_fd_id);
            $foodname = (!empty($packcount[$j]->fbbttm_food_name)) ? $packcount[$j]->fbbttm_food_name : $foodname;
            $food_details =  $packcount[$j]->fbbttm_food_details;


            $foodtime =  $packcount[$j]->fbbttm_food_time;
            $packday =  $packcount[$j]->fbbttm_package_day;
            $packdescription =  $packcount[$j]->fbbttm_package_description;
            $fooditemprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($packcount[$j]->fbbttm_fitem_price);
            $packspecialprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($packcount[$j]->fbbttm_special_price);
            $packtotalprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($packcount[$j]->fbbttm_package_price);

            $startdate = $packcount[$j]->fbbttm_food_startdate;
            $enddate = $packcount[$j]->fbbttm_food_enddate;
            // phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date 
            $currentdate = strtotime(date('Y-m-d'));
            $timestrapstartdate = strtotime(date($startdate));
            $timestrapenddate = strtotime(date($enddate));

                        if($currentdate >= $timestrapstartdate && $currentdate<= $timestrapenddate){
                            $packstatus = 'Running';
                        }
                        else if( $currentdate > $timestrapenddate)
                        {
                            $packstatus = 'Expired';
                            $renewlink ='';
                            $renewitemid ='';

                            if($lastval == $j)    
                              {
                                    
                                $renewlink =  '<a class="fbbttmreneworder" data-id='.esc_html($fbitemid).'>'.esc_html__('Renew','foodboxbooker').'</a>';  
                                $renewitemid =   $fbitemid;

                              }

                        }
                        else
                        {
                            $packstatus = 'Hold';   
                        }
                        // phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date 
                        $fbbttm_date1=date_create(date('Y-m-d'));

                        $fbbttm_date2=date_create($enddate);
                        $diff=date_diff($fbbttm_date1,$fbbttm_date2);
                        $countday = $diff->format("%a");
                        $remingday = $diff->format("%R%a days remaining");

                        if($countday<= 2){
                            $renewtext =  '<span class="fbbttmreneworder" data-id='.$fbitemid.'>'.esc_html__('Renew','foodboxbooker').'</a>';
                        }
                        else
                        {
                          $renewtext =  esc_html( $remingday);
                        }
                ?>
                        <tr>
                            <td><?php echo esc_html( $packstatus); ?>  </td>
                            <td><?php echo esc_html( $packname);?>    </td>
                            <td><?php echo esc_html( $foodname );?>    </td>   
                            <td><?php echo esc_html( $foodtime );?> </td>    
                            <td><?php echo esc_html( $packday );?>     </td>    
                            <td><?php echo esc_html( $fooditemprice );?></td>    
                            <td><?php echo esc_html( $packspecialprice );?></td>    
                            <td><?php echo esc_html( $packtotalprice );?></td>
                            <td><?php echo esc_html( $startdate );?></td>        
                            <td><?php echo esc_html( $enddate );?></td>
                        </tr>
                        <?php  
                              }
                          ?>
                        </tbody>
                        <?php
                             if(!empty($renewlink)){
                          ?>
                        <tfoot>
                          <tr>
                            <td colspan="12">
                              <div class="fbbttm-renewlink" data-id='<?php echo esc_html($renewitemid);?>'>
                            <?php  echo wp_kses($renewlink,$fbbttm_allowed_html);  ?>
                              </div>
                            </td>
                            
                          </tr>
                        </tfoot>
                        <?php } ?>
                </table> 
            </div>
         </div>
     <?php } }
     ?>
      </div>  
      <div class="fbbttm-orderenewblock">
    <div class="fbbttm-message fbbttm-packageaddcart-message"></div>
<div class="foodboxbooker-main" id="foodboxbooker-model-bg"></div>
        <div class="modal" id="foodboxbooker-model">
            <div class="header">
                <a class="cancel"><?php echo esc_attr__('X','foodboxbooker');?></a>
            </div>
            <div class="content">

                <div class="row packbooking-content " >
 <form name="fbbttm_add_to_cart_renew" id="fbbttm_add_to_cart_renew" method="post">

    <input type="hidden" name="fbbttm-packid" id="hiddenpackid" value=""/> 
    <input type="hidden" name="fbbttm-userid-renew" value="<?php echo esc_html(get_current_user_id());?>">

    <div class="fbbttm-packinfo-block" id="fbbttm-packinfo-block">

    </div>

    <div class="fbbttm-class-step-one">
        <label><?php echo esc_html__('Please choose Lunch, Dinner or Both','foodboxbooker');?></label>
        <label><?php echo esc_html__('Lunch','foodboxbooker');?> <input type="checkbox" class="food_time" name="food_time[]" value="lunch"> </label>
        <label><?php echo esc_html__('Dinner','foodboxbooker');?> <input type="checkbox" class="food_time" name="food_time[]" value="dinner"> </label>
        <div class="fbbttm-error fbbttm-deliverytime"></div>
    </div>

    <div class="fbbttm-class-step-two">
        <label><?php echo esc_html__('How often do you want your deliveries?','foodboxbooker');?></label>
        <label><?php echo esc_html__('Mon-Sun','foodboxbooker');?> <input type="radio" name="deliveryday" class="deliveryday"  value="Mon-Sun"> </label>
        <label><?php echo esc_html__('Mon-Fri','foodboxbooker');?> <input type="radio" name="deliveryday" class="deliveryday"  value="Mon-Fri"> </label>
        <label><?php echo esc_html__('Mon-Sat','foodboxbooker');?> <input type="radio" name="deliveryday" class="deliveryday"  value="Mon-Sat"> </label>
        <label><?php echo esc_html__('Custom','foodboxbooker');?> <input type="radio" name="deliveryday" class="deliveryday"  value="custom"> </label>


<?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) { ?>
                <label><?php echo esc_html($val);?></label>
                <input type="checkbox" name="deliday[]" value="<?php echo esc_html($key);?>" class="customdaycheckbox" checked />
            <?php }?>
        </div>
        <div class="fbbttm-error fbbttm-deliveryday"></div>
    </div>

    <div class="fbbttm-class-step-three">
        <?php  $enddate = date('Y-m-d', strtotime(date('Y-m-d') .' +1 day')); ?>
        <input type="date" name="startdate"  min="<?php echo esc_html($enddate);?>" />
     
         <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="addtocartrenew" value="<?php echo esc_html__('Add to Cart','foodboxbooker');?>"/>
        <div class="fbbttm-loader hide"></div> 
    </div>  
    <div class="fbbttm-error fbbttm-phperror"></div>
        <?php
    // Noncename needed to verify where the data originated
    wp_nonce_field( 'fbbttm_meta_box_nonce', 'fbbttm_meta_box_noncename' );
?>
  </form>
</div>

            </div>
        </div> 
</div>
   <div class="fbbttm-orderinfo fbbttm-myaccountcontent hide" >
            <h3><?php echo esc_html__('Order info','foodboxbooker');?></h3>

            <table>
            <thead>
            <tr>
            <th><?php echo esc_html__('Package Name','foodboxbooker');?></th>
            <th><?php echo esc_html__('Food Item Name','foodboxbooker');?></th>
            <th><?php echo esc_html__('Food Time','foodboxbooker');?></th>
            <th><?php echo esc_html__('Package Day','foodboxbooker');?></th>
            <th><?php echo esc_html__('Package Description','foodboxbooker');?></th>
            <th><?php echo esc_html__('Food Item Price','foodboxbooker');?></th>
            <th><?php echo esc_html__('Special Price on Package','foodboxbooker');?></th>
            <th><?php echo esc_html__('Total Package Price','foodboxbooker');?></th>
            <th><?php echo esc_html__('Package Start Date','foodboxbooker');?></th>
            <th><?php echo esc_html__('Package end Date','foodboxbooker');?></th>
            <th><?php echo esc_html__('Remaining Day','foodboxbooker');?></th>
            </tr>
            </thead>
            <tbody>
                   <?php          
                    foreach($orderiteminfo as $orderitem){
                        $fdbid =  $orderitem->fbbttm_fdbid;
                        $fbitemid = $orderitem->fbbttm_fbitem_id;
                        $packname =  $orderitem->fbbttm_pack_name;
                        $foodname =  get_the_title($orderitem->fbbttm_fd_id);
                        $foodtime =  $orderitem->fbbttm_food_time;
                        $packday =  $orderitem->fbbttm_package_day;
                        $packdescription =  $orderitem->fbbttm_package_description;
                        $fooditemprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderitem->fbbttm_fitem_price);
                        $packspecialprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderitem->fbbttm_special_price);
                        $packtotalprice = $fbbttm_helperobj->fbbttm_get_price_with_currency($orderitem->fbbttm_package_price);
                        $startdate = $orderitem->fbbttm_food_startdate;
                        $enddate = $orderitem->fbbttm_food_enddate;



                        $fbbttm_date1 = date_create(date('Y-m-d'));

                        $fbbttm_date2 = date_create($enddate);
                        $diff=date_diff($fbbttm_date1,$fbbttm_date2);
                        $countday = $diff->format("%a");
                        $remingday = $diff->format("%R%a days remaining");

                        if($countday<= 2){
                            $renewtext =  '<span class="fbbttmreneworder" data-id='.esc_html($fbitemid).'>'.esc_html__('Renew','foodboxbooker').'</a>';
                        }
                        else
                        {
                          $renewtext =  esc_html( $remingday );
                        }

                        ?>

                    <tr>

                    <td><?php echo esc_html( $packname );?></td>
                    <td><?php echo esc_html( $foodname );?></td>    
                    <td><?php echo esc_html( $foodtime );?></td>    
                    <td><?php echo esc_html( $packday );?></td>    
                    <td><?php echo esc_html( $packdescription );?></td>    
                    <td><?php echo esc_html( $fooditemprice );?></td>    
                    <td><?php echo esc_html( $packspecialprice );?></td>    
                    <td><?php echo esc_html( $packtotalprice );?></td>
                    <td><?php echo esc_html( $startdate );?></td>        
                    <td><?php echo esc_html( $enddate );?></td>
                    <td><?php echo esc_html( $renewtext );?></td>                

                    </tr>
                    
               <?php } ?>

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