<?php
require_once('config.php');
require_once('f_resize_imagepath.php');

//require_once('f_update_budget.php');

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$tkn_dvc_id = $_REQUEST['tkn_dvc_id'];
$Folder = $_REQUEST['folder'];

$sql_tkn_mst = "UPDATE	tkn_mst 
				SET 	tkn_logout = GetDate(),
						tkn_online = 0							
				WHERE	tkn_dvc_id =?";
		
	
	$params_tkn_mst = array($tkn_dvc_id);
	$stmt_tkn_mst = sqlsrv_query( $conn, $sql_tkn_mst,$params_tkn_mst);	
		
if( !$stmt_tkn_mst ) {
	$error_message = "Error update table (wko_sts)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}sqlsrv_free_stmt( $stmt_tkn_mst);



$directoryToDelete = "C:\\wamp64\\www\\".$Folder."\\temp_".$tkn_dvc_id."";


if (deleteDirectory($directoryToDelete)) {
    // echo "Directory deleted successfully.";
	// die();
} else {
    // echo "Error deleting directory.";
	// die();
}



if( $stmt_tkn_mst) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData();	
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
} 




/* if ($valid) {	
	returnData($json);	
}
 */
//sqlsrv_free_stmt( $stmt);


function returnData(){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'You have successfully logged out.');
	
	echo json_encode($returnData);
}

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