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

$error_message;
$valid = true;

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


$sql= "	SELECT	
		cause_code			= COALESCE(wko_det_cause_code +':'+wrk_ccd.wrk_ccd_desc,''),
		act_code			= COALESCE(wko_det_act_code+':'+wrk_act.wrk_act_desc,''),
		work_type			= COALESCE(wko_det_work_type+':'+wrk_typ.wrk_typ_desc,''),
		work_class			= COALESCE(wko_det_work_class+':'+wrk_cls.wrk_cls_desc,''),
		wko_det_planner		= COALESCE(wko_det_planner+':'+emp_mst.emp_mst_name,''),
		work_grp			= COALESCE(wko_det_work_grp+':'+ wrk_grp.wrk_grp_desc,''),
		work_sts			= COALESCE(wrk_sts_status +':'+ wrk_sts.wrk_sts_desc,''),
		wko_det_note1		= COALESCE(wko_det_note1,''),
		wko_det_varchar1	= COALESCE(wko_det_varchar1,''),
		wko_det_varchar2	= COALESCE(wko_det_varchar2,''),
		wko_det_varchar3	= COALESCE(wko_det_varchar3,''),
		work_complete 		= COALESCE(CASE wrk_sts_typ_cd WHEN 'OPEN' THEN '0' ELSE '1' END,''), 
		wko_det_corr_action = COALESCE(wko_det_corr_action,''),
		wko_det.RowID,
		wko_det_sched_date	= COALESCE(wko_det_sched_date,''),
		wko_det_cmpl_date	= COALESCE(wko_det_cmpl_date,''),
		wko_det_clo_date	= COALESCE(wko_det_clo_date,''),
		wko_mst_org_date	= COALESCE(wko_mst_org_date,''),  
		wko_mst_due_date	= COALESCE(wko_mst_due_date,''),  
		wko_ref_cnt = (Select Count(*) from wko_ref where wko_ref.site_cd = wko_det.site_cd and wko_ref.mst_RowID = wko_det.mst_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
		
		LEFT 
		OUTER 
		JOIN		emp_mst (NOLOCK)
		ON			wko_det.site_cd = emp_mst.site_cd
		AND			wko_det.wko_det_planner = emp_mst.emp_mst_empl_id


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

?>