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

$site_cd = $_REQUEST['site_cd'];

//Checking Work Order Number A/M
$sql= "Select cnt_mst_numbering From cnt_mst (NOLOCK) Where site_cd ='".$site_cd."' And cnt_mst_module_cd ='WKR'";
$stmt = sqlsrv_query( $conn, $sql);

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

$cnt_mst_numbering = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$cnt_mst_numbering = $row;	
	
     }
} while ( sqlsrv_next_result($stmt) );


//Mydefault values
$sql= "SELECT 
		 dft_mst_wko_sts = dft_mst_wko_sts + ' : ' + wrk_sts_desc, 
		 dft_mst_lab_act , 
		 dft_mst_mat_act , 
		 dft_mst_con_act , 
		 dft_mst_wko_pri = dft_mst_wko_pri + ' : ' + wrk_pri_desc + ' - ' + cast(wrk_pri_due_date_count as varchar), 
		 dft_mst_temp_ast , 
		 dft_mst_wko_asset_no , 
		 dft_mst_wo_default_cc , 
		 dft_mst_time_card_by , 
		 dft_mst_itm_resv , 
		 dft_mst_mr_approval , 
		 dft_mst_pur_email_approver , 
		 dft_mst_mtr_email_approver , 
		 dft_mst_gen_inv
	FROM dft_mst (NOLOCK) ,wrk_pri (NOLOCK), wrk_sts (NOLOCK) 
	WHERE dft_mst.site_cd = wrk_pri.site_cd 
	AND dft_mst.dft_mst_wko_pri = wrk_pri.wrk_pri_pri_cd 
	AND dft_mst.site_cd = wrk_sts.site_cd
	AND dft_mst.dft_mst_wko_sts = wrk_sts.wrk_sts_status 
	AND	dft_mst.site_cd ='".$site_cd."'";
$stmt = sqlsrv_query( $conn, $sql);

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

$Defaultvalue = array();

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


//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 ('COMPLETE', 'CLOSE', 'CANCEL', 'FORCE-CLOSE')ORDER BY wrk_sts_status ASC";
$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) );


//Priority
$sql= "SELECT wrk_pri.wrk_pri_pri_cd,  wrk_pri.wrk_pri_desc,   wrk_pri.wrk_pri_due_date_count  
FROM wrk_pri   (NOLOCK)
WHERE wrk_pri.site_cd = '".$site_cd."' AND wrk_pri.wrk_pri_disable_flag = '0'ORDER BY wrk_pri_pri_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

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

$Priority = array();

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



//FaultCode
$sql= "SELECT wrk_flt.wrk_flt_fault_cd,wrk_flt.wrk_flt_desc  
FROM wrk_flt (NOLOCK) 
WHERE wrk_flt.site_cd = '".$site_cd."' 
AND wrk_flt.wrk_flt_disable_flag = '0' ORDER BY wrk_flt_fault_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

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

$FaultCode = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {		
		$FaultCode[] = $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) );




//WorkGroupCode
$sql= "SELECT 	wrk_grp.wrk_grp_grp_cd,     wrk_grp.wrk_grp_desc     
FROM 	wrk_grp (NOLOCK)  
WHERE 	wrk_grp.site_cd = '".$site_cd."' 
AND 	wrk_grp.wrk_grp_disable_flag ='0' ORDER BY wrk_grp_grp_cd ASC";
$stmt = sqlsrv_query( $conn, $sql);

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

$WorkGroupCode = array();

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


//Campus 
$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'";


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

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

$Campus  = array();

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



//Building  
$sql= "	SELECT mst_war.mst_war_work_area, mst_war.mst_war_desc        
		FROM mst_war      
		WHERE (mst_war.mst_war_disable_flag = 0     ) 
		AND mst_war.site_cd = '".$site_cd."'";


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

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

$Building   = array();

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



//Infra   
$sql= "	SELECT blk_mst.blk_mst_blk_cod, blk_mst.blk_mst_description   
		FROM  blk_mst     
		WHERE ( blk_mst.blk_mst_disable_flag = 0  ) 
		AND blk_mst.site_cd = '".$site_cd."'";


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

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

$Infra   = array();

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


//source   
$sql= "	SELECT wrk_cls.wrk_cls_cls_cd,wrk_cls.wrk_cls_desc        
		FROM wrk_cls      
		WHERE wrk_cls.site_cd = '".$site_cd."'
		AND wrk_cls.wrk_cls_disable_flag ='0' ";


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

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

$source   = array();

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



//User Area 
$sql= "SELECT 		ulc_mst_loc_cd,  ulc_mst_desc  FROM ulc_mst    
WHERE (		ulc_mst.ulc_mst_disable_flag = 0  AND	ulc_mst.ulc_mst_cls = '1'  ) 
AND ulc_mst.site_cd = '".$site_cd."' ";


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

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

$UserArea  = array();

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



if ($valid) {

	//echo $sql;
	returnData($Defaultvalue,$cnt_mst_numbering,$WorkorderStatus,$Priority,$FaultCode,$CostCenter,$WorkGroupCode,$Campus,$Building,$Infra,$source,$UserArea);	
	
}

function returnData($Defaultvalue,$cnt_mst_numbering,$WorkorderStatus,$Priority,$FaultCode,$CostCenter,$WorkGroupCode,$Campus,$Building,$Infra,$source,$UserArea){
	//echo $json;
	$returnData = array(												
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'Defaultvalue' =>$Defaultvalue,	
	'Auto_Manual' => $cnt_mst_numbering,
	'WorkorderStatus' =>$WorkorderStatus,
	'CostCenter'=>$CostCenter,
	'WorkGroupCode'=>$WorkGroupCode,
	'Priority'=>$Priority,
	'Campus'=>$Campus,
	'Building'=>$Building,
	'Infra'=>$Infra,
	'source'=>$source,
	'UserArea'=>$UserArea,
	'FaultCode'=>$FaultCode);
	
	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);	

 
?>