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


$error_message;
$valid = true;

//$site_cd = $_REQUEST['site_cd'];
//$mst_RowID = $_REQUEST['mst_RowID'];

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


$sql="WITH LabelData AS (
    SELECT *
    FROM cf_label
    WHERE table_name = 'wko_det'
      AND language_cd = 'DEFAULT'
      AND column_name IN ('wko_det_rating1','wko_det_rating2','wko_det_rating3')
),
NumberedLabels AS (
    SELECT 
        ROW_NUMBER() OVER (ORDER BY 
            CASE column_name 
                WHEN 'wko_det_rating1' THEN 1
                WHEN 'wko_det_rating2' THEN 2
                WHEN 'wko_det_rating3' THEN 3
            END
        ) AS column_no,
        *
    FROM LabelData
)
SELECT * FROM NumberedLabels;";	
				
	//echo $sql;
	//$params = array($site_cd,$mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql);

			if( !$stmt ) {
				$error_message = "Error SELECT Table (wko_ls8)";
				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) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData($json);		
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}

function returnData($json){
	
	 $json1 = json_encode($json);
	  
/* if(empty(json_decode($json1,1))) {
     $returnData = array(
	'status' => 'Alert',
	'message' => 'No Records found',
	'data' => $json);
}else{
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);
} */
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $json);

	echo json_encode($returnData);
}

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

 
?>