<?php
namespace JetStudio;

require_once 'parts/init_libs.php';

use Jet\Http_Request;
use Jet\Locale;
use Jet\MVC;
use Jet\SysConf_URI;
use Jet\Tr;
use Jet\Translator;
use Jet\UI;

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>PHP Jet Studio</title>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<jet_layout_meta_tags/>
	<jet_layout_css/>
	<jet_layout_javascripts/>
</head>

<script type="text/javascript">
	$(document).ready(function () {
		const main_toolbar = document.getElementById("main-toolbar");
		if(!main_toolbar) {
			return;
		}
		const main_col = document.getElementById("main-col");
		const main_toolbar_initial_offset = main_toolbar.offsetTop;
		const main_toolbar_height = main_toolbar.offsetHeight;
		const main_col_initial_padding_top = main_col.style.paddingTop;

		window.onscroll = function() {
			if (window.scrollY > main_toolbar_initial_offset) {
				main_toolbar.classList.add("main-toolbar-sticky");
				main_col.style.paddingTop = main_toolbar_height+'px';
			} else {
				main_toolbar.classList.remove("main-toolbar-sticky");
				main_col.style.paddingTop = main_col_initial_padding_top;
			}
		};
	});
</script>

<body>
	<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
		<a href="<?=SysConf_URI::getBase()?>"><img src="https://php-jet.net/images/logo-st.svg" style="width: 140px;height: auto"></a>
		<a class="navbar-brand" href="<?=SysConf_URI::getBase()?>">Studio</a>

		<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNavBar" aria-controls="mainNavBar" aria-expanded="false" aria-label="">
			<span class="navbar-toggler-icon"></span>
		</button>
		<div class="collapse navbar-collapse" id="mainNavBar">
			<ul class="nav navbar-nav">
				
				<li class="nav-item dropdown">
					<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
						<?=UI::icon('eye')?>
						<span class="caret"></span>
					</a>
					<div class="dropdown-menu" aria-labelledby="navbarDropdown">
						<?php foreach( MVC::getBases() as $base ): ?>
							<div class="dropdown-item">
								<div style="display: inline-block;width: 250px;"><?=$base->getName()?></div>
								<?php foreach( $base->getLocales() as $locale ):
									$hp = $base->getHomepage($locale);
									?>
								<div style="display: inline-block;width: 100px;">
									<a href="<?=$hp->getURL()?>" target="_blank"><?=UI::flag($locale)?> <?=$locale->getLanguageName($locale)?></a>
								</div>
								<?php endforeach; ?>
							</div>
						<?php endforeach; ?>
					</div>
				</li>

				
				<li class="nav-item">
					<a class="nav-link"  href="<?=Tr::_( 'https://www.php-jet.net/doc/')?>" target="_blank">
						<?=UI::icon( 'book' )?>
						<?=Tr::_( 'PHP Jet Documentation' )?>
					</a>
				</li>
				
				<li class="nav-item">
					<a class="nav-link"  href="<?=Tr::_( 'https://questions.php-jet.net/')?>" target="_blank">
						<?=UI::icon( 'comments' )?>
						<?=Tr::_( 'Ask a question' )?>
					</a>
				</li>
				
				
			</ul>

			<ul class="navbar-nav ml-auto">

				<li class="nav-item dropdown">
					<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
						<span><?=UI::flag( Locale::getCurrentLocale() )?></span>
						<span class="caret"></span>
					</a>
					<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
						<?php foreach( JetStudio::getLocales() as $locale ): ?>
								<a class="dropdown-item" href="<?=Http_Request::currentURI(['std_locale' =>$locale->toString()])?>">
									<?=UI::flag( $locale )?>
									<?=$locale->getName( $locale )?>
								</a>
						<?php endforeach; ?>
					</div>
				</li>

				<li class="nav-item">
					<a class="nav-link"  href="?logout=logout">
						<?=UI::icon( 'sign-out-alt' )?>
						<?=Tr::_( 'Logout' )?>
					</a>
				</li>

			</ul>

		</div>
	</nav>


	<?php
	if(JetStudio::getCurrentModule()?->getManifest()->getUrlPathPart()):
		$tabs = [];
	
		foreach( JetStudio::getModuleManifests() as $m ) {
			if($m->getUrlPathPart()) {
				$tabs[$m->getName()] = UI::icon($m->getIcon()).' '.$m->getLabelTranslated();
			}
		}

		echo UI::tabs( $tabs, function( $tab) {
			return JetStudio::getModuleManifest($tab)->getURL();
			}, JetStudio::getCurrentModule()?->getName()??'' );
	endif; ?>
	
	<jet_layout_main_position/>

	
	<div id="__progress__">
		<div class="loading text-center">
			<div id="__progress_prc__"></div>
			<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
			<span class="sr-only"><?=Tr::_('Loading ...', [], Translator::COMMON_DICTIONARY);?></span>
		</div>
	</div>

</body>

</html>