<?php

$content = "";
$body_class = "";
$css_files = array();
$js_files = array();

if (isset($_SESSION['loggedin'])) {
	$body_class = "loggedin";
}

$randnbr = rand(0, 1000);

function print_page($variables=null) {
	/*
		$variables['navbar'] = true (default)/false
		$variables['content']
	*/
	global $css_files, $js_files, $randnbr, $body_class;
	
	$page_header = '
	<!DOCTYPE html>
	<html>
		<head>
			<meta  http-equiv="Content-Type" content="text/html;charset=UTF-8">
			<title>Login</title>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css">
		<link href="css/jquery-ui-timepicker-addon.min.css?'.$randnbr.'" rel="stylesheet" type="text/css">
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
		<link href="css/bootstrap.rtl.min.css?'.$randnbr.'" rel="stylesheet" type="text/css">
		<link href="css/bootstrap.min.css?'.$randnbr.'" rel="stylesheet" type="text/css">
		<link href="css/style.css?'.$randnbr.'" rel="stylesheet" type="text/css">
		<link rel="icon" href="includes/lunch/images/favicon.ico" type="image/vnd.microsoft.icon">
		<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
		<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
		<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
		<script type="text/javascript" src="js/script.js?'.$randnbr.'"></script>
		';
		// 		<script type="text/javascript" src="js/jquery-3.6.0.slim.min.js?'.$randnbr.'"></script>
		
		/*
		<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css">

		<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

		<script type="text/javascript" src="js/bootstrap.bundle.min.js?'.$randnbr.'"></script>
			
		*/

		foreach($css_files as $this_css_file) {
			$page_header .= $this_css_file;
		}
		foreach($js_files as $this_js_file) {
			$page_header .= $this_js_file;
		}
	$page_header .= '</head>
	';

	$content = '<body class="'.$body_class.'">
	';

	$content .= print_message_container();
	
	if(isset($variables['content'])) {
		$content .= $variables['content'];
	}

	$page_footer = '
		</body>
	</html>
	';
	
	print $page_header;
	
	if(isset($variables['navbar']) ? $variables['navbar'] : true) {
		print print_navbar();
	}
	
	print $content;

	print $page_footer;
	
}

function print_navbar() {
	include(LANGUAGEFILE);
	$returndata = "";
	
	$returndata = '
		<nav class="navtop make_sticky">
			<div>';
	// $returndata .= '<h1 onClick="window.location.href=\'lunch.php\'">Smörmagasinet</h1>';
	$returndata .= '<h1 onClick="window.location.href=\'index.php\'"><img src="images/fyrk-logo.jpg" class="navbar-logo" style="border-radius:40%;"></h1>';
	
	$returndata .= '<a href="index.php?profile"><i class="fas fa-user-circle"></i>'.$l_Profile.'</a>';
	if(SITEADMIN) {
		$returndata .= '<a href="index.php?system_settings"><i class="fas fa-sign-out-alt"></i>'.$l_system.'</a>';
	}

	$returndata .= '<a href="logout.php"><i class="fas fa-sign-out-alt"></i>'.$l_Logout.'</a>
			</div>
		</nav>
	
	';
	
	return $returndata;
}

function print_message_container() {
	$returndata = "";
	
	$returndata .= "<div class='popup_message_main_container' id='popup_message_main_container'>";
		/*
		// Example
		$returndata .= "<div class='popup_message_container'>";
			$returndata .= "Test";
		$returndata .= "</div>";
		*/
	$returndata .= "</div>";
	
	return $returndata;
}

function lang_select_menu($variables) {
	/*
		$variables['name']
		$variables['id']
		$variables['selected_value']
	*/
	$returndata = "";
	
	$available_languages = get_available_languages();
	
	if(isset($variables['selected_value'])) {
		$selected_value = $variables['selected_value'];
	} else {
		$selected_value = "";
	}
	
	
	$returndata .= "<select";
	
	if(isset($variables['name'])) {
		$returndata .= " name='".$variables['name']."'";
	}
	
	if(isset($variables['id'])) {
		$returndata .= " id='".$variables['id']."'";
	}
	
	$returndata .= ">";

	foreach($available_languages as $this_lang) {
		if($this_lang == $selected_value) {
			$set_selected = "SELECTED";
		} else {
			$set_selected = "";
		}
		
		$returndata .= "<option value='".$this_lang."' $set_selected>".$this_lang."</option>";
	}
	
	$returndata .= "</select>";
	
	return $returndata;
}

function date_input_field($variables) {
	/*
		$variables['id']
		$variables['name']
		$variables['value'] same as date
		$variables['date'] same as value
	*/
	
	if(!isset($variables['date']) AND !isset($variables['value'])) {
		$variables['date'] = date("Y-m-d", time());
	} elseif(!isset($variables['date']) AND isset($variables['value'])) {
		$variables['date'] = $variables['value'];
	}
	
	$returndata = '';
	
	$returndata .= "<input type='date' name='".$variables['name']."' id='".$variables['id']."' value='".$variables['date']."'>";
	
	return $returndata;
}

function add_js_file($file) {
	global $js_files, $randnbr;
	
	if(strpos('<script', $file) === false) {
		$file = '<script type="text/javascript" src="'.$file.'?'.$randnbr.'"></script>';
	}
	
	$js_files[] = $file;
}

function add_css_file($file) {
	global $css_files, $randnbr;
	
	if(strpos('<link', $file) === false) {
		$file = '<link href="'.$file.'?'.$randnbr.'" rel="stylesheet" type="text/css">';
	}

	$css_files[] = $file;
}

?>