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

$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$ROW_ID = $_REQUEST['rowid'];


$sql= "	SELECT	cause_code	= wko_det_cause_code +':'+wrk_ccd.wrk_ccd_desc,
		act_code		= wko_det_act_code+':'+wrk_act.wrk_act_desc,
		work_type		= wko_det_work_type+':'+wrk_typ.wrk_typ_desc,
		work_class		= wko_det_work_class+':'+wrk_cls.wrk_cls_desc,
		work_grp		= wko_det_work_grp+':'+ wrk_grp.wrk_grp_desc,
		work_sts		= wrk_sts_status +':'+ wrk_sts.wrk_sts_desc,
		work_complete 	= CASE wrk_sts_typ_cd WHEN 'OPEN' THEN '0' ELSE '1' END, 
		wko_det_corr_action,wko_det.RowID

		FROM 		wko_det (NOLOCK)

		INNER
		JOIN		wko_mst (NOLOCK)
		ON			wko_mst.site_cd = wko_det.site_cd
		AND			wko_mst.RowID = wko_det.mst_RowID
		
		INNER
		JOIN		wrk_sts (NOLOCK)
		ON			wko_mst.site_cd = wrk_sts.site_cd
		AND			wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
		
		LEFT 
		OUTER 
		JOIN		wrk_ccd (NOLOCK)
		ON			wko_det.site_cd = wrk_ccd.site_cd
		AND			wko_det.wko_det_cause_code = wrk_ccd.wrk_ccd_cause_cd

		LEFT 
		OUTER 
		JOIN		wrk_act (NOLOCK)
		ON			wko_det.site_cd = wrk_act.site_cd
		AND			wko_det.wko_det_act_code = wrk_act.wrk_act_action_cd


		LEFT 
		OUTER 
		JOIN		wrk_typ (NOLOCK)
		ON			wko_det.site_cd = wrk_typ.site_cd
		AND			wko_det.wko_det_work_type = wrk_typ.wrk_typ_typ_cd


		LEFT 
		OUTER 
		JOIN		wrk_cls (NOLOCK)
		ON			wko_det.site_cd = wrk_cls.site_cd
		AND			wko_det.wko_det_work_class = wrk_cls.wrk_cls_cls_cd


		LEFT 
		OUTER 
		JOIN		wrk_grp (NOLOCK)
		ON			wko_det.site_cd = wrk_grp.site_cd
		AND			wko_det.wko_det_work_grp = wrk_grp.wrk_grp_grp_cd


		WHERE  		wko_det.site_cd = '".$site_cd."' 
		AND 		wko_det.mst_RowID = '".$ROW_ID."'";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Asset Group Code)";
	 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) {

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

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

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

?>