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

$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$left_time = $_POST["LeftTime"];
$subject_name = $_POST["SubjectName"];
// echo $maestro_id." / ".$player_id;

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


if($left_time == 0)
	reset_left_time($maestro_id, $player_id, $subject_name);
else
	update_left_time($maestro_id, $player_id, $left_time, $subject_name);

send_result_success();
exit;



function reset_left_time($maestro_id, $player_id, $subject_name) {
	global $db_conn;

	$query = "
		UPDATE license_time
		SET LeftTime = null, SavedDateTime = null, SubjectName = ?
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('sii', $subject_name, $maestro_id, $player_id);
	$stmt->execute();
}

function update_left_time($maestro_id, $player_id, $left_time, $subject_name) {
	global $db_conn;

	$query = "
		UPDATE license_time
		SET LeftTime = ?, SavedDateTime = NOW(), SubjectName = ?
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('isii', $left_time, $subject_name, $maestro_id, $player_id);
	$stmt->execute();
}

?>