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

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

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

$name = $data->name;
$nameZh = $data->nameZh;
$email = $data->email;
$contact = $data->contact;
$year = $data->year;
$month = $data->month;
$date = $data->date;
$xingzuo = $data->xingzuo;
$occupation = $data->occupation;
$address = $data->address;
$submitBy = $data->submitBy;

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$res["taluomima"] = calculateThreeNum($year, $month, $date, $xingzuo);

	$sql = "
		INSERT INTO `taluomima_submission`
		(name, nameZh, email, contact, dob, xingzuo, occupation, address, taluomima, shengming, houtian, xingwei, submit_by)
		VALUES
		(
			'" . $name . "',
			'" . $nameZh . "',
			'" . $email . "',
			'" . $contact . "',
			'" . $year . "-" . $month . "-" . $date . "',
			'" . $xingzuo . "',
			'" . $occupation . "',
			'" . $address . "',
			'" . $res["taluomima"][0] . $res["taluomima"][1] . $res["taluomima"][2] . "',
			" . $res["taluomima"][0] . ",
			" . $res["taluomima"][1] . ",
			" . $res["taluomima"][2] . ",
			'" . $submitBy . "'
		)
	";

	if ($conn->query($sql) === TRUE) {
		$res["success"] = "Submit Successful";
	} else {
		$res["failed"] = "Submit Failed";
	}
}

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