<?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);

// $status =$_POST['status'];
$r_name =$_POST['r_name'];
$r_mobile =$_POST['r_mobile'];
$r_relation=$_POST['r_relation'];
$r_delivery_date =$_POST['r_delivery_date'];
$s_date =$_POST['s_date'];
$reason =$_POST['reason'];
$remark =$_POST['remark'];
$status_value =$_POST['status'];
$update_pod=0;

                  if($status_value==4){ $status="In Transit";}
                  if($status_value==5){ $status="Received at Destination";}
                  if($status_value==6){ $status="Out for delivery";}
                  if($status_value==7){ $status="Delivered";}
                  if($status_value==8){ $status="Undelivery";}
                 

    $up_day=date_create($s_date);
    $updated_date=date_format($up_day,'Y-m-d'); 
    $updated_int=strtotime($updated_date);
    $status_message=array();
    $status_ref_no=array();
    $current_status=array();
    $update_status=array();
    $pic_status=array();
    $imageError=0;
    $selected_img=0;


// print_r($_POST);


if($status_value=='7')
{
    for($i=1;$i<6;$i++)
    {
        if (isset($_FILES['pod_file'.$i])&& !empty($_FILES['pod_file'.$i])) 
            {
                $selected_img++;
                $path = $_FILES['pod_file'.$i]['name'];
                if($path!=''){
                    $ext = pathinfo( $path, PATHINFO_EXTENSION );
                    $file_name = basename( $path, '.' . $ext );
                    if( $ext!='jpg' && $ext!='png' && $ext!='jpeg' && $ext!='JPG' && $ext!='PNG' && $ext!='JPEG') {
                        $status_message[]= 'Pod Image-'.$i.' You must have to upload jpg, jpeg or png file';
                        $imageError++;
                    }
                 } 
             }
          }
      }
// print_r($imageError);
$pk_count=count($pickup_id);

