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

$site_cd = $_REQUEST['site_cd'];



//Workorder Status
$sql= "SELECT 	wrk_sts.wrk_sts_status,  wrk_sts.wrk_sts_typ_cd,    wrk_sts.wrk_sts_desc,  wrk_sts.wrk_sts_email_flag  
		FROM 	wrk_sts  (NOLOCK)
		WHERE 	(wrk_sts.wrk_sts_disable_flag = 0  ) 
		AND 	wrk_sts.site_cd = '".$site_cd."' 
		AND 	wrk_sts_typ_cd NOT IN ('CANCEL') ORDER BY wrk_sts_status";
$stmt = sqlsrv_query( $conn, $sql);

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

$WorkorderStatus = array();

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



//WorkArea
$sql= "SELECT mst_war.mst_war_work_area,mst_war.mst_war_desc 
FROM mst_war (NOLOCK)
WHERE (mst_war.mst_war_disable_flag = 0     ) 
AND mst_war.site_cd = '".$site_cd."'ORDER BY mst_war_work_area  ASC";


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

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

$WorkArea = array();

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




//AssetLocation
$sql= "SELECT 	ast_loc.ast_loc_ast_loc,ast_loc.ast_loc_desc,ast_loc.ast_loc_wr_option,ast_loc.ast_loc_wo_option, ast_loc.ast_loc_pm_option  
FROM 	ast_loc     (NOLOCK)
WHERE (	ast_loc.ast_loc_disable_flag = 0      ) 
AND ast_loc.site_cd = '".$site_cd."'ORDER BY ast_loc_ast_loc";


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

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

$AssetLocation = array();

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


//Level
$sql= "SELECT 	ast_lvl.ast_lvl_ast_lvl, ast_lvl.ast_lvl_desc    
FROM 		ast_lvl    (NOLOCK)
 WHERE (	ast_lvl.ast_lvl_disable_flag = 0)
  AND ast_lvl.site_cd = '".$site_cd."'ORDER BY ast_lvl_ast_lvl";


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

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

$Level = array();

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



$sql= "SELECT  emp_mst.emp_mst_empl_id,     			
		emp_mst.emp_mst_name,emp_mst_emg_phone = COALESCE(emp_mst_emg_phone,'') 		  
FROM 	emp_mst (NOLOCK),  			
		emp_det (NOLOCK),  			
		emp_sts (NOLOCK)  
WHERE	emp_mst.site_cd = emp_det.site_cd  
AND		emp_mst.RowID = emp_det.mst_rowid  
AND	 	emp_mst.site_cd = emp_sts.site_cd  
AND		emp_mst.emp_mst_status = emp_sts.emp_sts_status  
AND		emp_sts.emp_sts_typ_cd = 'ACTIVE'
AND		emp_mst.site_cd = '".$site_cd."'ORDER BY emp_mst.emp_mst_empl_id ASC";


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



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

$emp_mst = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
		//$row['emp_mst_name'] =  utf8_encode($row['emp_mst_name']);
		 $row['emp_mst_name'] =  mb_convert_encoding($row['emp_mst_name'], 'UTF-8', 'ISO-8859-7');
		$emp_mst[] = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


//WorkType
$sql= "	SELECT 	WorkType= wrk_typ.wrk_typ_typ_cd + ' : ' + wrk_typ.wrk_typ_desc, wrk_typ_option,  wrk_typ_pm_option  
		FROM 	wrk_typ     
		WHERE wrk_typ.site_cd = '".$site_cd."' AND wrk_typ.wrk_typ_disable_flag ='0' ORDER BY wrk_typ_typ_cd ASC ";
$stmt = sqlsrv_query( $conn, $sql);

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

$WorkType = array();

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


//CostCenter
$sql= "SELECT 	cf_cost_center.costcenter,    cf_cost_center.descs    
FROM 	cf_cost_center (NOLOCK)  
WHERE (	cf_cost_center.disable_flag = 0  ) 
AND cf_cost_center.site_cd = '".$site_cd."' 
AND disable_flag = '0'ORDER BY costcenter ASC";
$stmt = sqlsrv_query( $conn, $sql);

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

$CostCenter = array();

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



if ($valid) {

	//echo $sql;
	returnData($WorkorderStatus,$WorkArea,$AssetLocation,$Level,$emp_mst,$WorkType,$CostCenter);	
	
}

function returnData($WorkorderStatus,$WorkArea,$AssetLocation,$Level,$emp_mst,$WorkType,$CostCenter){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully',		
	'WorkorderStatus' =>$WorkorderStatus,
	'WorkArea'=>$WorkArea,
	'AssetLocation'=>$AssetLocation,
	'Level'=>$Level,
	'WorkType'=>$WorkType,
	'CostCenter'=>$CostCenter,
	'emp_mst'=>$emp_mst);
	
	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);	

 
?>