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

$hanzi1 = $data->hanzi1;
$hanzi2 = $data->hanzi2;
$hanzi3 = $data->hanzi3;
$hanzi4 = $data->hanzi4;

$conn = connect_db();

$bihua1 = 0;
$bihua2 = 0;
$bihua3 = 0;
$bihua4 = 0;

$jt_bushou1 = '';
$jt_bushou2 = '';
$jt_bushou3 = '';
$jt_bushou4 = '';

$ft_bushou1 = '';
$ft_bushou2 = '';
$ft_bushou3 = '';
$ft_bushou4 = '';

$tiangeBihua = 0;
$rengeBihua = 0;
$digeBihua = 0;
$waigeBihua = 0;
$zonggeBihua = 0;

$res = array();
$waigeRes = array();
$tiangeRes = array();
$rengeRes = array();
$digeRes = array();
$zonggeRes = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	if ($hanzi1 != "" && $hanzi1 != null) {
		$sql = "
			SELECT * 
			FROM `hanzi` 
			WHERE hanzi = '" . $hanzi1 . "' 
			LIMIT 1
		";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$bihua1 = (int)$row["kx_bihua"];
				$jt_bushou1 = $row["jt_bushou"];
				$ft_bushou1 = $row["ft_bushou"];
			}
		} else {
			$res["error1"] = "Hanzi1 not found";
		}
	} else {
		$bihua1 = 1;
	}

	if ($hanzi2 != "" && $hanzi2 != null) {
		$sql = "
			SELECT * 
			FROM `hanzi` 
			WHERE hanzi = '" . $hanzi2 . "' 
			LIMIT 1
		";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$bihua2 = (int)$row["kx_bihua"];
				$jt_bushou2 = $row["jt_bushou"];
				$ft_bushou2 = $row["ft_bushou"];
			}
		} else {
			$res["error2"] = "Hanzi2 not found";
		}
	} else {
		$bihua2 = 1;
	}

	if ($hanzi3 != "" && $hanzi3 != null) {
		$sql = "
			SELECT * 
			FROM `hanzi` 
			WHERE hanzi = '" . $hanzi3 . "' 
			LIMIT 1
		";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$bihua3 = (int)$row["kx_bihua"];
				$jt_bushou3 = $row["jt_bushou"];
				$ft_bushou3 = $row["ft_bushou"];
			}
		} else {
			$res["error3"] = "Hanzi3 not found";
		}
	} else {
		$bihua3 = 1;
	}

	if ($hanzi4 != "" && $hanzi4 != null) {
		$sql = "
			SELECT * 
			FROM `hanzi` 
			WHERE hanzi = '" . $hanzi4 . "' 
			LIMIT 1
		";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$bihua4 = (int)$row["kx_bihua"];
				$jt_bushou4 = $row["jt_bushou"];
				$ft_bushou4 = $row["ft_bushou"];
			}
		} else {
			$res["error4"] = "Hanzi4 not found";
		}
	} else {
		$bihua4 = 1;
	}

	$tiangeBihua = $bihua1 + $bihua2;
	$rengeBihua = $bihua2 + $bihua3;
	$digeBihua = $bihua3 + $bihua4;
	$waigeBihua = $bihua1 + $bihua4;
	$zonggeBihua = ($hanzi1 != "" && $hanzi1 != null ? $bihua1 : 0) +
		($hanzi2 != "" && $hanzi2 != null ? $bihua2 : 0) +
		($hanzi3 != "" && $hanzi3 != null ? $bihua3 : 0) +
		($hanzi4 != "" && $hanzi4 != null ? $bihua4 : 0);

	$sql = "
		SELECT *
		FROM `bihua_desc` 
		WHERE bihua = " . $tiangeBihua;
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$tiangeRes["bihua"] = $row['bihua'];
			$tiangeRes["icon"] = $row['bihua_result'];
			$tiangeRes["result"] = $row['result'];
			$tiangeRes["description"] = $row['description'];
			$tiangeRes["jiye"] = $row['jiye'];
			$tiangeRes["jiating"] = $row['jiating'];
			$tiangeRes["jiankang"] = $row['jiankang'];
			$tiangeRes["hanyi"] = $row['hanyi'];
		}
	} else {
		$tiangeRes["error"] = "tiange result not found";
	}

	$sql = "
		SELECT *
		FROM `bihua_desc` 
		WHERE bihua = " . $rengeBihua;
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$rengeRes["bihua"] = $row['bihua'];
			$rengeRes["icon"] = $row['bihua_result'];
			$rengeRes["result"] = $row['result'];
			$rengeRes["description"] = $row['description'];
			$rengeRes["jiye"] = $row['jiye'];
			$rengeRes["jiating"] = $row['jiating'];
			$rengeRes["jiankang"] = $row['jiankang'];
			$rengeRes["hanyi"] = $row['hanyi'];
		}
	} else {
		$rengeRes["error"] = "renge result not found";
	}

	$sql = "
		SELECT *
		FROM `bihua_desc` 
		WHERE bihua = " . $digeBihua;
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$digeRes["bihua"] = $row['bihua'];
			$digeRes["icon"] = $row['bihua_result'];
			$digeRes["result"] = $row['result'];
			$digeRes["description"] = $row['description'];
			$digeRes["jiye"] = $row['jiye'];
			$digeRes["jiating"] = $row['jiating'];
			$digeRes["jiankang"] = $row['jiankang'];
			$digeRes["hanyi"] = $row['hanyi'];
		}
	} else {
		$digeRes["error"] = "dige result not found";
	}

	$sql = "
		SELECT *
		FROM `bihua_desc` 
		WHERE bihua = " . $waigeBihua;
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$waigeRes["bihua"] = $row['bihua'];
			$waigeRes["icon"] = $row['bihua_result'];
			$waigeRes["result"] = $row['result'];
			$waigeRes["description"] = $row['description'];
			$waigeRes["jiye"] = $row['jiye'];
			$waigeRes["jiating"] = $row['jiating'];
			$waigeRes["jiankang"] = $row['jiankang'];
			$waigeRes["hanyi"] = $row['hanyi'];
		}
	} else {
		$waigeRes["error"] = "waige result not found";
	}

	$sql = "
		SELECT *
		FROM `bihua_desc` 
		WHERE bihua = " . $zonggeBihua;
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$zonggeRes["bihua"] = $row['bihua'];
			$zonggeRes["icon"] = $row['bihua_result'];
			$zonggeRes["result"] = $row['result'];
			$zonggeRes["description"] = $row['description'];
			$zonggeRes["jiye"] = $row['jiye'];
			$zonggeRes["jiating"] = $row['jiating'];
			$zonggeRes["jiankang"] = $row['jiankang'];
			$zonggeRes["hanyi"] = $row['hanyi'];
		}
	} else {
		$zonggeRes["error"] = "zongge result not found";
	}

	$res["bihua1"] = $bihua1;
	$res["bihua2"] = $bihua2;
	$res["bihua3"] = $bihua3;
	$res["bihua4"] = $bihua4;
	$res["jt_bushou1"] = $jt_bushou1;
	$res["jt_bushou2"] = $jt_bushou2;
	$res["jt_bushou3"] = $jt_bushou3;
	$res["jt_bushou4"] = $jt_bushou4;
	$res["ft_bushou1"] = $ft_bushou1;
	$res["ft_bushou2"] = $ft_bushou2;
	$res["ft_bushou3"] = $ft_bushou3;
	$res["ft_bushou4"] = $ft_bushou4;
	$res["waige"] = $waigeRes;
	$res["tiange"] = $tiangeRes;
	$res["renge"] = $rengeRes;
	$res["dige"] = $digeRes;
	$res["zongge"] = $zonggeRes;

	$sange = null;

	// * 三格同性
	if (
		(fmod($tiangeBihua, 10) == 1 || fmod($tiangeBihua, 10) == 2) &&
		(fmod($rengeBihua, 10) == 1 || fmod($rengeBihua, 10) == 2) &&
		(fmod($digeBihua, 10) == 1 || fmod($digeBihua, 10) == 2)
	)
		{
		$sangetongxing = true;
		
		$sql = "
			SELECT *
			FROM `xmx_sangetongxing` 
			WHERE sange = '1,2'";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$sange["desc"] = $row['description'];
			}
		} else {
			$sange["error"] = "sangetongxing result not found";
		}
	} else if (
		(fmod($tiangeBihua, 10) == 3 || fmod($tiangeBihua, 10) == 4) &&
		(fmod($rengeBihua, 10) == 3 || fmod($rengeBihua, 10) == 4) &&
		(fmod($digeBihua, 10) == 3 || fmod($digeBihua, 10) == 4)
	) {
		$sangetongxing = true;

		$sql = "
			SELECT *
			FROM `xmx_sangetongxing` 
			WHERE sange = '3,4'";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$sange["desc"] = $row['description'];
			}
		} else {
			$sange["error"] = "sangetongxing result not found";
		}
	} else if (
		(fmod($tiangeBihua, 10) == 5 || fmod($tiangeBihua, 10) == 6) &&
		(fmod($rengeBihua, 10) == 5 || fmod($rengeBihua, 10) == 6) &&
		(fmod($digeBihua, 10) == 5 || fmod($digeBihua, 10) == 6)
	) {
		$sangetongxing = true;

		$sql = "
			SELECT *
			FROM `xmx_sangetongxing` 
			WHERE sange = '5,6'";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$sange["desc"] = $row['description'];
			}
		} else {
			$sange["error"] = "sangetongxing result not found";
		}
	} else if (
		(fmod($tiangeBihua, 10) == 7 || fmod($tiangeBihua, 10) == 8) &&
		(fmod($rengeBihua, 10) == 7 || fmod($rengeBihua, 10) == 8) &&
		(fmod($digeBihua, 10) == 7 || fmod($digeBihua, 10) == 8)
	) {
		$sangetongxing = true;

		$sql = "
			SELECT *
			FROM `xmx_sangetongxing` 
			WHERE sange = '7,8'";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$sange["desc"] = $row['description'];
			}
		} else {
			$sange["error"] = "sangetongxing result not found";
		}
	} else if (
		(fmod($tiangeBihua, 10) == 9 || fmod($tiangeBihua, 10) == 0) &&
		(fmod($rengeBihua, 10) == 9 || fmod($rengeBihua, 10) == 0) &&
		(fmod($digeBihua, 10) == 9 || fmod($digeBihua, 10) == 0)
	) {
		$sangetongxing = true;

		$sql = "
			SELECT *
			FROM `xmx_sangetongxing` 
			WHERE sange = '9,0'";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$sange["desc"] = $row['description'];
			}
		} else {
			$sange["error"] = "sangetongxing result not found";
		}
	} else {
		$sangetongxing = false;
	}

	$res['sangetongxing'] = $sangetongxing;
	$res['sange'] = $sange;

	// * 成功运
	$sql = "
		SELECT *
		FROM `xmx_chenggongyun` 
		WHERE renge LIKE '%" . fmod($rengeBihua, 10) . "%'
		AND tiange LIKE '%" . fmod($tiangeBihua, 10) . "%'";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$chenggongyun["desc"] = $row['description'];
			$chenggongyun["jixiong"] = $row['jixiong'];
			$chenggongyun["jixiong_icon"] = $row['jixiong_icon'];
		}
	} else {
		$chenggongyun["error"] = "chenggongyun result not found";
	}

	$res['chenggongyun'] = $chenggongyun;

	// * 基础运
	$sql = "
		SELECT *
		FROM `xmx_jichuyun` 
		WHERE renge LIKE '%" . fmod($rengeBihua, 10) . "%'
		AND dige LIKE '%" . fmod($digeBihua, 10) . "%'";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$jichuyun["desc"] = $row['description'];
			$jichuyun["jixiong"] = $row['jixiong'];
			$jichuyun["jixiong_icon"] = $row['jixiong_icon'];
		}
	} else {
		$jichuyun["error"] = "jichuyun result not found";
	}

	$res['jichuyun'] = $jichuyun;

	// * 社交运
	$sql = "
		SELECT *
		FROM `xmx_shejiaoyun` 
		WHERE renge = " . fmod($rengeBihua, 10) . "
		AND waige = " . fmod($waigeBihua, 10);
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$shejiaoyun["desc"] = $row['description'];
			$shejiaoyun["jixiong"] = $row['jixiong'];
			$shejiaoyun["jixiong_icon"] = $row['jixiong_icon'];
		}
	} else {
		$shejiaoyun["error"] = "shejiaoyun result not found";
	}

	$res['shejiaoyun'] = $shejiaoyun;

	// * 三才配置
	$sql = "
		SELECT *
		FROM `xmx_sancaipeizhi` 
		WHERE tiange LIKE '%" . fmod($tiangeBihua, 10) . "%'
		AND renge LIKE '%" . fmod($rengeBihua, 10) . "%'
		AND dige LIKE '%" . fmod($digeBihua, 10) . "%'";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$sancaipeizhi["desc"] = $row['description'];
			$sancaipeizhi["jixiong"] = $row['jixiong'];
			$sancaipeizhi["jixiong_icon"] = $row['jixiong_icon'];
		}
	} else {
		$sancaipeizhi["error"] = "sancaipeizhi result not found";
	}

	$res['sancaipeizhi'] = $sancaipeizhi;

}

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