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

require_once "../config.php";

$conn = connect_db();

$nian_res = array();
$yue_res = array();
$ri_res = array();
$shichen_res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$nian_sql = "
		SELECT CONCAT(nian, ' (', year , ' - ', zodiac, ')') AS nian, ID 
		FROM `liangshu_nian` 
		ORDER BY year
	";
	$nian_result = $conn->query($nian_sql);

	if ($nian_result->num_rows > 0) {
		while ($nian_row = $nian_result->fetch_assoc()) {
			$nian_temp = array(
				"ID" => $nian_row["ID"],
				"nian" => $nian_row["nian"]
			);

			array_push($nian_res, $nian_temp);
		}
	}

	$yue_sql = "
		SELECT yue, ID 
		FROM `liangshu_yue` 
		ORDER BY month
	";
	$yue_result = $conn->query($yue_sql);

	if ($yue_result->num_rows > 0) {
		while ($yue_row = $yue_result->fetch_assoc()) {
			$yue_temp = array(
				"ID" => $yue_row["ID"],
				"yue" => $yue_row["yue"]
			);

			array_push($yue_res, $yue_temp);
		}
	}

	$ri_sql = "
		SELECT ri, ID 
		FROM `liangshu_ri` 
		ORDER BY day
	";
	$ri_result = $conn->query($ri_sql);

	if ($ri_result->num_rows > 0) {
		while ($ri_row = $ri_result->fetch_assoc()) {
			$ri_temp = array(
				"ID" => $ri_row["ID"],
				"ri" => $ri_row["ri"]
			);

			array_push($ri_res, $ri_temp);
		}
	}

	$shichen_sql = "
		SELECT CONCAT(shichen, ' (', time, ')') AS shichen, ID 
		FROM `liangshu_shichen` 
		ORDER BY ID
	";
	$shichen_result = $conn->query($shichen_sql);

	if ($shichen_result->num_rows > 0) {
		while ($shichen_row = $shichen_result->fetch_assoc()) {
			$shichen_temp = array(
				"ID" => $shichen_row["ID"],
				"shichen" => $shichen_row["shichen"]
			);

			array_push($shichen_res, $shichen_temp);
		}
	}

	$all_res = array(
		"nian_res" => $nian_res,
		"yue_res" => $yue_res,
		"ri_res" => $ri_res,
		"shichen_res" => $shichen_res,
	);
}

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