<?php require_once('header.php');?>

<?php
if(isset($_POST['Import'])) {
   
	$valid = 1;

	if(empty($_POST['awb_no'])) {
		$valid = 0;
		$error_message .= 'AWB No can not be empty<br>';
		$awb='';
	}else{
	$awb=$_POST['awb_no'];
	}
	$awb_type=$_POST['awb_type'];
	$pod_type=$_POST['pod_type'];
	$delivery_date=$_POST['delivery_date'] ?? date('Y-m-d') ;

 
	$statement = $pdo->prepare("SELECT * FROM pickup_entry where $awb_type='$awb'");
							$statement->execute();
							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
						    $total = $statement->rowCount();
                            if($total>0)
                            {
                                foreach($result as $res)
                                {
                                    $res_id=$res['id'];
                                }
                               
                            }
                            else
                            {
                                $valid=0;
                                $error_message .= 'AWB No Does not Exist<br>'; 
                            }
                            
    $statementsss = $pdo->prepare("SELECT * FROM tbl_pod where awb_no='$awb'");
	$statementsss->execute();
	$resultsss = $statementsss->fetchAll(PDO::FETCH_ASSOC);
	$totalsss = $statementsss->rowCount();
    if($totalsss>0)
    {
       $valid=0;
       $error_message .= 'AWB Already Updated<br>'; 
       
    }
    
							

    if($valid==1)
    {
        if($pod_type==1)
        {
            $img_type='p';
        }
         elseif($pod_type==2)
        {
            $img_type='d';
        }
        
        $path = $_FILES['pod_file1']['name'];
             if($path!=''){
		    $path_tmp = $_FILES['pod_file1']['tmp_name'];  
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
		    $file_name = basename( $path, '.' . $ext );
		    $final_name1 = $awb.'-'.$img_type.'-1'.'.'.$ext;
		    move_uploaded_file( $path_tmp, 'img/POD/'.$final_name1 );
             }else
           {
               $final_name1='';
           }
            $path = $_FILES['pod_file2']['name'];
             if($path!=''){
		    $path_tmp = $_FILES['pod_file2']['tmp_name'];  
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
		    $file_name = basename( $path, '.' . $ext );
		    $final_name2 = $awb.'-'.$img_type.'-2'.'.'.$ext;
		    move_uploaded_file( $path_tmp, 'img/POD/'.$final_name2 );
             }else
           {
               $final_name2='';
           }
            $path = $_FILES['pod_file3']['name'];
             if($path!=''){
		    $path_tmp = $_FILES['pod_file3']['tmp_name'];  
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
		    $file_name = basename( $path, '.' . $ext );
		    $final_name3 = $awb.'-'.$img_type.'-3'.'.'.$ext;
		    move_uploaded_file( $path_tmp, 'img/POD/'.$final_name3 );
             }else
           {
               $final_name3='';
           }
            $path = $_FILES['pod_file4']['name'];
             if($path!=''){
		    $path_tmp = $_FILES['pod_file4']['tmp_name'];  
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
		    $file_name = basename( $path, '.' . $ext );
		    $final_name4 = $awb.'-'.$img_type.'-4'.'.'.$ext;
		    move_uploaded_file( $path_tmp, 'img/POD/'.$final_name4 );
             }else
           {
               $final_name4='';
           }
            $path = $_FILES['pod_file5']['name'];
             if($path!=''){
		    $path_tmp = $_FILES['pod_file5']['tmp_name'];  
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
		    $file_name = basename( $path, '.' . $ext );
		    $final_name5 = $awb.'-'.$img_type.'-5'.'.'.$ext;
		    move_uploaded_file( $path_tmp, 'img/POD/'.$final_name5 );
             }else
           {
               $final_name5='';
           }
        
    
		$statement = $pdo->prepare("INSERT INTO tbl_pod (awb_id,awb_no,awb_type,url_1,url_2,url_3,url_4,url_5,pod_type,type,client_id,location) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
		$statement->execute(array($res_id,$awb,$awb_type,$final_name1,$final_name2,$final_name3,$final_name4,$final_name5,$pod_type,'image',$session_id,""));
		
		    $update_status_name = "DELIVERED";
            $pickup_status = 5;
            // $delivery_date= date('Y-m-d');
		
		    $statement = $pdo->prepare("UPDATE `pickup_entry` SET  `pod_status`=?,status_code=?, status=?, last_updated_status_date=? WHERE id=?");
    	    $result = $statement->execute(array('Updated',$pickup_status,$update_status_name,$delivery_date, $res_id));
    	    
    	   /*Status Update Code Start*/ 
        /*$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, $user_id, $location, $location_id, $update_status_name,
                                $pickup_status, $description, $remark, $receiver_name, $receiver_mobile,
                                $relation, $reason, $delivery_date, $update_at));*/
    	   
    	   /*Status Update Code End*/
    	    
    	    
			
		$success_message = 'POD Upload is Done Successfully!';

		unset($_POST['awb_no']);
		unset($_POST['awb_type']);
		
    }
	
}
?>



<section class="content-header">
	<div class="content-header-left">
		<h1>POD Upload</h1>
	</div>

	<div class="content-header-right" style="margin-right: 1%;">
		<a href="list_pod.php" class="btn btn-danger btn-sm">List All</a>
	</div>
</section>

<section class="content">
	<div class="row">
		<div class="col-md-12">

			<?php if($error_message): ?>
			<div class="callout callout-danger">
				<p>
					<?php echo $error_message; ?>
				</p>
			</div>
			<?php endif; ?>

			<?php if($success_message): ?>
			<div class="callout callout-success">
				<p><?php echo $success_message; ?></p>
			</div>
			<?php endif; ?>

			<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
				<div class="box box-info">
					<div class="box-body">
							    	<div class="form-group">
							<label for="" class="col-sm-2 control-label">AWB No Type <span>*</span></label>
							<div class="col-sm-6">
								<select class="form-control" name="awb_type" id="awb_type">
							        <option value="cscs_awb">AWB</option>
							        <option value="ref_no">REF NO</option>
							        <option value="3pl_awb">3PL AWB</option>
							    </select>
							</div>
						</div>
				    	<div class="form-group">
    						<label for="" class="col-sm-2 control-label"><span id='awb_txt'>AWB No:</span> <span>*</span></label>
    						<div class="col-sm-6">
    							<input type="text" autocomplete="off" class="form-control" id="awb_no" name="awb_no" value="<?php if(isset($_POST['awb_no'])){echo $_POST['awb_no'];} ?>">
    						</div>
    						<div class="col-sm-4">
    						    <span id="ncheck2"></span><i id='fa_icon'></i>
    						</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">POD Type <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="pod_type" >
							        <!--<option value="0">Select Type</option>-->
							        <option value="1">Pickup POD</option>
							        <option value="2" selected>Delivery POD</option>
							    </select>
							</div>
						</div>
						<div class="form-group">
    						<label for="" class="col-sm-2 control-label"><span id='deli_date'>Delivery Date:</span> <span>*</span></label>
    						<div class="col-sm-6">
    							<input type="date" autocomplete="off" class="form-control" id="delivery_date" name="delivery_date" value="<?php if(isset($_POST['delivery_date'])){echo $_POST['delivery_date'];} ?>">
    						</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">POD FILE <span>*</span></label>
							<div class="col-sm-9" style="padding-top:5px">
								<input type="file" name="pod_file1">(Only IMG Format)
							</div>
						</div>
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">POD FILE - 2</label>
							<div class="col-sm-9" style="padding-top:5px">
								<input type="file" name="pod_file2">(Only IMG Format)
							</div>
						</div>
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">POD FILE - 3</label>
							<div class="col-sm-9" style="padding-top:5px">
								<input type="file" name="pod_file3">(Only IMG Format)
							</div>
						</div>
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">POD FILE - 4</label>
							<div class="col-sm-9" style="padding-top:5px">
								<input type="file" name="pod_file4">(Only IMG Format)
							</div>
						</div>
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">POD FILE - 5</label>
							<div class="col-sm-9" style="padding-top:5px">
								<input type="file" name="pod_file5">(Only IMG Format)
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label"></label>
							<div class="col-sm-6">
								<button type="submit" id='submit_btn' class="btn btn-success pull-left" name="Import">Submit</button>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>

</section>

<script>
$(document).ready(function(){
      $("#awb_type").change(function(){
              var awb=$(this).val();
             if(awb=='cscs_awb')
             {
                 $('#awb_txt').text('CSCS AWB NO:');
             }
             else if(awb=='3pl_awb')
             {
                 $('#awb_txt').text('3PL AWB NO:');
             }
             else if(awb=='ref_no')
             {
                 $('#awb_txt').text('REF NO:');
             }
             get_data();
            });
      $("#awb_no").keyup(function(){
             get_data();
            });
            
            
            function get_data()
            {
              var awb=$('#awb_no').val();
              var awb_type=$('#awb_type').val();
              $.ajax({
                  type:'POST',
                  url:'get_unique_awb.php',
                  data:{awb:awb,type:'pod',awb_type:awb_type},
                   dataType: 'json',
                    beforeSend: function() {
                        $("#ncheck2").text(''); 
                        // setting a timeout
                        $('#fa_icon').addClass('fa fa-spinner fa-spin');
                    },
                  success:function(response){
                      $("#fa_icon").removeClass("fa fa-spinner fa-spin");
                      if(response[0]['error']=='0')
                      {
                          $("#ncheck2").css('color','green'); 
                         $("#ncheck2").text(response[0]['txt']); 
                         $("#submit_btn").prop('disabled', false);
                         
                      }
                      else
                      {
                          $("#ncheck2").css('color','red'); 
                         $("#ncheck2").text(response[0]['txt']);
                         $("#submit_btn").prop('disabled', true);
                      }
                  }
                });
            }
            
});
</script>


<?php require_once('footer.php'); ?>