<?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);

$name = $data->name;
$nameZh = $data->nameZh;
$email = $data->email;
$contact = $data->contact;
$dob_year = $data->dob_year;
$dob_month = $data->dob_month;
$dob_date = $data->dob_date;
$xingzuo = $data->xingzuo;
$occupation = $data->occupation;
$address = $data->address;
$divine_year = $data->divine_year;
$divine_month = $data->divine_month;
$divine_year = $data->divine_year;
$divine_month = $data->divine_month;
$divine_date = $data->divine_date;
$question = $data->question;
$answer = $data->answer;
$paizhen = $data->paizhen;
$tarot_cards = $data->tarot_cards;
$tarot_cards_flip = $data->tarot_cards_flip;
$submitBy = $data->submitBy;

$conn = connect_db();

$res = array();

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

	$sql = "
		INSERT INTO `taluozhanbu_submission`
		(name, nameZh, email, contact, dob, xingzuo, occupation, address, taluomima, shengming, houtian, xingwei, divine_date, question, answer, paizhen, tarot_cards, tarot_cards_flip, submit_by)
		VALUES
		(
			'" . $name . "',
			'" . $nameZh . "',
			'" . $email . "',
			'" . $contact . "',
			'" . $dob_year . "-" . $dob_month . "-" . $dob_date . "',
			'" . $xingzuo . "',
			'" . $occupation . "',
			'" . $address . "',
			'" . $res["taluomima"][0] . $res["taluomima"][1] . $res["taluomima"][2] . "',
			" . $res["taluomima"][0] . ",
			" . $res["taluomima"][1] . ",
			" . $res["taluomima"][2] . ",
			'" . $divine_year . "-" . $divine_month . "-" . $divine_date . "',
			'" . $question . "',
			'" . $answer . "',
			'" . $paizhen . "',
			'" . json_encode($tarot_cards) . "',
			'" . json_encode($tarot_cards_flip) . "',
			'" . $submitBy . "'
		)
	";

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

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