<?php
// get these values from your DB.
require_once('config.php');

// Begin transaction
if (sqlsrv_begin_transaction($conn) === false) {
    $error_message = "Transaction begin failed: " . print_r(sqlsrv_errors(), true);
    returnError($error_message);
}

// Get JSON data from input
$json = file_get_contents('php://input');
$data = json_decode($json, true);

// Extract data from JSON
$site_cd = $data['site_cd'] ?? '';
$last_user = $data['last_user'] ?? '';
$dvc_id = $data['dvc_id'] ?? '';
				
$sql="Select * from tkn_mst (NOLOCK) where tkn_site ='".$site_cd."' and tkn_dvc_id = '".$dvc_id."' and tkn_user ='".$last_user."' and tkn_logout is NULL";

$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
	 $error_message = "Error selecting (tkn_mst) ";
	 returnError($error_message);
	 die( print_r( sqlsrv_errors(), true));
	 
}
$json = array();
do 	{
	 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
	 $json[] = $row;
	
	}
} while ( sqlsrv_next_result($stmt) );


if ($stmt) {

	
	//echo $sql;
	returnData($json);	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'data' => $json);
	
	echo json_encode($returnData);	
}

function returnError( $error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => '');
	
	echo json_encode($returnData);	
	exit();	
		
}
	

 
?>