 <?php 
 require_once('header.php'); 
/*Initially Array Variable Start*/
    $error_message=array();
    $customer_code=array();
    $customer_destination=array();
    $customer_old_destination=array();
   $old_des="";
/*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; $j=0;
		  	
	         while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
	         {
	             if($emapData=='')
	             {
	                 break;
	             }
	             if($ri>0 && $emapData!='')
	             {
	                        if($emapData[0]!='' || $emapData[0]!="")
	                        {
	                            $cust_c=$emapData[0];
        	                    $statement = $pdo->prepare("SELECT * FROM tbl_customer WHERE cust_code='$cust_c'");
                                $statement->execute();
                                $rowcount = $statement->rowCount();
                                $response = $statement->fetchAll(PDO::FETCH_ASSOC);
                                foreach($response as $resp)
                                {
                                    $old_des=$resp['destination_city'];
                                }
                	               if($rowcount!=0)
                	               {  	 
                                      $customer_code[]=$emapData[0];
                                      $customer_old_destination[]=$old_des;
                	               }
                	               else
                	               {
                	                   $error_message[]="Row Number $ri Db code Not exist or Invalid Db Code...";
                	               }
	                        }
	                    	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Db code can't be empty...";
    	                 	}
    	                 	
    	                 	
    	                 	if($emapData[1]!='' || $emapData[1]!="")
	                        {
                                $customer_destination[]=$emapData[1];
	                        }
	                    	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Destination can't be empty...";
    	                 	}
				 }
				  $ri++;
	         }
			
	         fclose($file);
	         if(count($error_message)>0)
                            {
                            ?>
                               <center><h3>Back to Customer Edit Bulk Upload &emsp;&emsp;<a href="customer_edit_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 Customer List &emsp;&emsp;<a href="customer.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>Db Code</th>
                    		        <th>Old Destination</th>
                    		        <th>Changed Destination</th>
                    		        <th>Status</th>
                    		        </tr>
                    		        <tbody>
                                
                                <?php 
                                $k=0;
                                    foreach($customer_code as $cust_code)
                                    {
                                        $c_d=$customer_destination[$k];
                                        $c_c=$customer_code[$k];
                                        
                                        $statement = $pdo->prepare("UPDATE tbl_customer SET destination_city='$c_d' WHERE cust_code='$c_c'");
    		                            $statement->execute();
    		                            
    		                            $count = $statement->rowCount();

                                            if($count =='0'){
                                                $status= "Failed !";
                                            }
                                            else{
                                                $status= "Success !";
                                            }
    		                           //$result=1;
                                        
                        	              
                                    	        
                                	           ?>
                    	           
                                    	        <tr class="table-active">
                                    		    <td><?php echo $k+1 ?></td>
                                    		    <td><?php echo $customer_code[$k] ?></td>
                                    		    <td><?php echo $customer_old_destination[$k] ?></td>
                                    		     <td><?php echo $customer_destination[$k] ?></td>
                                    		     <td><?php echo  $status ;?></td>
                                    		    </tr>
                    	             <?php     
                    	             
                                         $k++;
                                    }
                                    ?>  
                                    </tbody>
                                    </table></center>
                                    <?php
                    	               }
		 }	 
    }
     catch(Exception $e) 
    {
        echo "<center><h3>Back to Customer Bulk Upload &emsp;&emsp;<a href='customer_edit_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>';
    }             
}
    require_once('footer.php'); ?>