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

$courier_id=$_POST["courier_id"];


/*Initially Array Variable Start*/
    $error_message=array();
    $awb_3pl_number=array();
    
/*Initially Array Variable End*/



if(isset($_POST["Import"])){
 try{
		$filename=$_FILES["file"]["tmp_name"];
		
		 if($_FILES["file"]["size"] > 0)
		 {
		  	$file = fopen($filename, "r");
		  	$ri=0;
	         while (($emapData = fgetcsv($file, 70000, ",")) !== FALSE)
	         {
	             if($emapData==''){
	                 $error_message[]="Empty Sheet";
	                 break;
	             }
	             
    	             if($ri>0)
    	             {
    	             /******************** Select AWB Number Available Or Not Details **************************/
    	                    if($emapData[1]!="")
    	                    {
    	                            $awbCode=$emapData[1];
            	                    $statement = $pdo->prepare("SELECT * FROM `tbl_awb` WHERE awb_code=?");
                                    $statement->execute(array($awbCode));
                                    $rowcount = $statement->rowCount();
                                    
            	                 	if($rowcount>0)
            	                 	{
            	                 	    	$error_message[]="Row Number $ri 3PL AWB Number $awbCode Already Exist...";
            	                 	}
            	                 	else
            	                 	{
            	                 	        $awb_3pl_number[]=$awbCode;
            	                 	} 
    	                    }
    	                    else
    	                    {
    	                        $error_message[]="Row Number $ri 3PL AWB Number Can't Be Empty...";
    	                    }
    				}
    				$ri++;
	         }
	         fclose($file);
	         
	                    if(count($error_message)>0)
                            {
                            ?>
                               <center><h3>Back to 3PL AWB Bulk Upload &emsp;&emsp;<a href="awbBulkUpload.php" class="btn btn-success btn-md">Back</a></h3></center>
    <div class="alert alert-danger alert-dismissible"  style='margin-left: 2%;margin-right: 2%;overflow:scroll;'>
		    <table class="table table-responsive" >
                            		        <tr>
                            		        <th>S.No</th>
                            		        <th>Status</th>
                            		        </tr>
                            		        <?php $ii=0;
                            foreach($error_message as $msg)
                            { ?>
                                 <tr>
                            		    <td><?php echo $ii+1 ?></td>
                            		    <td><?php echo $msg ?></td>
                            		    </tr>
                            <?php $ii++;  }?>
                                        </tbody>
                                    </table></center>
                                        <?php         
                                
                            }
                                        else {?>
                                        
                                        
                    <center><h3>Back to 3PL AWB List &emsp;&emsp;<a href="awbList.php" class="btn btn-success btn-md">Back</a></h3></center>
    <div class="alert alert-success alert-dismissible"  style='margin-left: 2%;margin-right: 2%;overflow:scroll;'>
		    <table class="table table-responsive" >
                    		        <tr class="table-success">
                    		        <th>S.No</th>
                    		        <th>3PL Number</th>
                    		        <th>Status Count <?=count($awb_3pl_number)?></th>
                    		        <!--<th>Courier Partner Name</th>-->
                    		        </tr>
                    		        <tbody>
                                
                                <?php 
                                $k=0;
                                    foreach($awb_3pl_number as $awb)
                                    {
                    	             $statement = $pdo->prepare("INSERT INTO tbl_awb (`awb_code`, `status`,`courier_id`) VALUES (?,'0',?)");
                                     $result= $statement->execute(array($awb,$courier_id));
                                ?>
                    	           
                                    	        <tr class="table-active">
                                    		    <td><?php echo $k+1 ?></td>
                                    		    <td><?php echo $awb ?></td>
                                    		    <td><?php echo  "<a href='#'class='btn btn-primary btn-xs'>Available</a> ";?></td>
                                    		    </tr>
                    	             <?php     
                    	             
                                         $k++;
                                    }
                                    ?>  
                                    </tbody>
                                    </table></center>
                                    <?php
                    	               }
                    	}	 
                    } 
                    
       catch(Exception $e) 
    {
        echo "<center><h3>Back to 3PL AWB Bulk Upload &emsp;&emsp;<a href='awbBulkUpload.php' class='btn btn-success btn-md'>Back</a></h3></center>";
        echo "<div class='alert alert-danger alert-dismissible' style='margin-left: 2%;margin-right: 2%;margin-top: 22%;'>";
        echo '<center>Message: ' .$e->getMessage();
        echo '<br>Error found in uploaded CSV file</center>';
        echo '</div>';
    }             
                    
}
                     ?> 
<?php require_once('footer.php'); ?>