<?php
header("Content-Type: application/json");

include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";

$maestroName = $_POST["maestro_name"];


$maestroUpgradeList = null;
if(strlen($maestroName) === 0)
	$maestroUpgradeList = get_maestro_upgrade_list();
else
	$maestroUpgradeList = get_maestro_update_list_by_name($maestroName);

if(strlen($maestroUpgradeList) === 0) {
	set_error_message("마에스트로 업그레이드 요청 목록 없음");
	send_result_fail();
	exit;
}

set_data("maestroUpgradeList", $maestroUpgradeList);
send_result_success();
exit;



function get_maestro_upgrade_list() {
	global $db_conn;

	$query = "
		SELECT U.MaestroUpgradeID, U.MaestroID, M.Name, U.RegisteredAccountType, U.RequestedAccountType, U.RequestedDateTime, M.AvailableActivateDateTime, U.Status
		FROM maestro_upgrade U, maestro M
		WHERE U.Status < 2 AND U.MaestroID = M.MaestroID
		ORDER BY U.MaestroUpgradeID DESC
	";
	$stmt = $db_conn->prepare($query);
	// $stmt->bind_param("iii", $maestroID, $startNo, $endNo);
	// $stmt->bind_param("ii", $maestroName, $startNo);
	$stmt->execute();
	$stmt->bind_result($maestroUpgradeID, $maestroID, $maestroName, $registeredAccountType, $requestedAccountType, $requestedDateTime, $availableActivateDateTime, $status);

	$maestroUpgradeList = array();
	while($stmt->fetch()) {
	    $maestro["maestroUpgradeID"] = $maestroUpgradeID;
	    $maestro["maestroID"] = $maestroID;
	    $maestro["maestroName"] = $maestroName;
	    $maestro["registeredAccountType"] = $registeredAccountType;
	    $maestro["requestedAccountType"] = $requestedAccountType;
	    $maestro["requestedDateTime"] = $requestedDateTime;
	    $maestro["availableActivateDateTime"] = $availableActivateDateTime;
	    $maestro["status"] = $status;
	    array_push($maestroUpgradeList, $maestro);
	}

	return $maestroUpgradeList;
}

function get_maestro_update_list_by_name($searchMaestroName) {
	global $db_conn;

	$query = "
		SELECT U.MaestroUpgradeID, U.MaestroID, M.Name, U.RegisteredAccountType, U.RequestedAccountType, U.RequestedDateTime, M.AvailableActivateDateTime, U.Status
		FROM maestro_upgrade U, maestro M
		WHERE M.Name LIKE CONCAT('%', ?, '%') AND U.MaestroID = M.MaestroID
		ORDER BY U.MaestroUpgradeID DESC
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("s", $searchMaestroName);
	$stmt->execute();
	$stmt->bind_result($maestroUpgradeID, $maestroID, $maestroName, $registeredAccountType, $requestedAccountType, $requestedDateTime, $availableActivateDateTime, $status);

	$maestroUpgradeList = array();
	while($stmt->fetch()) {
	    $maestro["maestroUpgradeID"] = $maestroUpgradeID;
	    $maestro["maestroID"] = $maestroID;
	    $maestro["maestroName"] = $maestroName;
	    $maestro["registeredAccountType"] = $registeredAccountType;
	    $maestro["requestedAccountType"] = $requestedAccountType;
	    $maestro["requestedDateTime"] = $requestedDateTime;
	    $maestro["availableActivateDateTime"] = $availableActivateDateTime;
	    $maestro["status"] = $status;
	    array_push($maestroUpgradeList, $maestro);
	}

	return $maestroUpgradeList;
}

?>