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


$error_message;
$valid = true;

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

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


$sql="Select Total = SUM(CASE WHEN wko_isp_stp_datatype = 'T' and (wko_isp_varchar1 IS NULL OR wko_isp_varchar1 = '') and wko_isp_varchar3 = '1' Then 1
				WHEN wko_isp_stp_datatype = 'N' and (wko_isp_numeric1 IS NULL) and wko_isp_varchar3 = '1' Then 1  
				WHEN wko_isp_stp_datatype = 'C' and (wko_isp_checkbox1 IS NULL) and wko_isp_varchar3 = '1' Then 1  
				WHEN wko_isp_stp_datatype = 'D' and (wko_isp_datetime1 IS NULL) and wko_isp_varchar3 = '1' Then 1  
				WHEN wko_isp_stp_datatype = 'Z' and (wko_isp_dropdown1 IS NULL) and wko_isp_varchar3 = '1' Then 1  
				ELSE 0 
				END)
		From	wko_isp (NOLOCK)
		WHere	wko_isp.mst_rowid = '".$mst_RowID."'
		And		wko_isp.site_cd = '".$site_cd."'";	
								
						
	$stmt = sqlsrv_query( $conn, $sql);

			if( !$stmt ) {
				$error_message = "Error select Table (wko_isp)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}

			

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


if( $stmt) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData($Total);		
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}

function returnData($Total){
		
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'Total' => $Total);

	echo json_encode($returnData);
}

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

 
?>