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


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

$data = json_decode($json, true);

$site_cd = $data['site_cd'];
$work_area= $data['work_area'];
$asset_locn= $data['asset_locn'];
$ast_lvl= $data['ast_lvl'];


if (!empty($work_area)){	
	$where_work_area = " AND mst_war_work_area like '%".$work_area."%' ";	
}else{	
	$where_work_area = "";	
}

if (!empty($asset_locn)){	
	$where_asset_locn = " AND ast_mst_asset_locn like '%".$asset_locn."%' ";	
}else{	
	$where_asset_locn = "";	
}


if (!empty($ast_lvl)){	
	$where_ast_lvl = " AND ast_mst.ast_mst_ast_lvl like '%".$ast_lvl."%' ";	
}else{	
	$where_ast_lvl = "";	
}


$sql="Select  	count = count(ast_mst_asset_no)
				
								
				FROM 	ast_mst (NOLOCK) 

							INNER JOIN		ast_det (NOLOCK)
							ON				ast_mst.site_cd = ast_det.site_cd
							AND				ast_mst.rowid = ast_det.mst_rowid
					
							INNER JOIN		ast_sts (NOLOCK)
							ON				ast_mst.site_cd = ast_sts.site_cd
							AND				ast_mst.ast_mst_asset_status = ast_sts.ast_sts_status

							LEFT 
							OUTER 
							JOIN			ast_grp (NOLOCK)
							ON				ast_mst.site_cd = ast_grp.site_cd
							AND				ast_mst.ast_mst_asset_grpcode = ast_grp.ast_grp_grp_cd


							LEFT 
							OUTER 
							JOIN			ast_type (NOLOCK)
							ON				ast_mst.site_cd = ast_type.site_cd
							AND				ast_mst.ast_mst_asset_type = ast_type.ast_type_cd

							LEFT 
							OUTER 
							JOIN			ast_cod (NOLOCK)
							ON				ast_mst.site_cd = ast_cod.site_cd
							AND				ast_mst.ast_mst_asset_code = ast_cod.ast_cod_ast_cd


							LEFT 
							OUTER 
							JOIN			mst_war (NOLOCK)
							ON				ast_mst.site_cd = mst_war.site_cd
							AND				ast_mst.ast_mst_work_area = mst_war.mst_war_work_area


							LEFT 
							OUTER 
							JOIN			ast_loc (NOLOCK)
							ON				ast_mst.site_cd = ast_loc.site_cd
							AND				ast_mst.ast_mst_asset_locn = ast_loc.ast_loc_ast_loc


							LEFT 
							OUTER 
							JOIN			ast_lvl (NOLOCK)
							ON				ast_mst.site_cd = ast_lvl.site_cd
							AND				ast_mst.ast_mst_ast_lvl = ast_lvl.ast_lvl_ast_lvl


							LEFT 
							OUTER 
							JOIN			ast_cri (NOLOCK)
							ON				ast_mst.site_cd = ast_cri.site_cd
							AND				ast_mst.ast_mst_cri_factor = ast_cri.ast_cri_cri_factor


							LEFT 
							OUTER 
							JOIN			cf_cost_center (NOLOCK)
							ON				ast_mst.site_cd = cf_cost_center.site_cd
							AND				ast_mst.ast_mst_cost_center = cf_cost_center.costcenter

				

			WHERE  			(ast_mst.site_cd = '".$site_cd."' )".
							$where_work_area.
							$where_asset_locn.
							$where_ast_lvl;

			$stmt = sqlsrv_query( $conn, $sql);

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

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





if ($valid) {

	//echo $sql;
	returnData($json);	
	
}

function returnData($json){
	 $json1 = json_encode($json);
	 //echo$json;
	 
	 if(empty(json_decode($json1,1))) {
     $returnData = array(	
	'status' => 'SUCCESS',
	'message' => 'No Records found ',	
	'data' => $json);
}else{
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => '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);	

 
?>