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

$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json, true);
$site_cd = $data['site_cd'];
$XML = $data['XML'];



//XML validation
$sql= "select column_name,default_label,cf_label_required from cf_label where language_cd ='DEFAULT' and table_name = 'wko_det' and column_name in ".$XML." ";
		
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wrk_ccd)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$XML_validation = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$XML_validation[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );




//CasuseCode
$sql= "	SELECT CasuseCode = wrk_ccd.wrk_ccd_cause_cd+ ' : ' + wrk_ccd.wrk_ccd_desc      
		FROM wrk_ccd (NOLOCK)      
		WHERE wrk_ccd.site_cd = '".$site_cd."' 
		AND wrk_ccd.wrk_ccd_disable_flag ='0' ORDER BY wrk_ccd_cause_cd ASC";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wrk_ccd)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$CasuseCode = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$CasuseCode[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );



//ActionCode
$sql= "	SELECT ActionCode = wrk_act.wrk_act_action_cd+ ' : ' +wrk_act.wrk_act_desc        
		FROM wrk_act (NOLOCK)  
		WHERE wrk_act.site_cd = '".$site_cd."' 
		AND wrk_act.wrk_act_disable_flag ='0' ORDER BY wrk_act_action_cd ASC";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wrk_act)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$ActionCode = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$ActionCode[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );



//WorkClass
$sql= "	SELECT 	WorkClass= wrk_cls.wrk_cls_cls_cd+ ' : ' + wrk_cls.wrk_cls_desc  
		FROM 	wrk_cls (NOLOCK)       
		WHERE wrk_cls.site_cd = '".$site_cd."' 
		AND wrk_cls.wrk_cls_disable_flag  ='0'ORDER BY wrk_cls_cls_cd ASC";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wrk_cls)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$WorkClass = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$WorkClass[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );



//WorkGroup
$sql= "	SELECT 	WorkGroup= wrk_grp.wrk_grp_grp_cd+ ' : ' + wrk_grp.wrk_grp_desc    
		FROM wrk_grp (NOLOCK)      
		WHERE (wrk_grp_disable_flag = '0'  ) 
		AND wrk_grp.site_cd = '".$site_cd."' 
		AND wrk_grp.wrk_grp_disable_flag ='0' ORDER BY wrk_grp_grp_cd ASC ";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wrk_grp)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$WorkGroup = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$WorkGroup[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


//Work status
$sql= "	SELECT 	wrk_sts_status= wrk_sts_status + ' : ' +wrk_sts_desc,   wrk_sts.wrk_sts_typ_cd,   wrk_sts.wrk_sts_desc,  wrk_sts.wrk_sts_email_flag  
		FROM 	wrk_sts (NOLOCK)   
		WHERE (wrk_sts.wrk_sts_disable_flag = 0  ) 
		AND wrk_sts.site_cd = '".$site_cd."' 
		AND wrk_sts.wrk_sts_typ_cd = 'COMPLETE' order by wrk_sts_status ";
		
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (wrk_grp)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$Workstatus = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$Workstatus[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );






if ($valid) {

	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	//echo $sql;
	returnData($XML_validation,$CasuseCode,$ActionCode,$WorkClass,$WorkGroup,$Workstatus);	
	
}

function returnData($XML_validation,$CasuseCode,$ActionCode,$WorkClass,$WorkGroup,$Workstatus){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'XML_validation'=>$XML_validation,	
	'CasuseCode' => $CasuseCode,
	'ActionCode' =>$ActionCode,	
	'WorkClass'=>$WorkClass,
	'WorkGroup'=>$WorkGroup,
	'Workstatus'=>$Workstatus);
	
	echo json_encode($returnData);
}

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

 
?>