<?php 
ob_start();
session_start();
include "../inc/config.php";
$update_at = date("Y-m-d H:i:s");
$today_date = date('Y-m-d');
//$session_id = $_SESSION['users']['id'];

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[random_int(0, $charactersLength - 1)];
    }
    return $randomString;
}



if(isset($_POST['type']) && $_POST['type']=='drs_update_status'){
     try{
         
        $pdo->beginTransaction(); 
        
        
        for($i=0; $i<count($_POST['pickup_entry_id']); $i++){
            $pickup_entry_id = $_POST['pickup_entry_id'][$i];
            $pickup_status = $_POST['pickup_status'][$i];
            $remarks = $_POST['remarks'][$i];
            
            $statement = $pdo->prepare("UPDATE `pickup_entry` SET `remarks`=?, `status_code`=? WHERE id=?");
    	    $result = $statement->execute(array($remarks, $pickup_status, $pickup_entry_id));
    	    
    	    $user_id = "";
    	    $location = "";
    	    $location_id = "";
    	    $status = "";
    	    $status_code = "";
    	    $description = "";
    	    $remark = "";
    	    $receiver_name = "";
    	    $receiver_mobile = "";
    	    $relation = "";
    	    $reason = "";
    	    $delivery_date = $_POST['delivery_date'];
    	    
    	    
    	   // error_log("UPDATE `tbl_branch` SET `remarks`='$remarks', `status_code`='$pickup_status' WHERE id='$pickup_entry_id'");
    	    
    	    $statement1 = $pdo->prepare("INSERT INTO tbl_tracking (pickup_entry_id, user_id, location, location_id, status, 
    	                        status_code, description, remark, receiver_name, receiver_mobile, 
    	                        relation, reason, delivery_date, created_at) 
                        	                 VALUES (?,?,?,?,?,
                        	                 ?,?,?,?,?,
                        	                 ?,?,?,?)");
            $result= $statement1->execute(array($pickup_entry_id, $user_id, $location, $location_id, $pickup_status,
                                $pickup_status, $description, $remark, $receiver_name, $receiver_mobile,
                                $relation, $reason, $delivery_date, $update_at));
        }
           
            
            
            
        
        

        if($result){
            $last_inserted_id = $pdo->lastInsertId();
            $pdo->commit();
            $return_arr['message']='Pickup Entry Status Updated Successfully...';
            $return_arr['status']=200;

        }
        else
        {
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
     }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}



?>