<?php /** @var \Zend\View\Renderer\PhpRenderer $this */ ?>
<?php
//@todo rewrite as view helper
/** @var \Zend\Expressive\Router\RouterInterface $router */
if (!$router = $this->router) {
    throw new Exception('Router required');
}
/** @var \Psr\Http\Message\ServerRequestInterface $request */
if (!$request = $this->request) {
    throw new Exception('Request required');
}

$langDropDownInit = <<<JSDOC
$(document).ready(function() {
    var lang = $('.header-lang').find('.droplist-item.m-filter__switch-item_active');
    if (lang.length && document.updateLang) {
        document.updateLang(lang[0]);
        console.log('activate selected');
    }
})
JSDOC;

$this->inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $langDropDownInit);


/** @var \Zend\Expressive\Router\RouteResult $routeResult */
$routeResult = $router->match($request);

$matchedRoute = $routeResult->isSuccess() ? $routeResult->getMatchedRouteName() : 'home';

?>
<div class="header-lang col sm-1 xs-1 order-xs-2 droplist <?= $request->getAttribute('lang') ?
'm-filter__switch-item_active' :
''?>" lang="<?= $request->getAttribute('lang') ?>">
    <span class="droplist-current text bold tab-input"><?= $request->getAttribute('lang') ? $request->getAttribute('lang') : _t("Язык") ?></span>
    <ul class="droplist-block">
        <?php
        /** @var \App\Model\Locales $locales */
        $locales = $this->locales;
        foreach ($locales->getLanguages() as $lang) : ?>
        <li class="droplist-item text bold <?= ($request->getAttribute('lang') == $lang ? 'm-filter__switch-item_active' : '') ?>" data-link="<?=

        $this->url(
            $matchedRoute,
            array_merge($request->getAttributes(), ['lang' => $lang]));
        ?>" data-link-flag-lang='/img/flag/<?= $lang ?>.png'><?= $lang ?></li>
        <?php endforeach; ?>
    </ul>
</div>
