<?php 
ob_start();
session_start();
include("../inc/config.php");
include "../inc/session.php";
if(isset($_POST['type']) && $_POST['type']=='scanning_status_update_api'){
    try{
        $pdo->beginTransaction(); 
        $updated_ids = implode(',', $_POST['updated_ids']);
        //$updated_box_ids = implode(',', $_POST['updated_awb_box']);
        $manifest_id= $_POST['manifest_id'];

        $update_status=$_POST['update_status'];
        $update_status_name=$_POST['update_status_name'];
        $status_location=$_POST['status_location'];
        
        $to_branch_name = $_POST['to_branch_name'] ?? '';
        $status_location_name=$_POST['status_location_name'];
        $status_description=$_POST['status_description']." ".$to_branch_name;
        $status_date=$_POST['status_date'];
        
        $from_branch=0;
        $to_branch=$_POST['to_branch'];
        $coloader=0;
        $vehicle_no=$_POST['vehicle_no'];
        $driver_name=$_POST['driver_name'];
        $driver_mobile=$_POST['driver_mobile'];
        
        $count=count($_POST['updated_ids']);
        $cur_date=date('Y-m-d');
        
        $statement1 = $pdo->prepare("UPDATE manifest SET manifest_status=1,incoming_ids='$updated_ids',missing_ids='',update_date='$cur_date' WHERE id=".$manifest_id);
        $statement1->execute();
        
        $statement1 = $pdo->prepare("UPDATE pickup_entry SET status_code='$update_status',status='$update_status_name',last_updated_status_date='$status_date',delivered_date='$status_date' WHERE id in ($updated_ids)");
        $statement1->execute();
       
       foreach($_POST['updated_ids'] as $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 ($ids,$session_id,'$status_location_name','$status_location','$update_status_name',$update_status,'$status_description','','','','','','$status_date','$status_date')");
        $result= $statement1->execute();

       }
        
        $pdo->commit();
        $response['error']=false;
        $response['meassge']='Status Updated Successfully: ';
        $response['manifest_id']=$manifest_id;
    }catch(exception $e){
        $pdo->rollBack();
        $response['error']=true;
        $response['meassge']="Something Went Wrong";
        error_log($e);
    }
    print_r(json_encode($response));
}
?>