<?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(); 
        
    $from_branch=$_POST['from_branch'] ?? 0;    
        
    $unique_id = "1000";
    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'manifest'");
    $statement->execute();
    $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
    foreach ($memeber_id_result as $result) {
        $unique_id = $unique_id + $result['Auto_increment'];
    }
    
    $prefix='';
    $statement = $pdo->prepare("SELECT * FROM tbl_branch where id=$from_branch");
    $statement->execute();
    $branch_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    if($branch_data){
        $prefix=$branch_data[0]['prefix_for_manifest'];
    }
    
    
    $manifest_id = $prefix.$unique_id;
    
    
    
        $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'];
        
        $to_branch_name=$_POST['to_branch_name'];
        
        $status_description = $_POST['status_description']." ".$to_branch_name;
        $status_date = $_POST['status_date'];
        
        
        
        $to_branch=$_POST['to_branch'];
        $coloader=$_POST['coloader'];
        $vehicle_no=$_POST['vehicle_no'];
        $driver_name = $_POST['driver_name'];
        $driver_mobile=$_POST['driver_mobile'];
        
        $count = count($_POST['updated_ids']);
        

        $statement1 = $pdo->prepare("INSERT INTO manifest (`manifest_id`, `origin`, `destination`, `entry_date`, `no_of_entry`, 
                                    `coloader_id`, `vehicle_no`, `driver_name`, `driver_mobile_no`, manifest_type, ids) 
                                    VALUES (?,?,?,?,?,
                                    ?,?,?,?,?,?)");
        $result= $statement1->execute(array($manifest_id, $from_branch, $to_branch, $status_date, $count, 
                                        $coloader, $vehicle_no, $driver_name, $driver_mobile, 2, $updated_ids));
        
        error_log("INSERT INTO manifest (`manifest_id`, `origin`, `destination`, `entry_date`, `no_of_entry`, 
                                    `coloader_id`, `vehicle_no`, `driver_name`, `driver_mobile_no`, manifest_type, ids) 
                                    VALUES ($manifest_id, $from_branch, $to_branch, $status_date, $count, 
                                        $coloader, $vehicle_no, $driver_name, $driver_mobile, 2, $updated_ids)");
        
        $manifest_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();
        
        
       
       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));
}
?>