<?php 
session_start();
include("inc/config.php");
/*select the login details for checking client and admin*/
if($_SESSION['user']['role']=='Modicare')
{
$clientId=$_SESSION['user']['id'];
$clientLocation=$_SESSION['user']['location'];
$clientType=$_SESSION['user']['role'];
}else
{
$session_id=$_SESSION['user']['id'];
$statement = $pdo->prepare("SELECT * FROM `tbl_user` WHERE id=1");
$statement->execute();
$response = $statement->fetchAll(PDO::FETCH_ASSOC);
 foreach($response as $res)
 {
$clientId=$res['id'];
$clientLocation=$res['location'];
$clientType=$res['role'];
 }
}

$pickup_id=array();
$pickup_id =$_POST['pickup_id'];
// $pickup_id=explode(",",$pickup_id);
$entry_date =$_POST['entry_date'];
$type =$_POST['type'];
$msg=array();
foreach($pickup_id as $id) 
{
    $statement = $pdo->prepare("SELECT pk.*,tt.created_at as last_updated, tt.status as t_status FROM `pickup_entry` pk INNER JOIN tbl_tracking tt ON pk.id=tt.pickup_entry_id WHERE pk.id=? ORDER BY tt.t_id DESC LIMIT 1");
    $statement->execute(array($id));
    $get_result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach($get_result as $row)
    {
        $last_updated=$row['last_updated'];
        $cscs_awb=$row['cscs_awb'];
    }
    
    $day=date_create($last_updated);
    $last_date=date_format($day,'Y-m-d');                        
    
     $up_day=date_create($entry_date);
     $updated_date=date_format($up_day,'Y-m-d');                        
    
    $updated_int=strtotime($updated_date);
    $last_date_int=strtotime($last_date);
    // $created_date = new DateTime($entry_date);
    // $entry_date = new DateTime($created_date);
    
    if($updated_int >= $last_date_int )
    { 
      $statement = $pdo->prepare("UPDATE `pickup_entry` SET status='picked',status_code='2' WHERE id=?");
      $result=$statement->execute(array($id));
      
      if($result){
    	$statement = $pdo->prepare("INSERT INTO tbl_tracking (created_at,pickup_entry_id,user_id,location,status,status_code,description,remark) VALUES (?,?,?,?,?,?,?,?)");
    	$result1=$statement->execute(array($entry_date,$id,$clientId,$clientLocation,'picked','2','Shipment Successfully Picked','Material Safe...'));
            echo $updated_int;
            echo "\n";
            echo $last_date_int;
    	 $msg[]="<strong class='text-success'>Success</strong> <i class='fa fa-check-circle-o ' aria-hidden='true' style='font-size:18px;color:green;'></i> The AWB NO $cscs_awb is successfully picked on $updated_date";
      }
      else
      {
          $msg[]="<strong class='text-danger'>Failed</strong> <i class='fa fa-times-circle-o ' aria-hidden='true' style='font-size:18px;color:red;'></i> Failed to pickup AWB NO $cscs_awb  ";
      }
    }
    else
    {
      $msg[]="<strong class='text-danger'>Failed</strong> <i class='fa fa-times-circle-o ' aria-hidden='true' style='font-size:18px;color:red;'></i> For $cscs_awb pickup date should be greater than $last_date";
    }
}
echo'	<thead>';
echo "<tr>";
echo "<th>S.No</th>";
echo "<th>Status</th>";
echo "</tr>";
echo'</thead>';
echo '<tbody>';
$j=1;

foreach($msg as $messgae)
{
    echo "<tr>";
    echo "<td>".$j."</td>";
    echo "<td>$messgae</td>";
    echo "</tr>";
    $j++;
}
echo'</tbody>';  
// if($result)
// echo"<script>alert('Status Updated Successfully...');window.location = 'view-pickup-entry.php?status=2';</script>";
// else
// echo"<script>alert('Failed To Update...');window.location = 'view-pickup-entry.php?status=1';</script>";
?>