<?php
header("Access-Control-Allow-Origin: *");
include 'conexion.php';
$curl = curl_init();
$getJson = file_get_contents('php://input');
$json = json_decode($getJson);
$userx = isset($json->userx) ? $json->userx : null;
$phonex = isset($json->phonex) ? $json->phonex : null;

if ($userx != null) {
	echo $userx;
	$updateAuth = $connect->query("UPDATE users SET auth_sms=lpad(floor(0+ RAND()*10000),4,0) WHERE username='$userx'");
	$getAuth = $connect->query("SELECT auth_sms from users WHERE username='$userx'");
	$result = array();
	while ($fetchData = $getAuth->fetch_assoc()) {
		$result[] = $fetchData;
	}
	$authCode = $result[0]['auth_sms'];

	$getPhone = $connect->query("SELECT phone from users WHERE username='$userx'");
	$resultPhone = array();
	while ($fetchData = $getPhone->fetch_assoc()) {
		$resultPhone[] = $fetchData;
	}
	$phone = $resultPhone[0]['phone'];
	curl_setopt_array($curl, array(
		CURLOPT_URL => 'https://api.fonnte.com/send',
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_POSTFIELDS => array(
			'target' => $phone,
			'message' => 'kode verifikasi anda adalah: ' . $authCode,
			'countryCode' => '62', //optional
		),
		CURLOPT_HTTPHEADER => array(
			'Authorization: iUGn3mZfJrXVH3pMMJog' //change TOKEN to your actual token
		),
	));

	$response = curl_exec($curl);
	if (curl_errno($curl)) {
		$error_msg = curl_error($curl);
	}
	curl_close($curl);

	if (isset($error_msg)) {
		echo $error_msg;
	}
	$response = [
		"error" => false,
		"data" => "Nomor Telephone tidak ada"
	];
	echo json_encode($response);
} else {
	$updateAuth = $connect->query("UPDATE users SET auth_sms=lpad(floor(0+ RAND()*10000),4,0) WHERE phone='$phonex'");
	$getAuth = $connect->query("SELECT auth_sms FROM users WHERE phone='$phonex'");

	if ($getAuth->num_rows > 0) {
		while ($fetchData = $getAuth->fetch_assoc()) {
			$result[] = $fetchData;
		}
		$authCode = $result[0]['auth_sms'];
		$phone = $phonex;
		curl_setopt_array($curl, array(
			CURLOPT_URL => 'https://api.fonnte.com/send',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS => array(
				'target' => $phone,
				'message' => 'kode verifikasi anda adalah: ' . $authCode,
				'countryCode' => '62', //optional
			),
			CURLOPT_HTTPHEADER => array(
				'Authorization: iUGn3mZfJrXVH3pMMJog' //change TOKEN to your actual token
			),
		));

		$response = curl_exec($curl);
		if (curl_errno($curl)) {
			$error_msg = curl_error($curl);
		}
		curl_close($curl);

		if (isset($error_msg)) {
			echo $error_msg;
		}
		$response = [
			"error" => false,
			"data" => "Nomor Telephone tidak ada"
		];
		echo json_encode($response);
	} else {
		$response = [
			"error" => true,
			"message" => "Nomor Telephone tidak ada",
			"data" => null
		];
		echo json_encode($response);
	}
}
