<?php
// get these values from your DB.
require_once('config.php');
$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];

//Mydefault values
$sql= "Select 	ast_mst_asset_no,  			
				ast_mst_asset_type,  			
				ast_mst_asset_grpcode,  			
				ast_mst_asset_status,  			
				ast_mst_asset_shortdesc,   			
				ast_mst_asset_locn,  			
				ast_ls2_meter_id,  			
				ast_ls2_meter_desc 			= COALESCE(ast_ls2_meter_desc,''),	   			
				ast_ls2_meter_point,   			
				ast_ls2_usage_uom,  			
				ast_ls2_meter_install_date,  			
				ast_ls2_usage_date,  			
				ast_ls2_usage_reading,  			
				ast_ls2_old_ltd_usage,  			
				ast_ls2_ltd_usage,		  			
				ast_ls2_avg_usage,  			
				ast_ls2_max_avg_usage,  			
				ast_ls2_incr_usage_flag,  			
				ast_mst.RowID,  			
				ast_ls2.RowID  
				From 		ast_mst,   			ast_det,   			ast_ls2
				WHERE  ( ast_mst.site_cd = ast_det.site_cd )
				AND  ( ast_mst.rowid = ast_det.mst_rowid ) 
				AND  ( ast_mst.site_cd = ast_ls2.site_cd ) 
				AND  ( ast_mst.rowid = ast_ls2.mst_rowid ) 
				AND  ( ast_mst.site_cd = '".$site_cd."' )";

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

if( !$stmt ) {
     $error_message = "Error selecting table (Mydefault values)";
	 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 ($valid) {
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);

	//echo $sql;
	returnData($json);	
	
}

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

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

 
?>