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

$error_message;
$valid = true;

$sql = "SELECT rowid,
			mst_rowid,
			site_cd,
			wko_isp_asset_no,
			wko_isp_job_cd,
			wko_isp_job_desc,
			wko_isp_sec_no,
			wko_isp_sec_desc,
			wko_isp_stp_no,
			wko_isp_stp_desc,
			wko_isp_stp_datatype,
			wko_isp_stp_rowid,
			wko_isp_varchar1,
			wko_isp_varchar2,
			wko_isp_varchar3,
			wko_isp_numeric1,
			wko_isp_numeric2,
			wko_isp_numeric3,
			wko_isp_datetime1,
			wko_isp_datetime2,
			wko_isp_datetime3,
			wko_isp_checkbox1,
			wko_isp_checkbox2,
			wko_isp_checkbox3,
			wko_isp_dropdown1,
			wko_isp_dropdown2,
			wko_isp_dropdown3,
			'total' = (SELECT SUM(CAST(wko_isp_varchar3 as int)) 
							FROM wko_isp WHERE site_cd = '".$_REQUEST['site_cd']."' 
							AND wko_isp_asset_no = '".$_REQUEST['wko_isp_asset_no']."' 
							AND mst_rowid = ".$_REQUEST['rowid']." 
							AND A.wko_isp_sec_no = wko_isp_sec_no),
			'done' = (SELECT SUM(CASE WHEN wko_isp_varchar3 = '1' and wko_isp_stp_datatype = 'T' and wko_isp_varchar1 IS NOT NULL Then 1
							WHEN wko_isp_varchar3 = '1' and wko_isp_stp_datatype = 'N' and wko_isp_numeric1 IS NOT NULL Then 1
							WHEN wko_isp_varchar3 = '1' and wko_isp_stp_datatype = 'D' and wko_isp_datetime1 IS NOT NULL Then 1
							WHEN wko_isp_varchar3 = '1' and wko_isp_stp_datatype = 'C' and wko_isp_checkbox1 = '1' Then 1
							WHEN wko_isp_varchar3 = '1' and wko_isp_stp_datatype = 'Z' and wko_isp_dropdown1 IS NOT NULL Then 1 ELSE 0 END) 
						FROM wko_isp WHERE site_cd = '".$_REQUEST['site_cd']."' 
						AND wko_isp_asset_no = '".$_REQUEST['wko_isp_asset_no']."' 
						AND mst_rowid = ".$_REQUEST['rowid']." 
						AND A.wko_isp_sec_no = wko_isp_sec_no),
			'na' = (SELECT SUM(CASE WHEN wko_isp_varchar3 = '0' Then 1 ELSE 0 END ) 
						FROM wko_isp WHERE site_cd = '".$_REQUEST['site_cd']."' 
						AND wko_isp_asset_no = '".$_REQUEST['wko_isp_asset_no']."' 
						AND mst_rowid = ".$_REQUEST['rowid']." 
						AND A.wko_isp_sec_no = wko_isp_sec_no)
			FROM wko_isp A
			WHERE site_cd = '".$_REQUEST['site_cd']."' 
			AND wko_isp_asset_no = '".$_REQUEST['wko_isp_asset_no']."'
			AND mst_rowid = ".$_REQUEST['rowid'] ."order by wko_isp_sec_no,wko_isp_stp_no" ;
$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (Work Order Check List)";
	 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) {
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);	
	
	returnData($json);	
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Select 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);	
		
	exit();
}


?>