<?php
require_once('config.php');
$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$RowID = $_REQUEST['RowID'];
$mst_RowID = $_REQUEST['mst_RowID'];
$wko_isp_ref_stp_RowID = $_REQUEST['wko_isp_ref_stp_RowID'];
 
 
$sql = "DELETE FROM wko_isp_ref WHERE   site_cd = '".$site_cd."' AND RowID = '".$RowID."'";
		
$stmt = sqlsrv_query( $conn, $sql);	
		
if( !$stmt ) {
	$error_message = "Error update table (itm_mst)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}
 while ( sqlsrv_next_result($stmt));
 
 
 $sql = "SELECT Img = count(*) 
		FROM 	wko_isp_ref (NOLOCK) 
		WHERE 	site_cd = '".$site_cd."' 
		AND 	wko_isp_ref_stp_RowID = '".$wko_isp_ref_stp_RowID."'
		AND 	mst_RowID = '".$mst_RowID."'";
			
		$stp_ref = sqlsrv_query( $conn, $sql);

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

		$Img_count;

		do {
			 while ($row = sqlsrv_fetch_array($stp_ref, SQLSRV_FETCH_ASSOC)) {
				//$json_list[] = $row;
				
				$Img_count = $row['Img'];
				
			 }
		} while ( sqlsrv_next_result($stp_ref) );

 
if ($valid) {
	
	sqlsrv_close( $conn);	
	returnData($Img_count);	
}
 
function returnData($Img_count){
	$returnData = array(
	'status' => 'SUCCESS',	
	'Img_count' => $Img_count,
	'message' => 'Delete successfully');

	echo json_encode($returnData);
}

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



 
 
?>