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

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

$maestroID = $_POST["maestro_id"];
$playerName = $_POST["player_name"];
$startNo = $_POST["start_no"];
$endNo = $_POST["end_no"];


$playerList = get_player_list_page($maestroID, $playerName, $startNo, $endNo);
if($playerList.length === 0) {
	set_error_message("해당 이름으로 등록된 학생 없음");
	send_result_fail();
	exit;
}

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


function get_player_list_page($maestroID, $playerName, $startNo, $endNo) {
	global $db_conn;

	$query = "
		SELECT PlayerID, Name, EnterCode FROM player
		WHERE MaestroID = ? AND AccountType <> 1 AND Name LIKE CONCAT('%', ?, '%')
		ORDER BY PlayerID DESC
		LIMIT ?, 10;
	";
	$stmt = $db_conn->prepare($query);
	// $stmt->bind_param("iii", $maestroID, $startNo, $endNo);
	$stmt->bind_param("isi", $maestroID, $playerName, $startNo);
	$stmt->execute();
	$stmt->bind_result($playerID, $name, $enterCode);

	$playerList = array();
	while($stmt->fetch()) {
	    $player["playerID"] = $playerID;
	    $player["playerName"] = $name;
	    $player["enterCode"] = $enterCode;
	    array_push($playerList, $player);
	}

	return $playerList;
}

?>