<?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 ));
}

$site_cd = $_REQUEST['site_cd'];
$ast_dwntime_down_wo = $_REQUEST['ast_dwntime_down_wo'];
$ast_dwntime_asset_no = $_REQUEST['ast_dwntime_asset_no'];

$sql="Select 	ast_dwntime.site_cd,
				mst_rowid,			
				ast_dwntime_out_date			= COALESCE(ast_dwntime_out_date,''),			
				ast_dwntime_rts_date			= COALESCE(ast_dwntime_rts_date,''),			
				ast_dwntime_downtime			= COALESCE(ast_dwntime_downtime,0),			
				ast_dwntime_repair_from			= COALESCE(ast_dwntime_repair_from,''),		
				ast_dwntime_repair_to			= COALESCE(ast_dwntime_repair_to,''),		
				ast_dwntime_repairtime			= COALESCE(ast_dwntime_repairtime,0),
				ast_dwntime_down_wo				= COALESCE(ast_dwntime_down_wo,''),
				ast_dwntime_up_wo				= COALESCE(ast_dwntime_up_wo,''),		
				ast_dwntime_status_original		= COALESCE(status_original.ast_sts_status+':'+ status_original.ast_sts_desc,''),	
				ast_dwntime_status_down			= COALESCE(status_down.ast_sts_status+':'+ status_down.ast_sts_desc,''),		
				ast_dwntime_status_up			= COALESCE(status_up.ast_sts_status+':'+ status_up.ast_sts_desc,''),			
				ast_dwntime_asset_no			= COALESCE(ast_dwntime_asset_no,''),			
				ast_dwntime_sched_flag			= COALESCE(ast_dwntime_sched_flag,''),	
				ast_dwntime_remark				= COALESCE(ast_dwntime_remark,''),			
				ast_dwntime.audit_user,
				ast_dwntime.audit_date,
				ast_dwntime.rowid
		From 	ast_dwntime
		
		LEFT 
		OUTER 
		JOIN		ast_sts status_original (NOLOCK)
		ON			ast_dwntime.site_cd = status_original.site_cd
		AND			ast_dwntime.ast_dwntime_status_original = status_original.ast_sts_status
		
		LEFT 
		OUTER 
		JOIN		ast_sts status_down  (NOLOCK)
		ON			ast_dwntime.site_cd = status_down.site_cd
		AND			ast_dwntime.ast_dwntime_status_down = status_down.ast_sts_status
		
		
		LEFT 
		OUTER 
		JOIN		ast_sts status_up (NOLOCK)
		ON			ast_dwntime.site_cd = status_up.site_cd
		AND			ast_dwntime.ast_dwntime_status_up = status_up.ast_sts_status

		Where 	ast_dwntime.site_cd = '".$site_cd."'  
		And 	ast_dwntime_down_wo = '".$ast_dwntime_down_wo."' 
		AND		ast_dwntime_asset_no = '".$ast_dwntime_asset_no."'
		AND 	ast_dwntime.ast_dwntime_out_date IS NOT NULL AND 	ast_dwntime.ast_dwntime_rts_date IS NULL";;	
				
	//echo $sql;
	//$params = array($site_cd,$mst_RowID);					
						
	$stmt = sqlsrv_query( $conn, $sql);

			if( !$stmt ) {
				$error_message = "Error SELECT Table (ast_dwntime_)";
				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();
}

 
?>