<?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']=='scanning_delivery_status_insert_api'){
    try{
        $pdo->beginTransaction(); 
        
        
        $insert_ids = implode(',', $_POST['updated_ids']);
        $count = count($_POST['updated_ids']);
        
        $delivery_update_date = $_POST['delivery_update_date'];
        $update_status = $_POST['update_status'];
       
       
        $statementss = $pdo->prepare("SELECT * FROM tbl_status where id=?");
		$statementss->execute(array($update_status));
		$status_result = $statementss->fetchAll(PDO::FETCH_ASSOC);
		$status_count = $statementss->rowCount();
       
       
        
                $sheet_no="1000";
			    $statementss = $pdo->prepare("SHOW TABLE STATUS WHERE `Name` = 'tbl_delivery_status'");
                $statementss->execute();
                $memeber_id_result = $statementss->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($memeber_id_result as $resultss) {
			        $sheet_no=$sheet_no+$resultss['Auto_increment'];
                }
                $sheet_no = "VDS".$sheet_no; 
                


        $statement1 = $pdo->prepare("INSERT INTO tbl_delivery_run_sheet ( `delivery_status_no`, `date`, `shipment_ids`, `shipment_count`, `status`) 
                                            VALUES (?,?,?,?,?)");
        $result = $statement1->execute(array($sheet_no, $delivery_update_date, $insert_ids,$count,1));
        
        
        
        
        // Tracking and Pickup Entry - Update Status Starts Here
        
        foreach($_POST['updated_ids'] as $ids){     
            
            
            $update_status_name = $status_result[0]['name'];
            $pickup_status = $update_status;
            $user_id = "";
    	    $location = "";
    	    $location_id = "";
    	    $status = "";
    	    $status_code = "";
    	    $description = "";
    	    $remark = "";
    	    $receiver_name = "";
    	    $receiver_mobile = "";
    	    $relation = "";
    	    $reason = "";
    	    $delivery_date = $_POST['delivery_update_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, $session_id, $location, $location_id, $pickup_status,
                                $pickup_status, $description, $remark, $receiver_name, $receiver_mobile,
                                $relation, $reason, $delivery_date, $update_at));

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