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


$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$wko_det_assign_to = $_REQUEST['wko_det_assign_to'];

/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}




	$sql="  select 
					wko_mst.site_cd,
					mst_RowID,
					wko_det.RowID,
					wko_det_tech_resp_date	= COALESCE(wko_det_tech_resp_date,''),
					wko_det_tech_resp_id	= COALESCE(wko_det_tech_resp_id,''),
					wko_det_resp_contact	= COALESCE(wko_det_resp_contact,''),
					wko_det_resp_id			= COALESCE(wko_det_resp_id,''),
					wko_det_resp_name		= COALESCE(wko_det_resp_name,''),
					wko_det_resp_date		= COALESCE(wko_det_resp_date,'') 
					
			from 	wko_mst(NOLOCK)
			
			INNER JOIN wko_det (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
			
			
			WHERE 	wko_mst.site_cd = '".$site_cd ."' 
			AND		wko_det_assign_to='".$wko_det_assign_to ."'
			AND		wrk_sts_typ_cd='OPEN' ORDER BY  wko_mst_create_date DESC";	
				
				
						
	$stmt = sqlsrv_query( $conn, $sql);

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

 
?>