<?php 
include ('log_entry.php');

error_reporting ( E_ALL & ~ E_NOTICE & ~ E_DEPRECATED );
function getEmployeeOptions() {
	$employeeOptions = null;
	
	if (isset ( $_SESSION ['CachedEmployeeOptions'] ) && ! empty ( $_SESSION ['CachedEmployeeOptions'] )) {
		$employeeOptions = $_SESSION ['CachedEmployeeOptions'];
		// echo "pulling from cache";
	} else {
		$sql_emp = "SELECT id, CONCAT(patient_name, ' ', emp_code) AS emp_details FROM patient_master order by patient_name";
		$employeeOptions = generate_options ( $sql_emp, '', 'id', 'emp_details', '', '' );
		$_SESSION ['CachedEmployeeOptions'] = $employeeOptions;
	}
	return $employeeOptions;
}
function getEmployeekOptions() {
	$employeeOptions = null;
	
	if (isset ( $_SESSION ['CachedEmployeeOptions'] ) && ! empty ( $_SESSION ['CachedEmployeeOptions'] )) {
		$employeeOptions = $_SESSION ['CachedEmployeeOptions'];
		// echo "pulling from cache";
	} else {
		$sql_emp = "SELECT patient_name FROM patient_master order by patient_name";
		$employeeOptions = generate_options ( $sql_emp, '', 'patient_name', '', '', '' );
		$_SESSION ['CachedEmployeeOptions'] = $employeeOptions;
	}
	return $employeeOptions;
}
function getWellnessProgram($emp_id){
	$wellnessOptions = null;
	
	if (isset ( $_SESSION ['CachedWellnessOptions'] ) && ! empty ( $_SESSION ['CachedWellnessOptions'] )) {
		$wellnessOptions = $_SESSION ['CachedWellnessOptions'];
		// echo "pulling from cache";
	} else {
		$sql_wel = "select a.training_schedule_id,
		concat(training_name,'(',`description`,' days ',durations,')') as training
		from assign_training a left join 
		training_schedule b on a.training_schedule_id = b.schedule_id
		left join training_master c 
		on b.training_id = c.training_master_id where a.employee_id='".$emp_id."' and a.training_schedule_id != '0'";
		$wellnessOptions = generate_options ( $sql_wel, '', 'training_schedule_id', 'training', '', '' );

		error_log("wellnessOptions ".$sql_wel);
		$_SESSION ['CachedWellnessOptions'] = $wellnessOptions;
	}
	return $wellnessOptions;
}
function getFirstboxDetails() {
    $FirstboxDetails = null;
    
    if (isset ( $_SESSION ['CachedFirstboxDetails'] ) && ! empty ( $_SESSION ['CachedFirstboxDetails'] )) {
        $employeeOptions = $_SESSION ['CachedFirstboxDetails'];
        // echo "pulling from cache";
    } else {
        $sql_box = "SELECT box_id, CONCAT(box_name, ' ', box_code) AS box_details FROM first_aid_box order by box_name";
        $FirstboxDetails = generate_options ( $sql_box, '', 'box_id', 'box_details', '', '' );
        $_SESSION ['CachedFirstboxDetails'] = $FirstboxDetails;
    }
    return $employeeOptions;
}
function getCurrentUserClients() {
	$clients = null;
	if (isset ( $_SESSION ['CachedCurrentUserClients'] ) && ! empty ( $_SESSION ['CachedCurrentUserClients'] )) {
		$clients = $_SESSION ['CachedCurrentUserClients'];
		// echo "pulling clients from cache";
	} else {
		$sql_completed = " select client_id from user_clients where user_id='" . $_SESSION ['user_id'] . "' ";
		// echo $sql;
		$result_completed = mysqli_query($GLOBALS['conn'],$sql_completed );
		$strTranslations = array ();
		while ( $row_completed = mysqli_fetch_assoc ( $result_completed ) ) {
			$strTranslations [] = $row_completed ['client_id'];
		}
		$clients = implode ( ",", $strTranslations );
		$_SESSION ['CachedCurrentUserClients'] = $clients;
	}
	return $clients;
}
function getCurrentUserOHCs() {
	$OHCs = null;
	if (isset ( $_SESSION ['ohctypes'] ) && ! empty ( $_SESSION ['ohctypes'] )) {
		$OHCs = $_SESSION ['ohctypes'];
		// echo "pulling OHCs from cache";
	}
	return $OHCs;
}
function getConfigKey($key) {
	$configMap = null;
	if (isset ( $_SESSION ['__CONFIG_MAP'] ) && ! empty ( $_SESSION ['__CONFIG_MAP'] )) {
		$configMap = $_SESSION ['__CONFIG_MAP'];
	} else {
		$configMap = getKeyValueMap ( 'config', 'key_name', 'value' );
		$_SESSION ["__CONFIG_MAP"] = $configMap;
	}
	
	return $configMap [$key];
}
function getMedicineFrequencyQty($key) {
	$fequencyMap = null;
	if (isset ( $_SESSION ['__FREQ_MAP'] ) && ! empty ( $_SESSION ['__FREQ_MAP'] )) {
		$fequencyMap = $_SESSION ['__FREQ_MAP'];
	} else {
		$fequencyMap = getKeyValueMap ( 'medicine_frequency', 'frequency_id', 'qty' );
		$_SESSION ["__FREQ_MAP"] = $fequencyMap;
	}
	
	return $fequencyMap [$key];
}

function getItemUnitName($itemId) {
	error_log($itemId);
	$resultItemUnitMap=null;
	$itemUnitName=null;
	if (isset ( $_SESSION ['CachedItemUnitMap'] ) && ! empty ( $_SESSION ['CachedItemUnitMap'] )) {
		$resultItemUnitMap = $_SESSION ['CachedItemUnitMap'];
		error_log("resultItemUnitMap:".$resultItemUnitMap);
	} else {
		$sql_completed = "select item_id, unit_name from tbl_items i, unit_master u where i.unit_id=u.unit_id";
		// echo $sql;
		$item_result = mysqli_query($GLOBALS['conn'],$sql_completed );
		if ($item_result) {
			while ( $row_item = @mysqli_fetch_array ( $item_result ) ) {
				@extract ( $row_item );
				$keys [] = $row_item ['item_id'];
				$values [] = $row_item ['unit_name'];
			}
		}
		$resultItemUnitMap = array_combine ( $keys, $values );
		error_log("Newly loaded resultItemUnitMap:".$resultItemUnitMap);
		$_SESSION ['CachedItemUnitMap']=$resultItemUnitMap;
	}
	if($resultItemUnitMap!=null){
		$itemUnitName = $resultItemUnitMap[$itemId];
	}
	return $itemUnitName;
}

function getCachedEmailList() {
	
	if (isset ( $_SESSION ['_EMAIL_LIST'] ) && ! empty ( $_SESSION ['_EMAIL_LIST'] )) {
		$email_list = $_SESSION ['_EMAIL_LIST'];
		error_log("got here");
	} else {
		$query = "SELECT offiial_email_id FROM `patient_master` WHERE offiial_email_id is not null and trim(offiial_email_id) !='' ";
		error_log("to email:".$query);
		if (!$result = @mysqli_query($GLOBALS['conn'],$query)) {
			exit(mysqli_error($GLOBALS['conn']));
		}
		$email_list = array();
		if(mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_assoc($result)) {
				$email_list[] = $row['offiial_email_id'];
			}
		}
		
		$email_list= json_encode($email_list);
		$_SESSION ["_EMAIL_LIST"] = $email_list;
	}
	
	return $email_list;
}