<?php
	include_once('../config.php');      
	include_once(ABS_PATH.'/custom_function/default.php'); 
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
                        
    
    if(isset($_REQUEST['goBck']))
    { 
        unset($_SESSION['Adharreference_id']);
        unset($_SESSION['adh']);
        header("Location: ../");
        die;
    }
    
    if(isset($_POST['submitOtp'])) 
    { 
        
            $number = $_POST['OTP'];
            $ref = $_SESSION['Adharreference_id'];
            $RechargeAccess_token = "c1IJbwdJ7NaChZ6a6UFECH0h0Cq6MiHteJ3G5x3G";    
            $data_string = '{"reference_id":"'.$ref.'","client_id":"'.mt_rand(343555,23255).'","otp":"'.$number.'"}';  
            $ch = curl_init('https://mobileapi.in/api/v1/verify/aadhaar/otp/verify');                                             
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            'x-tera-tenant-region: IN', 
            'Content-Type: application/json', 
            $headers[] = "Authorization: Bearer ".$RechargeAccess_token,
            'Content-Length: ' . strlen($data_string))                                                                       
            );       																		 
            $response = curl_exec($ch);     
            $Result = json_decode($response);   
            if($Result->status_id==1)
            {  
					$stmt=$conn->prepare("insert into  UI__AdharVeri (status,adh,nm,address,dob,cntry,dist,pin,state,custId,Tdate) values(:status,:adh,:nm,:address,:dob,:cntry,:dist,:pin,:state,:custId,:Tdate) ");    
					$stmt->bindValue(':status',  $Result->data->status, PDO::PARAM_STR);
					
					$stmt->bindValue(':adh',   $_SESSION['adh']  , PDO::PARAM_STR);
					$stmt->bindValue(':nm',  $Result->data->name, PDO::PARAM_STR);  
					$stmt->bindValue(':address',  $Result->data->address , PDO::PARAM_STR);  
					$stmt->bindValue(':dob',  $Result->data->dob  , PDO::PARAM_STR);  
					$stmt->bindValue(':cntry',  $Result->data->split_address->country  , PDO::PARAM_STR);  
					$stmt->bindValue(':dist',   $Result->data->split_address->dist , PDO::PARAM_STR);  
					$stmt->bindValue(':pin',  $Result->data->split_address->pincode , PDO::PARAM_STR);  
					$stmt->bindValue(':state',  $Result->data->split_address->state  , PDO::PARAM_STR);  
					$stmt->bindValue(':custId', $_SESSION['userId'] , PDO::PARAM_STR);  
					$stmt->bindValue(':Tdate',  date("d-M-Y"), PDO::PARAM_STR);
					$stmt->execute();  
					$_SESSION['Regis'] = "Verification Successful";    
					$_SESSION['topSuc'] = 1;
					unset($_SESSION['Adharreference_id']);
					unset($_SESSION['adh']); 
            }else
				$_SESSION['Regis'] = "Enter Correct OTP";  				
			
			header("Location: adharVeri.php");
			die;  
	}			
    
    
    if(isset($_POST['submit'])) 
    {               
                  
                   
                $stmt=$conn->prepare("select  *  from  UI__AdharVeri   where adh=:adh ");    
                $stmt->bindValue(':adh', $_POST['mobile'] , PDO::PARAM_STR); 
                $stmt->execute();    
                if($stmt->rowCount()>0)   
				{
				    $FResult = $stmt->fetch(PDO::FETCH_OBJ);    
				    $stmt=$conn->prepare("insert into  UI__AdharVeri (status,adh,nm,address,dob,cntry,dist,pin,state,custId,Tdate) values(:status,:adh,:nm,:address,:dob,:cntry,:dist,:pin,:state,:custId,:Tdate) ");    
					$stmt->bindValue(':status',  $FResult->status, PDO::PARAM_STR); 
					$stmt->bindValue(':adh',   $FResult->adh  , PDO::PARAM_STR);
					$stmt->bindValue(':nm',  $FResult->nm, PDO::PARAM_STR);  
					$stmt->bindValue(':address',  $FResult->address , PDO::PARAM_STR);  
					$stmt->bindValue(':dob',  $FResult->dob  , PDO::PARAM_STR);  
					$stmt->bindValue(':cntry',  $FResult->cntry  , PDO::PARAM_STR);  
					$stmt->bindValue(':dist',   $FResult->dist , PDO::PARAM_STR);  
					$stmt->bindValue(':pin',  $FResult->pin , PDO::PARAM_STR);  
					$stmt->bindValue(':state',  $FResult->state  , PDO::PARAM_STR);  
					$stmt->bindValue(':custId', $_SESSION['userId'] , PDO::PARAM_STR);  
					$stmt->bindValue(':Tdate',  date("d-M-Y"), PDO::PARAM_STR);
					$stmt->execute();  
	 				$_SESSION['Regis'] = "Verification Successful";    
					$_SESSION['topSuc'] = 1;
					unset($_SESSION['Adharreference_id']);
					unset($_SESSION['adh']); 
				} 
                else 
                { 
                    $number = $_POST['mobile'];
                    $RechargeAccess_token = "c1IJbwdJ7NaChZ6a6UFECH0h0Cq6MiHteJ3G5x3G";    
                    $data_string = '{"provider_id":"10","client_id":"'.mt_rand(343555,23255).'","number":"'.$number.'"}';     
                    $ch = curl_init('https://mobileapi.in/api/v1/verify/aadhaar/otp');                                             
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                    'x-tera-tenant-region: IN', 
                    'Content-Type: application/json', 
                    $headers[] = "Authorization: Bearer ".$RechargeAccess_token,
                    'Content-Length: ' . strlen($data_string))                                                                       
                    );       																		 
                    $response = curl_exec($ch);  
                    $Result = json_decode($response);  
                    if($Result->status_id==1)
                    {
                        $_SESSION['Adharreference_id'] = $Result->reference_id;
                        $_SESSION['adh'] = $number;
                        $_SESSION['Regis'] = "OTP sent to your registered mobile no";
                    }else
                        $_SESSION['Regis'] = "Enter Correct Adhar No";     
                }
                header("Location: adharVeri.php");
                die; 
    }  
	
    $stmt=$conn->prepare("select * from   UI__AdharVeri where custId=:cuurentId");    
    $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
    $stmt->execute(); 
    $aCt = $stmt->rowCount();    
    $Cname = $stmt->fetch(PDO::FETCH_OBJ);    
    
include_once("../view/dashboard/adharVeri.php");
?>