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

$site_cd = $_REQUEST['site_cd'];

//Mydefault values
$sql= "SELECT 
		
		 dft_mst_wko_sts = COALESCE(dft_mst_wko_sts + ' : ' + wrk_sts_desc,''), 
		 dft_mst_lab_act = COALESCE(dft_mst_lab_act,''), 
		 dft_mst_mat_act = COALESCE(dft_mst_mat_act,''), 
		 dft_mst_con_act = COALESCE(dft_mst_con_act,''), 
		 dft_mst_wko_pri = COALESCE(dft_mst_wko_pri + ' : ' + wrk_pri_desc + ' - ' + cast(wrk_pri_due_date_count as varchar),''), 
		 dft_mst_temp_ast , 
		 dft_mst_wko_asset_no= COALESCE(dft_mst_wko_asset_no,'') , 
		 dft_mst_wkr_asset_no= COALESCE(dft_mst_wkr_asset_no,'') ,
		 dft_mst_wo_default_cc , 
		 dft_mst_time_card_by , 
		 dft_mst_itm_resv , 
		 dft_mst_po_auto_close,
		 dft_mst_mr_approval , 
		 dft_mst_pur_email_approver , 
		 dft_mst_mtr_email_approver , 
		 dft_mst_tim_act= COALESCE(dft_mst_tim_act,''),
		 dft_mst_gen_inv,
		 dft_mst_mbl_chk_cmp,
		 dft_mst_mbl_chk_scan,
		 cf_site.site_name
		 
	FROM dft_mst (NOLOCK) 
	
	LEFT 
	OUTER 
	JOIN	wrk_pri (NOLOCK)
	ON		dft_mst.site_cd = wrk_pri.site_cd
	AND		dft_mst.dft_mst_wko_pri = wrk_pri.wrk_pri_pri_cd 

	LEFT 
	OUTER 
	JOIN	wrk_sts (NOLOCK)
	ON		dft_mst.site_cd = wrk_sts.site_cd
	AND		dft_mst.dft_mst_wko_sts = wrk_sts.wrk_sts_status 
		
	LEFT 
	OUTER 
	JOIN	cf_site (NOLOCK)
	ON		dft_mst.site_cd = cf_site.site_cd

	WHERE	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) );


if ($valid) {
	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);

	//echo $sql;
	returnData($Defaultvalue,$ad_authentication);	
	
}

function returnData($Defaultvalue,$ad_authentication){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'ad_authentication' => $ad_authentication,
	'data' => $Defaultvalue);
	
	echo json_encode($returnData);	
}

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

 
?>