<?php 
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
$update_at = date("Y-m-d H:i:s");


if(isset($_POST['type']) && $_POST['type']=='delivery_status_delivered'){
    try{
        $pdo->beginTransaction(); 
        
        
        $insert_ids = implode(',', $_POST['updated_ids']);
        $count = count($_POST['updated_ids']);
        
        $delivery_date = $_POST['delivery_date'];
        
        

        
        // Tracking and Pickup Entry - Update Status Starts Here
        
        foreach($_POST['updated_ids'] as $ids){     
            
            
            $update_status_name = "DELIVERED";
            $pickup_status = 5;
            $user_id = $session_id;
    	    $location = "";
    	    $location_id = "";
    	    $status = $update_status_name;
    	    $description = $update_status_name;
    	    $remark = "";
    	    $receiver_name = "";
    	    $receiver_mobile = "";
    	    $relation = "";
    	    $reason = "";
    	    $delivery_date = $_POST['delivery_date'];
        
        
        $statement1 = $pdo->prepare("UPDATE pickup_entry SET status_code=?, status=?, last_updated_status_date=? WHERE id=?");
        $statement1->execute(array($pickup_status, $update_status_name, $delivery_date, $ids));
            
            
        $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($ids, $user_id, $location, $location_id, $update_status_name,
                                $pickup_status, $description, $remark, $receiver_name, $receiver_mobile,
                                $relation, $reason, $delivery_date, $update_at));

       }
        
        // Tracking and Pickup Entry - Update Status Endss Here
        
        
        
        
        
       
        $pdo->commit();
        $response['error']=false;
        $response['meassge']='Delivery Status Updated Successfully: ';
    }catch(exception $e){
        $pdo->rollBack();
        $response['error']=true;
        $response['meassge']="Something Went Wrong";
        error_log($e);
    }
    print_r(json_encode($response));
}
?>