
<?php 
include("inc/config.php");

ini_set('memory_limit', '2048M');
if($_POST['type']=='confirm'){
    $content='';
   // $awb_number=$_POST['awb_number'];
    for($i=0;$i<count($_FILES['files']['name']);$i++){
        $awb = explode(".",  $_FILES["files"]["name"][$i]);
        
            
            
     
    $statement = $pdo->prepare("SELECT * FROM tbl_pickup_entry where cscs_awb='$awb[0]'");
	$statement->execute();
	$awb_result = $statement->fetchAll(PDO::FETCH_ASSOC);

    $statement = $pdo->prepare("SELECT * FROM tbl_pod where awb_no='$awb[0]'"); 
	$statement->execute();
	$pod_result = $statement->fetchAll(PDO::FETCH_ASSOC);    
        
        
    if($awb_result){ 
        if(!$pod_result){
            
            [$year, $month, $day] = explode('-', explode(' ', $awb_result[0]['created_at'])[0]);
            // Create dynamic folder structure
            $base_dir = "img/POD/$year/$month/$day";
            if (!is_dir($base_dir)) {
                mkdir($base_dir, 0777, true);
            }
            
            
            if(isset($_FILES['files']) && $_FILES["files"]["name"][$i]!=""){
                $extension = end(explode(".", $_FILES["files"]["name"][$i]));
                //$image = $awb[0].'-d-'.rand(10,1000000000).''.'.'.$extension;
                $image = $awb[0].''.'.'.$extension;
                $upload = move_uploaded_file($_FILES['files']['tmp_name'][$i], $base_dir.'/' . $image);
                $upload_image = $year."/".$month."/".$day."/".$image;   
            }
            else{
                $upload_image='';
            } 
        
        $statement = $pdo->prepare("INSERT INTO tbl_pod (awb_id,awb_no,awb_type,url_1,pod_type,type,client_id,location) VALUES (?,?,?,?,?,?,?,?)");
		$statement->execute(array($awb_result[0]['id'],$awb[0],'ref_no',$upload_image,2,'image',$awb_result[0]['client_id'],'Bangalore'));
		
		$r='UPDATED';
        $statement = $pdo->prepare("UPDATE tbl_pickup_entry SET pod_update_status=? WHERE id=?");
        $resultls=$statement->execute(array($r,$awb_result[0]['id']));
		
		
        }   
        
        $s='<span style="padding: 1px 5px;" class="btn btn-success btn-sm">Ready To Updated</span>';
        if($pod_result){$s='<span style="padding: 1px 5px;" class="btn btn-danger btn-sm">Already Updated</span>';}
        $conent.='<div class="col-sm-3 " style="padding: 2px 2px;">
                   <div class="success_div">
                    Awb Number : <b>'.$awb[0].'</b> <br>
                    Current Status : <b>'.$awb_result[0]['status'].'</b> <br>
                    Booking Date : <b>'.$awb_result[0]['created_at'].'</b> <br>';
            if($awb_result[0]['status']=='Delivered'){        
                     $conent.='Delivery Date : <b>'.$awb_result[0]['delivered_date'].'</b> <br>';
            }       
                    
                     $conent.='POD Status :'.$s.' 
                    </div>
               </div>';
    }else{
         $conent.='<div class="col-sm-3 " style="padding: 2px 2px;">
                   <div class="warning_div">
                        <center><b>'.$awb[0].'</b><br>Invalid AWB Number</center>
                   </div>
               </div>';
    }
    
    
    
    
    }
    $respone['data']=$conent;
    $respone['msg']="ganesan";
    echo json_encode($respone);$pdo = null;
    return 0;
    
    
}

?>