if($imageError==0){
 foreach($pickup_id as $id) 
{
 
    
$status_code="";
$ref_no="";
$c_status="";
    $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE id=?");
    $statement->execute(array($id));
    $rowcount = $statement->rowCount();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
 	foreach($result as $res)
 	{
 	    $status_code=$res['status_code'];
 	    $ref_no=$res['ref_no'];
 	    $c_status=$res['status'];
 	}
 	
 	$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'];
                $res_id=$row['id'];
            }
 	
 	            $day=date_create($last_updated);
                $last_date=date_format($day,'Y-m-d');                        
                $last_date_int=strtotime($last_date);
 	
                
 	 if($rowcount>0)
	 {
	     
	     if($updated_int >= $last_date_int){ 
	     
	     if($status_value!=7)
	     {
	        $r_delivery_date=""; 
	     }
	     
	      if($status_value>$status_code)
        {
        
            $statement = $pdo->prepare("UPDATE `pickup_entry` SET status='$status',status_code='$status_value' WHERE id=$id");
            $update_result=$statement->execute();
            $statement = $pdo->prepare("INSERT INTO tbl_tracking (pickup_entry_id,user_id,location,status,status_code,description,remark,receiver_name,receiver_mobile,relation,reason,delivery_date,created_at) VALUES ($id,$clientId,'$clientLocation','$status',$status_value,'','$remark','$r_name','$r_mobile','$r_relation','$reason','$r_delivery_date','$s_date')");
            $insert_result=$statement->execute();
            	$status_ref_no[]=$ref_no;
        		$current_status[]=$c_status;
        		$update_status[]=$status;
        		$pic_status[]=1;
        		$status_message[]="Status Updated Successfully...";
        		
        	if($status_value==7 && $selected_img>0)
        	{
        	    $update_pod=1;
        	}
        	
        		
        		
        }
        else if($status_value==7 && $status_code==8)
        {
            $statement = $pdo->prepare("UPDATE `pickup_entry` SET status='$status',status_code='$status_value' WHERE id=$id");
            $update_result=$statement->execute();
            $statement = $pdo->prepare("INSERT INTO tbl_tracking (pickup_entry_id,user_id,location,status,status_code,description,remark,receiver_name,receiver_mobile,relation,reason,delivery_date,created_at) VALUES ($id,$clientId,'$clientLocation','$status',$status_value,'','$remark','$r_name','$r_mobile','$r_relation','$reason','$r_delivery_date','$s_date')");
            $insert_result=$statement->execute();
            	$status_ref_no[]=$ref_no;
        		$current_status[]=$c_status;
        		$update_status[]=$status;
        		$pic_status[]=1;
        		$status_message[]="Status Updated Successfully...";
        		
        	if($status_value==7 && $selected_img>0)
        	{
        	    $update_pod=1;
        	}
        }
        else
 	    {
 	        $update_result=0;
 	        $insert_result=0;
 	        $status_ref_no[]=$ref_no;
 	        $current_status[]=$c_status;
        	$update_status[]=$status;
        	$pic_status[]=0;
 	        $status_message[]="Reference Number $ref_no Status already exists...";
 	    }
 	  } else{
 	      
 	      $update_result=0;
 	      $insert_result=0;
 	      $status_ref_no[]=$ref_no;
 	      $current_status[]=$c_status;
          $update_status[]=$status;
          $pic_status[]=0;
 	      $status_message[]="For AWB Number $cscs_awb Status update date should be greater than or equal to $last_date"; 
 	      }  
	 }
	 else
    {
          $update_result=0;
 	      $insert_result=0;
 	      $status_ref_no[]="---";
 	      $current_status[]="---";
          $update_status[]=$status;
          $pic_status[]=0;
          $status_message[]="Reference Number $ref_no is Invalid...";
    }
}
}
               
        if($update_pod==1)
        {
            $pod_type=2;
            $img_type='d';
            $awb_type='cscs_awb';
           $awb=$cscs_awb;
                         for($i=1;$i<6;$i++)
                        {       
                                $path = $_FILES['pod_file'.$i]['name'];
                                 if($path!=''){
                    		    $path_tmp = $_FILES['pod_file'.$i]['tmp_name'];  
                                $ext = pathinfo( $path, PATHINFO_EXTENSION );
                    		    $file_name = basename( $path, '.' . $ext );
                    		    $final_name = $awb.'-'.$img_type.'-'.$i.'.'.$ext;
                    		    move_uploaded_file( $path_tmp, 'img/POD/'.$final_name );
                                 }else
                              {
                                  $final_name='';
                              }
                              
                              if($i==1){ $final_name1=$final_name; }
                              if($i==2){ $final_name2=$final_name; }
                              if($i==3){ $final_name3=$final_name; }
                              if($i==4){ $final_name4=$final_name; }
                              if($i==5){ $final_name5=$final_name; }
                        }       
            
          $statement = $pdo->prepare("INSERT INTO tbl_pod (awb_id,awb_no,awb_type,url_1,url_2,url_3,url_4,url_5,pod_type,type,client_id,location) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
		   $statement->execute(array($res_id,$awb,$awb_type,$final_name1,$final_name2,$final_name3,$final_name4,$final_name5,$pod_type,'image',$clientId,$clientLocation));
}
                            echo'	<thead>';
						    echo'	<tr>';
							echo'	<th width="30">SL</th>';
							echo'   <th>Ref No</th>';
							echo'	<th>Curent Status</th>';
							echo'	<th>Update Status</th>';
							echo'	<th>Status</th>';
							echo'	<th>Message</th>';
							echo    '</tr>';
						    echo    '</thead>';
						    echo    '<tbody>';
                $i=0;$j=1;
                        foreach ($status_message as $msg) { 
                            if($pic_status[$i]==1){ $btn="<button class='btn btn-primary'><i class='fa fa-check'></i></button>"; }else{ $btn="<button class='btn btn-danger'><i class='fa fa-times'></i></button>"; }
                            echo    '<tr>';
                            echo    '<td>'. $j.'</td>';
                            echo    '<td>'. $status_ref_no[$i] .'</td>';
                            echo    '<td>'. $current_status[$i] .'</td>';
                            echo    '<td>'. $update_status[$i] .'</td>'; 
                            echo    '<td>'. $btn .'</td>';
	                        echo    '<td>'. $msg .'</td>';
	                        echo    '</tr>';
	                        $i++;$j++;
                    }
                            echo'</tbody>';  
  
 
 /* 
  $response[] = array("status_ref_no"=>$status_ref_no, "current_status"=> $current_status, "update_status"=> $update_status, "status_message"=> $status_message);
echo json_encode($response);*/
?>