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

$maestro_id = $_POST["MaestroID"];
// echo $maestro_id;

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


$default_password = "1111";

// get license_time data
$password = get_maestro_password($maestro_id);
if($password == null) {
	$password = $default_password;
	insert_maestro_password($maestro_id, $default_password);
}

// send license_time data back to client
set_data("maestroPassword", $password);
send_result_success();
exit;



function get_maestro_password($maestro_id) {
	global $db_conn;

	$query = "
		SELECT Password
		FROM license_maestro_password
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($password);
	$stmt->fetch();
	$stmt->close();

	return $password;
}

function insert_maestro_password($maestro_id, $password) {
	global $db_conn;

	$query = "
		INSERT INTO license_maestro_password (MaestroID, Password)
		VALUES (?, ?);
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestro_id, $password);
	$stmt->execute();
}

?>