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

require_once "../config.php";
require_once "../taluomima/formula.php";

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

$username = $data->username;

$conn = connect_db();

$res = array();

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

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$dob = strtotime($row["dob"]);
			$xingzuo = $row["xingzuo"];

			$year = date("Y", $dob);
			$month = date("m", $dob);
			$date = date("d", $dob);

			$res["dob"] = $month . "/" . $date . "/" . $year;
			$res["result"] = calculateThreeNum($year, $month, $date, $xingzuo);
			$res["xingzuo"] = $xingzuo;
		}
	} else {
		$res["error"] = "User not found";
	}
}

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