<?php
include("header_bulk_uploads.php");



// if(isset($_POST["test"])){
//   // $pics_data[]=$_FILES['pod_pics']['name'];
//     $total = $_FILES['pod_pics']['name'];
    
    
//     print_r($_FILES['pod_pics']['name']);
// }


if(isset($_POST["Import"])){
		
		$filename=$_FILES["file"]["tmp_name"];
		
            $failure_data=array();
            $failed_count=0;
		 if($_FILES["file"]["size"] > 0)
		 {
		  	$file = fopen($filename, "r");
		  	$ri=0; $j=0;
		  	$rc=0;
		  	$url_1='';$url_2='';$url_3='';$url_4='';$url_5='';
	         while (($emapData = fgetcsv($file, 50000, ",")) !== FALSE)
	         {
	             $valid=1;
	             if($emapData==''){
	                 break;
	             }
	             if($ri>0 && $emapData!=''){
	                 $rc++;
	             /******************** Select Pincode Details **************************/
	                    $awb_no=$emapData[0];
	                    $pod_type=$emapData[1];
	                    $url_1=$emapData[2];
	                    $url_2=$emapData[3];
	                    $url_3=$emapData[4];
	                    $url_4=$emapData[5];
	                    $url_5=$emapData[6];
	                    
	               //     $statement = "SELECT * FROM `tbl_pod` WHERE awb_no='$awb_no' AND pod_type='$pod_type'";
	               //  	$response=mysqli_query($conn,$statement);
	               //  	$rowcount=mysqli_num_rows($response);
	               //  	if($rowcount>0)
	               //  	{
	               //  	    $valid=0;
	                 	   
	               //  	}
	                 	if(!empty($url_1)){
	                 	$statement = "SELECT * FROM `tbl_pod` WHERE url_1='$url_1' OR url_2='$url_1' OR url_3='$url_1' OR url_4='$url_1' OR url_5='$url_1'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 	if($rowcount>0)
	                 	{
	                 	    $valid=0;
	                 	    $error++;
	                 	    $error_msg[]=$rc.'.The image name '.$url_1.' already exist at row'.$ri;
	                 	    $rc++;
	                 	}
	                 	}
	                 	if(!empty($url_2)){
	                 	$statement = "SELECT * FROM `tbl_pod` WHERE url_1='$url_2' OR url_2='$url_2' OR url_3='$url_2' OR url_4='$url_2' OR url_5='$url_2'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 	if($rowcount>0)
	                 	{
	                 	    $valid=0;
	                 	    $error++;
	                 	    $error_msg[]=$rc.'.The image name '.$url_2.' already exist at row'.$ri;
	                 	    $rc++;
	                 	}
	                 	}
	                 	if(!empty($url_3)){
	                 	$statement = "SELECT * FROM `tbl_pod` WHERE url_1='$url_3' OR url_2='$url_3' OR url_3='$url_3' OR url_4='$url_3' OR url_5='$url_3'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 	if($rowcount>0)
	                 	{
	                 	    $valid=0;
	                 	    $error++;
	                 	    $error_msg[]=$rc.'.The image name '.$url_3.' already exist at row'.$ri;
	                 	    $rc++;
	                 	}
	                 	}
	                 	if(!empty($url_4)){
	                 	$statement = "SELECT * FROM `tbl_pod` WHERE url_1='$url_4' OR url_2='$url_4' OR url_3='$url_4' OR url_4='$url_4' OR url_5='$url_4'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 	if($rowcount>0)
	                 	{
	                 	    $valid=0;
	                 	    $error++;
	                 	    $error_msg[]=$rc.'.The image name '.$url_4.' already exist at row'.$ri;
	                 	    $rc++;
	                 	}
	                 	}
	                 	if(!empty($url_5)){
	                 	$statement = "SELECT * FROM `tbl_pod` WHERE url_1='$url_5' OR url_2='$url_5' OR url_3='$url_5' OR url_4='$url_5' OR url_5='$url_5'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 	if($rowcount>0)
	                 	{
	                 	    $valid=0;
	                 	    $error++;
	                 	    $error_msg[]=$rc.'.The image name '.$url_5.' already exist at row'.$ri;
	                 	    $rc++;
	                 	}
	                 	}
	                 
	    if($valid==1){  
	        $paths[] = $_FILES['pod_pics']['name'];
	        $path_tmp[] = $_FILES['pod_pics']['tmp_name'];  
	        $paths_count=count($_FILES['pod_pics']['name']);
	        
	        for($i=0;$i<$paths_count;$i++)
	        {
	            if($paths[$i]==$url_1)
	            {
	                move_uploaded_file( $path_tmp[$i], 'img/POD/'.$paths[$i] );
	            }
	            if($paths[$i]==$url_2)
	            {
	                move_uploaded_file( $path_tmp[$i], 'img/POD/'.$paths[$i] );
	            }
	            if($paths[$i]==$url_3)
	            {
	                move_uploaded_file( $path_tmp[$i], 'img/POD/'.$paths[$i] );
	            }
	            if($paths[$i]==$url_4)
	            {
	                move_uploaded_file( $path_tmp[$i], 'img/POD/'.$paths[$i] );
	            }
	            if($paths[$i]==$url_5)
	            {
	                move_uploaded_file( $path_tmp[$i], 'img/POD/'.$paths[$i] );
	            }
	        }
	       
	                   
    	        $statement ="INSERT INTO tbl_pod (`awb_no`, `url_1`, `url_2`, `url_3`, `url_4`, `url_5`, `pod_type`, `type`, `client_id`, `location`) 
    	          VALUES ('$emapData[0]','$url_1','$url_2','$url_3','$url_4','$url_5','$pod_type','image',' ',' ')";
    	        $result=$conn->query($statement);
	           $j++;
	               }
	               if($ri>0 && ($result==0))
				    {
				        $failure_data[]=$emapData;
				        $failed_count++;
				    }
	 	                   }
				        $ri++;
		    }
			
	         fclose($file);
	          // throws a message if data successfully imported to mysql database from excel file
	        
	        if($failed_count==0){
	            echo "<script type=\"text/javascript\">
	         	alert(\" $j POD successfully Imported.$failed_count POD Failed to Update...\");	</script>";
	        }
	        if($error>1)
	        {?>
	            <html>
	                <head>
	                    <title>
	                        Results - Pod Upload
	                    </title>
	                    <body>
	                        <center>
	                            <table>
	                                <tr>
	                                <th>S.no</th>
	                                <th>Error</th>
	                               </tr>
	                               <tbody>
	                                   <?php foreach($error_msg as $msgs){ ?>
	                                   <tr>
	                                       <td>
	                                           <?php echo $msgs; ?>
	                                       </td>
	                                   </tr>
	                                   <?php } ?>
	                               </tbody>
	                            </table>
	                        </center>
	                    </body>
	                </head>
	            </html>
	       <?php }
			 //close of connection
			mysqli_close($conn); 
				
	}	 
} 
 ?> 