<?php
/**
 * YAWIK
 *
 * View template for the login status box.
 *
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
 * @license   MIT
 */
?>
<?php $switchUserSession = new \Laminas\Session\Container(\Auth\Controller\Plugin\UserSwitcher::SESSION_NAMESPACE);
$defaultLang = is_null($this->params('lang')) ? "en":$this->params('lang');
$lang = isset($this->lang)?$this->lang:$defaultLang; ?>
<ul id="yk-login-info" class="nav navbar-nav navbar-collapse collapse navbar-right">
    <li class="dropdown">

        <?php if ($this->auth()->isLoggedIn()):?>

            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                <?php if ($switchUserSession->isSwitchedUser): ?> [<?=$this->translate('switched')?>]<?php endif ?>
                (<?php echo $this->translate($this->acl()->getUser()->getRole())?>)
                <strong><?php echo $this->auth('info')->getDisplayName()?></strong>
                <b class="caret"></b>
            </a>

            <ul class="dropdown-menu pull-right">
                <li>
                    <a href="<?php echo $this->url('lang/my', array('action' => 'profile', 'lang' => $lang, true)) ?>">
                        <span class="yk-icon yk-icon-user fa-fw"></span> <?php echo $this->translate('My profile')?></a>
                </li>
                <li>
                    <a href="<?php echo $this->url('lang/my', array('action' => 'password', 'lang' => $lang), true) ?>"><span class="yk-icon yk-icon-password fa-fw"></span> <?php echo $this->translate('Change password')?></a>
                </li>
                <?php if ($this->acl()->isRole(\Auth\Entity\User::ROLE_RECRUITER)): ?>
                    <?php
                    /*<li>
                      <a href="<?php echo $this->url('lang/my-groups', array('lang' => $lang)) ?>"><span class="yk-icon yk-icon-group"></span> <?php echo $this->translate('Manage groups') ?></a>
                      </li>*/

                    $user = $this->auth()->getUser(); /* @var $user \Auth\Entity\User */
                    $org  = $user->getOrganization(); /* @var \Organizations\Entity\OrganizationReference $org */
                    if ($org): /* An Anonymous user does not have the OrganizationReference */
                        if (!$org->hasAssociation() || $org->isOwner() ):?>
                            <li>
                            <a href="<?php echo $this->url('lang/my-organization', array('lang' => $lang)) ?>"><span class="yk-icon fa-building fa-fw"></span> <?php
                                if (!$org->hasAssociation()) {
                                    echo $this->translate('Create my organization');
                                } else {
                                    $orgName = $org->getOrganizationName();
                                    echo sprintf($this->translate('Edit "%s"'), $orgName ? $orgName->getName() : '&lt;unnamed organization&gt;');
                                }
                                ?></a>
                            </li><?php endif ?>
                    <?php endif; ?>
                <?php endif ?>
                <?php
                    if ($switchUserSession->isSwitchedUser): ?>
                <li class="divider"></li>
                <li><a id="su-clear-link" href="<?=$this->url(
                    'lang/user-switch',
                    [],
                    ['query' => ['do' => 'clear']],
                    true
                    )?>"
                        onclick="$.get($(this).attr('href'), function(data) { if (data.ref) { window.location.href = data.ref; } else { window.location.reload(); } }); return false;">
                        <?=$this->translate('Clear switched user')?></a>
                </li>
                <?php elseif ($this->acl()->isRole(\Auth\Entity\User::ROLE_ADMIN)): ?>
                <li class="divider"></li>
                    <li><a id="su-switch-link" href="#switch-user">
                            <?=$this->translate('Switch user')?> &hellip;
                    </a>
                    </li>
                        <script type="text/javascript">
                            ;(function($) {


                                $(function() {
                                   $('#su-switch-link').click(function(e) {
                                       var $modal = $('#su-select-dialog');
                                       $modal.modal({backdrop:false});
                                       return false;

                                   }) ;
                                });
                            })(jQuery);

                        </script>
                <?php endif ?>
                <li class="divider"></li>
                <li>
                    <a id="ykLogoutButton" href="<?php echo $this->url('auth-logout',array('lang' => $lang)) ?>"><span class="yk-icon yk-icon-logout fa-fw"></span><?php echo $this->translate('Logout')?></a>
                </li>
            </ul>


        <?php else: ?>
            <a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo sprintf(
                    $this->translate('Hello %s'),
                    '<strong>' . $this->translate('Guest') . '</strong>'
                )?><b class="caret"></b></a>

            <ul class="dropdown-menu pull-right">
                <li><a href="<?php

                    $ref = $this->buildReferer(array('logout', 'ref'));
                    echo $this->url('lang/auth', array(), array(
                        'query' => array(
                            'ref' => urlencode($ref),
                        )), true)?>">
                        <span class="yk-icon yk-icon-login fa-fw"></span> <?php echo $this->translate('Login')?>
                    </a>
                </li>
                <?php if (!isset($this->allowRegister) || $this->allowRegister == true): ?>
                    <li class="divider"></li>
                    <li class="disabled dropdown-header"><a><?php echo $this->translate('Login via')?>...</a></li>
                    <?php foreach ($this->socialButtons() as $provider => $name): ?>
                        <li>
                            <a class="login-link" title="<?php echo sprintf($this->translate('Login with %s'), $name)?>" href="<?php echo $this->url('auth-provider', array('provider' => $provider), array('query' => array('ref' => urlencode($ref))))?>">
                                <span class="fa fa-<?php echo $provider?> fa-fw"></span> <?php echo $name?>
                            </a>
                        </li>
                    <?php endforeach ?>
                <?php endif; ?>
            </ul>

        <?php endif ?>
    </li>
</ul>


<div id="su-select-dialog" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?=$this->translate('Select the user to switch to')?>:
                <button type="button" class="close pull-right" data-dismiss="modal">
                    <span>&times;</span>
                </button></h5>
            </div>
            <div class="modal-body">
                <form id="su-form" action="<?=$this->url('lang/user-switch', [], [], true)?>" method="get" class="form-horizontal">
                <select id="su-user-list" data-autoinit="false" name="id" class="form-control"></select></form>

                <script type="text/javascript">
                    $('#su-form').submit(function(e) {
                       var $form = $(e.target);
                       var data = $form.serialize();
                       data.ref = encodeURIComponent(window.location.href);
                            $.get($form.attr('action'), data, function() { window.location.reload(); });
                        return false;
                    });

                    $('#su-user-list').select2({
                        theme: "bootstrap",
                        width: '100%',
                        minimumInputLength: 0,
                        ajax: {
                            url: basePath + '/' + lang + '/user/switch',
                            dataType: 'json',
                            delay: 200,
                            data: function(params) {
                                return { "do": "list", q: params.term, page: params.page };
                            },
                            processResults: function(data, params) {
                                params.page = params.page || 1;
                                return {
                                    results: data.items,
                                    pagination: {
                                        more: params.page * 10 < data.total
                                    }
                                };
                            }
                        },
                        templateResult: function(data) {
                            if (data.loading) { return data.text; }
                            return data.name + (data.email ? ' <' + data.email + '>' : '') + (data.login ? ' [' + data.login + ']' : '');
                        },
                        templateSelection: function(data) {
                            if (!data.id) { return data.text; }
                            return data.login;
                        },
                        escapeMarkup: function (m) { return m; }
                    });

                </script>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="$('#su-form').submit()"><?=$this->translate('Switch user')?></button>
                <button type="button" class="btn btn-default" data-dismiss="modal"><?=$this->translate('Cancel')?></button>
            </div>
        </div>
    </div>
</div>
