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

/*Initially Array Variable Start*/
    $error_message=array();
    $customer_code=array();
    $customer_name=array();
    $customer_mobile_number=array();
    $customer_address=array();
    $customer_city=array(); 
    $customer_state=array();
    $customer_pincode=array();
    $customer_destination=array();
    
    $cus_status=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; $j=0;
		  	
		  	
		  	 if($client_id=='' || $client_id=="" || $client_id==0)
         	{
         	       $error_message[]="Pls Select Client....";
         	}
         	
	         while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
	         {
	             if($emapData=='')
	             {
	                 break;
	             }
	             if($ri>0 && $emapData!='')
	             {
	                 $cus_statu=1;
	                        if($emapData[0]!='' || $emapData[0]!="")
	                        {
	                            $cust_c=$emapData[0];
        	                    $statement = $pdo->prepare("SELECT * FROM tbl_customer WHERE cust_code='$cust_c' and client_id=$client_id");
                                $statement->execute();
                                $rowcount = $statement->rowCount();
                                $response = $statement->fetchAll(PDO::FETCH_ASSOC);
                	               if($rowcount==0)
                	               {  	 
                                      $customer_code[]=$emapData[0];
                	               }
                	               else
                	               {
                	                   $cus_statu=2;
                	                   $customer_code[]=$emapData[0];
                	                   //$error_message[]="Row Number $ri Db code aleady exist...";
                	               }
	                        }
	                    	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Db code can't be empty...";
    	                 	}
    	                 	//error_log('customer Status : '.$cus_statu);
    	                 $cus_status[]= $cus_statu;
    	                 	if(count($customer_code)>0)
        	                 {
        	                     foreach(array_count_values($customer_code) as $num => $value)
                                {
                                    if($value>1 && $value=='')
                                    {
                                    	$error_message[]=$num." Customer Code is having duplicate value of ".$value." times in our csv file";
                                    }
                                }
        	                 }
    	                 	
    	                 	if($emapData[1]!='' || $emapData[1]!="")
    	                 	{
    	                 	    
    	                 	    $customer_name[]=preg_replace('/[^A-Za-z0-9\-\x20\.]/', '', $emapData[1]);
    	                 	}
    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Name Of the Consultant can't be empty...";
    	                 	}
    	                 	/*if($emapData[2]!='' || $emapData[2]!="")
    	                 	{
    	                 	    $customer_mobile_number[]=$emapData[2];
    	                 	}
    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Mobile Number can't be empty...";
    	                 	}*/
    	                 	$customer_mobile_number[]=$emapData[2];
    	                 	
    	                 	if($emapData[3]!='' || $emapData[3]!="")
    	                 	{
    	                 	    if(strlen($emapData[3])>310)
    	                 	    {
    	                 	        $error_message[]="Row Number $ri Address Character Length Should be below 210 ...";
    	                 	    }
    	                 	    else
    	                 	    {
    	                 	        $customer_address[]=preg_replace('/[^A-Za-z0-9\-\x20\.]/', '', $emapData[3]);
    	                 	    }
    	                 	    
    	                 	}
    	                 	else
    	                 	{
    	                 	    $customer_address[]="";
    	                 	}
    	                 /*	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Address can't be empty...";
    	                 	}*/
    	                 	
    	                 	/*if($emapData[4]!='' || $emapData[4]!="")
    	                 	{
    	                 	    $customer_city[]=$emapData[4];
    	                 	}
    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri City can't be empty...";
    	                 	}
    	                 	*/
    	                 	$customer_city[]=$emapData[4];
    	                 	
    	                 	/*if($emapData[5]!='' || $emapData[5]!="")
    	                 	{
    	                 	    $customer_state[]=$emapData[5];
    	                 	}
    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri State can't be empty...";
    	                 	}*/
    	                 	$customer_state[]=$emapData[5];
    	                 	
    	                 	/*if($emapData[6]!='' || $emapData[6]!="")
    	                 	{
    	                 	    $customer_pincode[]=$emapData[6];
    	                 	}
    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Pincode can't be empty...";
    	                 	}
    	                 	if($emapData[7]!='' || $emapData[7]!="")
    	                 	{
    	                 	    $customer_destination[]=$emapData[7];
    	                 	}
    	                 	else
    	                 	{
    	                 	       $error_message[]="Row Number $ri Destination can't be empty...";
    	                 	}*/
    	                 	$customer_pincode[]=$emapData[6];
    	                 	$customer_destination[]=$emapData[7];
    	                 	
				 }
				  $ri++;
	         }
			
	         fclose($file);
	         if(count($error_message)>0)
                            {
                            ?>
                               <center><h3>Back to Status Bulk Upload &emsp;&emsp;<a href="customer_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>Status</th>
                    		        <th>Db Code</th>
                    		        <th>Name</th>
                    		        <th>Mobile Number</th>
                    		        <th>Address</th>
                    		        <th>City</th>
                    		        <th>State</th>
                    		        <th>Pincode</th>
                    		        <th>Destination</th>
                    		        </tr>
                    		        <tbody>
                                
                                <?php 
                                $k=0;
                                    foreach($customer_code as $cust_code)
                                    {
                                        
                                    if($cus_status[$k]==1){    
                                        $statement = $pdo->prepare("INSERT INTO tbl_customer ( `cust_code`, `cust_name`, `cust_phone`, `cust_address`, `cust_city`, `cust_state`, `cust_zip`,`destination_city`,`client_id`) VALUES 
	            	 ('$customer_code[$k]','$customer_name[$k]','$$customer_mobile_number[$k]','$customer_address[$k]','$customer_city[$k]','$customer_state[$k]','$customer_pincode[$k]','$customer_destination[$k]','$client_id')");
                                        $result=$statement->execute();
                                        $n=1;
                                    }else{
                                        $statement = $pdo->prepare("UPDATE tbl_customer SET cust_name=?,cust_phone=?,cust_address=?,cust_city=?,cust_state=?,cust_zip=?,destination_city=?,client_id=?,cust_type=? WHERE cust_code=?");
    		                            $resu=$statement->execute(array($customer_name[$k],$customer_mobile_number[$k],$customer_address[$k],$customer_city[$k],$customer_state[$k],$customer_pincode[$k],$customer_destination[$k],$client_id,'regular',$customer_code[$k]));
                                    $n=2;
                                       // error_log('result: '.$resu);
                                    }    
                                        
                                        
                        	               if($result || $resu)
                                    	        {
                                    	           if($n==1){ $status='Success';}else{ $status='Updated';}
                                    	        }
                                    	        else{ $status='Failed'; }
                                    	        
                                	           ?>
                    	           
                                    	        <tr class="table-active">
                                    		    <td><?php echo $k+1 ?></td>
                                    		    <td><?php echo  $status ;?></td>
                                    		    <td><?php echo $customer_code[$k] ?></td>
                                    		    <td><?php echo $customer_name[$k] ?></td>
                                    		    <td><?php echo $customer_mobile_number[$k] ?></td>
                                    		    <td><?php echo $customer_address[$k] ?></td>
                                    		    <td><?php echo $customer_city[$k] ?></td>
                                    		    <td><?php echo $customer_state[$k] ?></td>
                                    		    <td><?php echo $customer_pincode[$k] ?></td>
                                    		     <td><?php echo $customer_destination[$k] ?></td>
                                    		    </tr>
                    	             <?php     
                    	             
                                         $k++;
                                    }
                                    ?>  
                                    </tbody>
                                    </table></center>
                                    <?php
                    	               }
		 }	 
    }
     catch(Exception $e) 
    {
        echo "<center><h3>Back to Customer Bulk Upload &emsp;&emsp;<a href='customer_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'); ?>