<?php 
ob_start();
session_start();
include("../inc/config.php");
include "../inc/session.php";
$clientId=$session_id;
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']);
        
        $update_status=$_POST['update_status'];
        $update_status_name=$_POST['update_status_name'];
        $status_location=$_POST['status_location'];
        $status_location_name=$_POST['status_location_name'];
        $status_description=$_POST['status_description'];
        $status_date=$_POST['status_date'];
        
        

        /*$statement1 = $pdo->prepare("INSERT INTO status_histry (`date`, `location`, `description`, `vehicle_no`, `driver_name`, `driver_mobile`, `status`,`branch_name`,pickup_id,box_id,verify_status) 
        VALUES ('$status_date','$status_location','$status_description','$vehicle_id','$driver_name','$driver_mobile','$update_status','$branch_name','$updated_ids','$updated_box_ids','final')");
        $result= $statement1->execute();
        $ai_id = $pdo->lastInsertId();*/
        
        
        
        /*$s="UPDATE pickup_entry SET status='$update_status',last_updated_status_date='$status_date',delivered_date='$status_date' WHERE id in ($updated_ids)";*/
        $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();
        
        /*$s="UPDATE tbl_no_of_box SET status='$update_status',mf_id='$ai_id' WHERE id in ($updated_box_ids)";
        $statement1 = $pdo->prepare("UPDATE tbl_no_of_box SET status='$update_status',mf_id='$ai_id' WHERE id in ($updated_box_ids)");
        $statement1->execute();*/
        
        
        
        
       
       foreach($_POST['updated_ids'] as $ids){                 
           
        /*$statement = $pdo->prepare("SELECT id FROM tbl_no_of_box where pickup_entry_id=$ids group by status");
        $statement->execute();
        $totalCount = $statement->rowCount();
        $ship_status=1;
        if($totalCount>1){
            $ship_status=0;
        }
        
        $statement1 = $pdo->prepare("UPDATE pickup_entry SET ship_status='$ship_status' WHERE id=$ids");
        $statement1->execute();*/
        
        $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,$clientId,'$status_location_name','$status_location','$update_status_name',$update_status,'$status_description','','','','','','$status_date','$status_date')");
        $result= $statement1->execute();
        
        
        
        /*$s.="INSERT INTO tbl_tracking (pickup_entry_id,user_id,location,status,description,remark,receiver_name,receiver_mobile,relation,reason,delivery_date,created_at) 
                        	                 VALUES ($ids,$clientId,'$status_location','$update_status','$status_description','','','','','','$status_date','$status_date')";*/
       
       }
       
       /*if(isset($_POST['mf_id'])){
           $statement = $pdo->prepare("DELETE FROM status_histry WHERE id=?");
            $result=$statement->execute(array($_POST['mf_id']));
       }*/
       
        
        $pdo->commit();
        $response['error']=false;
        $response['meassge']='Status Updated Successfully: ';
    }catch(exception $e){
        $pdo->rollBack();
        $response['error']=true;
        $response['meassge']="Something Went Wrong";
        error_log($e);
    }
    print_r(json_encode($response));
}
?>