<?php

require_once "connection.php";
if(isset($_GET['phoneNumber'])){
	  $sql = ("SELECT * FROM nasabah WHERE phoneNumber='".$_GET['phoneNumber']."'");
 if(!$con->query($sql)){
        echo "Error in executing query.";
    }else{
        $otp = rand(100000,999999);
        $result = $con->query($sql);
        if($result->num_rows <= 0){
            mysqli_query($con,"INSERT INTO nasabah(phoneNumber,tokenHp) VALUES ('" . $_GET['phoneNumber'] . "', '" . $otp . "')");
            
		  $url = 'https://websms.co.id/api/smsgateway-otp?token=662e9556c6aa19fb029dea1771d11938&to=' . $_GET['phoneNumber'] . '&msg=Berikut kodeOTP Anda ' . $otp . ' RAHASIAKAN kodeverifikasi dan abaikan jika anda tidak meminta kodeverifikasi ini - NUSA Mobile';

$header = array(
'Accept: application/json',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);

echo $result;
            
        }else {
           mysqli_query($con,"UPDATE nasabah SET tokenHp = '" . $otp . "' WHERE phoneNumber =  '" . $_GET['phoneNumber'] . "'");
           
        $url = 'https://websms.co.id/api/smsgateway-otp?token=662e9556c6aa19fb029dea1771d11938&to=' . $_GET['phoneNumber'] . '&msg=Berikut kodeOTP Anda ' . $otp . ' RAHASIAKAN kodeverifikasi dan abaikan jika anda tidak meminta kodeverifikasi ini - NUSA Mobile';

$header = array(
'Accept: application/json',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);

echo $result;
	   }
    }
    }
?>