<?php require_once('header.php'); 
//$client_id = $_POST['client_id'];
// $type=$_POST['type'];
$posted_courier_id = $_POST['courier_id'];

/*Initially Array Variable Start*/
    $error_message=array();
    $pick_ids=array();
    $cscs_awb=array();
    $c_status=array();
    $pl3_awb=array();
    // $references=array();

/*Initially Array Variable End*/


if(isset($_POST["Import"])){
 try{
		$filename=$_FILES["file"]["tmp_name"];
		
            $failed_count=0;
		 if($_FILES["file"]["size"] > 0)
		 {
		  	$file = fopen($filename, "r");
		  	$ri=0; $j=0;
	         while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
	         {
	             if($emapData==''){
	                 break;
	             }
	             if($ri>0 && $emapData!=''){
	             /**************** Check given CSCS AWB is already used or not ****************/
	                    $awb_value=$emapData[0];
	                    $type='cscs_awb';
	                    
	                    $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE $type=?");
                        $statement->execute(array($awb_value));
                        $rowcount = $statement->rowCount();
                        $response = $statement->fetchAll(PDO::FETCH_ASSOC);
	                    if($rowcount>0)
	                    {
	                    foreach($response as $res)
	                 	{
	                 	    $id=$res['id'];
	                 	    $courier_id=$res['courier_id'];
	                 	    $reference=$res['ref_no'];
	                 	}
	                 	$pick_ids[]=$id;
	                 	$cscs_awb[]=$emapData[0];
	                    }
	                    else
	                 	{
	                 	       $error_message[]="Row Number $ri CSCS AWB Number $awb_value is Invalid ";
	                 	}
	                 	
	                 	
	                  if(count($cscs_awb)>0)
	                 {
	                     foreach(array_count_values($cscs_awb) as $num => $value)
                        {
                            if($value>1)
                            {
                            	$error_msg[]=$num."Row Number ".$ri." CSCS AWB Number is having duplicate value of ".$value." times in our csv file";
                            }
                        }
	                 }
	                 	
	                 	
	               /**************** Check given 3PL AWB is already used or not ****************/  	
	                 	$given_value=$emapData[1];
	                    
	                    $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE 3pl_awb=?");
                        $statement->execute(array($given_value));
                        $rowcount = $statement->rowCount();
                        $response = $statement->fetchAll(PDO::FETCH_ASSOC);
	                    if($rowcount>0)
	                    {
	                     $error_message[]="Row Number $ri given 3PL AWB Number $given_value is already exists";
	                 	 $pl3_awb[]='';
	                    }
	                    else
	                 	{
	                 	  
	           //      	$statement = $pdo->prepare("SELECT * FROM `tbl_awb` WHERE status=0 and courier_id=? and awb_code=?");
            // 			$statement->execute(array($courier_id,$given_value));
            // 			$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            // 			$count_3pl = $statement->rowCount();
            			
            //             if($count_3pl>0)
            //             {
            //                 $pl3_awb[]=$given_value;
            //             }
            //      	 else{    
            //      	     $error_message[]="Row Number $ri 3PL AWB Number $given_value is Invalid ";
            //         	}
            
                                    $pl3_awb[]=$given_value;
	                   }
	               if(count($pl3_awb)>0)
	                 {
	                     foreach(array_count_values($pl3_awb) as $num => $value)
                        {
                            if($value>1)
                            {
                            	$error_msg[]=$num."Row Number ".$ri." 3PL AWB Number is having duplicate value of ".$value." times in our csv file";
                            }
                        }
	                 }
	             }
	                        $ri++;
			  }
	         fclose($file);
	         
	                    if(count($error_message)>0)
                            {
                            ?>
    <center><h3>Back to Pickup 3PL AWB Bulk Upload &emsp;&emsp;<a href="pickup-3pl-bulk-upload.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 Pickup list &emsp;&emsp;<a href="view-pickup-entry.php?status=2" 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>Status</th>
                    		        <th>CSCS AWB</th>
                    		        <th>3PL AWB</th>
                    		        <th>REF Number</th>
                    		        <th>Current Status</th>
                    		        </tr>
                    		        <tbody>
                                
                                <?php 
                                $k=0;
                                    foreach($pick_ids as $pid)
                                    {
                                            
                                            $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE id='$pid'");
                                            $statement->execute();
                                            $get_courier = $statement->fetchAll(PDO::FETCH_ASSOC);
                                            foreach($get_courier as $rows)
                                	           {
                                	            $courier_data=$rows['courier_id'];       
                                	           }
                                            
                                            if(empty($courier_data) || $courier_data=='')
                                            {
                                            $statement = $pdo->prepare("UPDATE pickup_entry SET 3pl_awb='$pl3_awb[$k]',courier_id='$posted_courier_id' WHERE id='$pid'");
                                            $result1=$statement->execute(); 
                                            }else
                                            {
                                            $statement = $pdo->prepare("UPDATE pickup_entry SET 3pl_awb='$pl3_awb[$k]' WHERE id='$pid'");
                                            $result1=$statement->execute();
                                            }
                                            
                                            $statement = $pdo->prepare("UPDATE tbl_awb SET status='1' WHERE awb_code='$pl3_awb[$k]'");
                                            $result2=$statement->execute();
                                            if($result1==1 && $result2==1)
                                            {
                                                $status='Success';
                                            }
                                            else
                                            {
                                                $status='Failed';
                                            }
                    	          
                    	                    $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE id='$pid'");
                                            $statement->execute();
                    	                    $response = $statement->fetchAll(PDO::FETCH_ASSOC);
                                	           foreach($response as $pid)
                                	           {
                                	              $cscs_awb_f= $pid['cscs_awb'];
                                	              $pl3=$pid['3pl_awb'];
                                	              $ref_no= $pid['ref_no'];
                                	              $status_lines= $pid['status'];
                                	           }
                                	           ?>
                    	           
                                    	        <tr class="table-active">
                                    		    <td><?php echo $k+1 ?></td>
                                    		    <td><?php echo  $status ;?></td>
                                    		    <td><?php echo $cscs_awb_f ?></td>
                                    		    <td><?php echo $pl3 ?></td>
                                    		    <td><?php echo $ref_no ?></td>
                                    		    <td><?php echo $status_lines ?></td>
                                    		    </tr>
                    	             <?php     
                    	             
                                         $k++;
                                    }
                                    ?>  
                                    </tbody>
                                    </table></center>
                                    <?php
                    	               }
                    	}	 
                    } 
                    
       catch(Exception $e) 
    {
        echo "<center><h3>Back to Status Bulk Upload &emsp;&emsp;<a href='delivery_bulk_upload.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.Please follow the bulk upload terms and conditions</center>';
        echo '</div>';
    }             
                    
}
                     ?> 
<?php require_once('footer.php'); ?>