<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST");

require_once "../config.php";

$json = file_get_contents("php://input");
$data = json_decode($json);

$username = $data->username;
$email = $data->email;
$role = $data->role;

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$sql = "
		SELECT * 
		FROM `user` 
		WHERE username = '" . $username . "' 
		AND email = '" . $email . "' 
		AND role = '" . $role . "'
		LIMIT 1
	";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$res["name"] = $row["name"];
			$res["nameZh"] = $row["nameZh"];
			$res["email"] = $row["email"];
			$res["contact"] = $row["contact"];
			$res["dob"] = $row["dob"];
			$res["registered"] = $row["registered"];
			$res["referrer"] = $row["referrer"];
			$res["referral_code"] = $row["referral_code"];
		}
	} else {
		$res["error"] = "Profile not found";
	}
}

$conn->close();
echo json_encode($res);
