<?php 
session_start();
$clientId=$_SESSION['user']['id'];
$clientLocation=$_SESSION['user']['location'];
include("inc/config.php");
// $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_value'];

$status_message=array();
$status_ref_no=array();
$current_status=array();
$update_status=array();
$pic_status=array();


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'];
 	}
 	
 	 if($rowcount>0)
	 {
	     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...";
        }
        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...";
        }
        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[]="---";
 	      $current_status[]="---";
          $update_status[]=$status;
          $pic_status[]=0;
          $status_message[]="Reference Number $ref_no is Invalid...";
    }
}   
               

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