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

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$sql = "
		SELECT `taluozhanbu_submission`.*, `user`.name AS submitter_name, `user`.nameZh AS submitter_nameZh
		FROM `taluozhanbu_submission` 
		LEFT JOIN `user` ON `user`.username = `taluozhanbu_submission`.submit_by
		WHERE `taluozhanbu_submission`.status = 'pending_deletion' 
		ORDER BY `taluozhanbu_submission`.submission_datetime DESC;
	";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$temp = array(
				"name" => $row['name'],
				"nameZh" => $row['nameZh'],
				"email" => $row['email'],
				"contact" => $row['contact'],
				"dob" => $row['dob'],
				"xingzuo" => $row['xingzuo'],
				"occupation" => $row['occupation'],
				"address" => $row['address'],
				"shengming" => $row['shengming'],
				"houtian" => $row['houtian'],
				"xingwei" => $row['xingwei'],
				"divine_date" => $row['divine_date'],
				"question" => $row['question'],
				"answer" => $row['answer'],
				"paizhen" => $row['paizhen'],
				"tarot_cards" => $row['tarot_cards'],
				"tarot_cards_flip" => $row['tarot_cards_flip'],
				"submission_datetime" => $row['submission_datetime'],
				"submit_by" => $row['submit_by'],
				"submitter_name" => $row['submitter_name'] . ' ' . $row['submitter_nameZh'],
			);

			array_push($res, $temp);
		}
	} else {
		$res["error"] = "Submission not found";
	}
}

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