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

$username = $data->username;

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$sql = "
		SELECT * 
		FROM `taluomima_submission` 
		WHERE submit_by = '" . $username . "' 
		AND status IN ('valid', 'pending_deletion') 
		ORDER BY submission_datetime DESC
	";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$temp = array(
				"id" => $row['ID'],
				"name" => $row['name'],
				"nameZh" => $row['nameZh'],
				"email" => $row['email'],
				"contact" => $row['contact'],
				"dob" => $row['dob'],
				"xingzuo" => $row['xingzuo'],
				"occupation" => $row['occupation'],
				"address" => $row['address'],
				"taluomima" => $row['taluomima'],
				"shengming" => $row['shengming'],
				"houtian" => $row['houtian'],
				"xingwei" => $row['xingwei'],
				"submission_datetime" => $row['submission_datetime'],
				"status" => $row['status'],
			);

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

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