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

/*Initially Array Variable Start*/
    $error_message=array();
    $pk_ids=array();
    $status_line=array();
    $remark=array();
    $receiver_name=array();
    $receiver_mobile=array();
    $reason=array();
    $delivery_date=array();
    $status_date=array();
    $new_status="";
/*Initially Array Variable End*/



if($type=='cscs_awb'){ $type_name="CSCS AWB"; }elseif($type=='3pl_awb'){ $type_name="3PL AWB";}elseif($type=='ref_no'){ $type_name="REF";}
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!=''){
	             /******************** Select Pincode Details **************************/
	                    $awb_value=$emapData[0];
	                    
	                    $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE $type=?");
                        $statement->execute(array($awb_value));
                        $rowcount = $statement->rowCount();
                        $response = $statement->fetchAll(PDO::FETCH_ASSOC);
	                 	foreach($response as $res)
	                 	{
	                 	    $id=$res['id'];
	                 	    $status_code=$res['status_code'];
	                 	    $c_status=$res['status'];
	                 	}
	                 	
	                    if($rowcount>0)
	                    {
	                        // Change the line below to your timezone!
	                        if($emapData[7]=="")
    	                 	        {
    	                 	            date_default_timezone_set('Asia/Kolkata');
                                        $status_date[] = date('Y-m-d h:i:s a', time());
    	                 	        }
    	                 	        else
    	                 	        {
    	                 	         //   $day=date_create($emapData[7]);
	                               //     $created=date_format($day,'Y-m-d');
    	                 	         //  $status_date[]=$created; 
    	                 	           
    	                 	           
    	                 	    $given=$emapData[7];
                                $len=strlen($given);
                                if($len==10){
                                $date=date_create_from_format("d-m-Y",$given);
                                $created= date_format($date,"Y-m-d");
                                 $status_date[] = $created;
                                }elseif($len==8)
                                {
                                $date=date_create_from_format("d-m-y",$given);
                                $created= date_format($date,"Y-m-d");
                                 $status_date[] = $created;
                                }
    	                      }
	                        
    	                   	if(is_numeric($emapData[1]) && $emapData[1]>1)
    	                 	{
    	                 	    if($emapData[1]>$status_code)
    	                 	    {
    	                 	        
    	                 	        $pk_ids[]=$id;
    	                 	        
    	                 	        $unique_status[]=$emapData[1];
    	                 	        if($emapData[1]==2)
    	                 	        {
    	                 	            $status_line[]='picked';
    	                 	        }
    	                 	        elseif($emapData[1]==3)
    	                 	        {
    	                 	           $status_line[]='manifest'; 
    	                 	        }
    	                 	         elseif($emapData[1]==4)
    	                 	        {
    	                 	           $status_line[]='In Transit'; 
    	                 	        }
    	                 	         elseif($emapData[1]==5)
    	                 	        {
    	                 	           $status_line[]='Received at destination'; 
    	                 	        }
    	                 	         elseif($emapData[1]==6)
    	                 	        {
    	                 	           $status_line[]='Out for delivery'; 
    	                 	        }
    	                 	         elseif($emapData[1]==7)
    	                 	        {
    	                 	           $status_line[]='Delivered'; 
    	                 	        }
    	                 	         elseif($emapData[1]==8)
    	                 	        {
    	                 	           $status_line[]='Undelivery'; 
    	                 	        }
    	                 	        else
    	                 	        {
    	                 	           $status_line[]='null';  
    	                 	        }
    	                 	     
    	                 	        if($emapData[6]==''){ $remark[]=''; }else{ $remark[]=$emapData[6]; }
    	                 	        if($emapData[2]==''){ $receiver_name[]=''; }else{ $receiver_name[]=$emapData[2]; }
    	                 	        if($emapData[3]==''){ $receiver_mobile[]=''; }else{ $receiver_mobile[]=$emapData[3]; }
    	                 	        if($emapData[5]==''){ $reason[]=''; }else{ $reason[]=$emapData[5]; }
    	                 	        if($emapData[4]==''){ $delivery_date[]=''; }else{ $delivery_date[]=$emapData[4]; }
    	                 	        
    	                 	    }
                	                 	    else if($emapData[1]==7 && $status_code==8)
                	                 	    {
                	                 	        
                	                 	        $pk_ids[]=$id;
                	                 	        
                	                 	        $unique_status[]=$emapData[1];
                	                 	        if($emapData[1]==2)
                	                 	        {
                	                 	            $status_line[]='picked';
                	                 	        }
                	                 	        elseif($emapData[1]==3)
                	                 	        {
                	                 	           $status_line[]='manifest'; 
                	                 	        }
                	                 	         elseif($emapData[1]==4)
                	                 	        {
                	                 	           $status_line[]='In Transit'; 
                	                 	        }
                	                 	         elseif($emapData[1]==5)
                	                 	        {
                	                 	           $status_line[]='Received at destination'; 
                	                 	        }
                	                 	         elseif($emapData[1]==6)
                	                 	        {
                	                 	           $status_line[]='Out for delivery'; 
                	                 	        }
                	                 	         elseif($emapData[1]==7)
                	                 	        {
                	                 	           $status_line[]='Delivered'; 
                	                 	        }
                	                 	         elseif($emapData[1]==8)
                	                 	        {
                	                 	           $status_line[]='Undelivery'; 
                	                 	        }
                	                 	        else
                	                 	        {
                	                 	           $status_line[]='null';  
                	                 	        }
                	                 	     
                	                 	        if($emapData[6]==''){ $remark[]=''; }else{ $remark[]=$emapData[6]; }
                	                 	        if($emapData[2]==''){ $receiver_name[]=''; }else{ $receiver_name[]=$emapData[2]; }
                	                 	        if($emapData[3]==''){ $receiver_mobile[]=''; }else{ $receiver_mobile[]=$emapData[3]; }
                	                 	        if($emapData[5]==''){ $reason[]=''; }else{ $reason[]=$emapData[5]; }
                	                 	        if($emapData[4]==''){ $delivery_date[]=''; }else{ $delivery_date[]=$emapData[4]; }
                	                 	        
                	                 	    }
    	                 	    else
    	                 	    {
    	                 	        if($emapData[1]==2)
    	                 	        {
    	                 	            $new_status="Picked";
    	                 	        }
    	                 	        elseif($emapData[1]==3)
    	                 	        {
    	                 	           $new_status="Manifest";
    	                 	        }
    	                 	         elseif($emapData[1]==4)
    	                 	        {
    	                 	           $new_status="In Transit";
    	                 	        }
    	                 	         elseif($emapData[1]==5)
    	                 	        {
    	                 	           $new_status="Received at Destination";
    	                 	        }
    	                 	         elseif($emapData[1]==6)
    	                 	        {
    	                 	           $new_status="Out for Delivery";
    	                 	        }
    	                 	         elseif($emapData[1]==7)
    	                 	        {
    	                 	           $new_status="Delivered";
    	                 	        }
    	                 	         elseif($emapData[1]==8)
    	                 	        {
    	                 	           $new_status="Undelivery";
    	                 	        }
    	                 	        else
    	                 	        {
    	                 	           $new_status="";
    	                 	        }
    	                 	         $error_message[]="Row Number $ri $type_name Number $awb_value Current Status: $c_status New Status:$new_status In-valid Status...";
    	                 	    }
    	                 	}

    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri $type_name Number $awb_value having Invalid Status Code...";
    	                 	}
	                    }
	                    else
	                    {
	                          $error_message[]="Row Number $ri $type_name Number $awb_value is Invalid...";
	                    }
	                    
	                        }
	                        $ri++;
				        }
			
	         fclose($file);
	         
	                    if(count($error_message)>0)
                            {
                            ?>
                               <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>
    <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 Delivery &emsp;&emsp;<a href="delivery.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>Status</th>
                    		        <th>CSCS AWB</th>
                    		        <th>3PL AWB</th>
                    		        <th>REF Number</th>
                    		        <th>Current Status</th>
                    		        <th>Receiver Name</th>
                    		        <th>Receiver Mobile</th>
                    		        <th>Reason</th>
                    		        <th>Delivery Date</th>
                    		         <th>Status Updated Date</th>
                    		        <th>Remark</th>
                    		        </tr>
                    		        <tbody>
                                
                                <?php 
                                $k=0;
                                    foreach($pk_ids as $pid)
                                    {
                    	             /******************** Check if service is available or not along with its available services ******************/
                    	             
                    	           // $sql="UPDATE `pickup_entry` SET status='$status_line[$k]',status_code='$unique_status[$k]' WHERE ref_no='$awb_value'";
                                //     $result=$conn->query($sql);
                        
                                            $statement1 = $pdo->prepare("UPDATE pickup_entry SET status='$status_line[$k]',status_code='$unique_status[$k]' WHERE id='$pid'");
                                            $statement1->execute();
                                            
                        
                        
                        
                        	/*Tracking Module Start*/
                        	
                        	                $statement1 = $pdo->prepare("INSERT INTO tbl_tracking (pickup_entry_id,user_id,location,status,status_code,remark,receiver_name,receiver_mobile,reason,delivery_date,created_at) 
                        	                 VALUES (?,?,?,?,?,?,?,?,?,?,?)");
                                         $result= $statement1->execute(array($pid,$clientId,$clientLocation,$status_line[$k],$unique_status[$k],$remark[$k],$receiver_name[$k],$receiver_mobile[$k],$reason[$k],$delivery_date[$k],$status_date[$k]));
                        	        
                                    	        if($result)
                                    	        {
                                    	            $status='Success';
                                    	        }
                                    	        else{ $status='Failed'; }
                                    	        
                    		/*Tracking Module End*/
                    	          
                    	                    $statement2 = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE id='$pid'");
                                            $statement2->execute();
                    	                    $response1 = $statement2->fetchAll(PDO::FETCH_ASSOC);
                    	                    
                                	           foreach($response1 as $pid)
                                	           {
                                	              $cscs_awb_f= $pid['cscs_awb'];
                                	              $pl3_awb=$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_awb ?></td>
                                    		    <td><?php echo $ref_no ?></td>
                                    		    <td><?php echo $status_lines ?></td>
                                    		    <td><?php echo $receiver_name[$k] ?></td>
                                    		    <td><?php echo $receiver_mobile[$k] ?></td>
                                    		    <td><?php echo $reason[$k] ?></td>
                                    		    <td><?php echo $delivery_date[$k] ?></td>
                                    		    <td><?php echo $status_date[$k] ?></td>
                                    		    <td><?php echo $remark[$k] ?></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'); ?>