<?php
/**
 * Copyright (c) 2016 Serhii Borodai <clarifying@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *
 */

/** @var $this \Zend\View\Renderer\PhpRenderer */

$this->headScript()
    ->appendFile('/js/adm/users.js')
;

$this->headLink()
    ->appendStylesheet('/css/styles-wide-table.css')
;

$lang = $this->lang;

/** @var \Zend\Db\ResultSet\HydratingResultSet $users */
$users = $this->users;

$filters = array(
    'segment' => $this->filter_segment,
    'category' => $this->filter_category,
    'country' => $this->filter_country,
);
$filter_args = function($filter, $value) use ($filters) {
    $tmp = $filters;    
    foreach($tmp as $filterName => $filterValue) {
        if($filterName == $filter) {
            if($value == '') {
                unset($tmp[$filterName]);
            } else {
                $tmp[$filterName] = $value;
            }
        }
    }   
    if(!$tmp) {
        return '';
    }
    
    $tmp = array_filter($tmp);    
    if(!$tmp) {
        return '';
    }
    
    return '?'.urldecode(http_build_query($tmp));
};


$pageLink = $this->url('adm.users', [                                        
    'lang' => $this->lang,
]);

?>

<section class="b-content__work">         
        <div class="panel panel-info">
            <div class="panel-heading">
                <h1 style="padding: 0 0 10px 0;"><?= _t('Пользователи') ?></h1>
                <div class="btn-toolbar">
                    <div class="btn-group" role="group">
                        <?php                
                            $btn_link = $this->url('adm.users.create', [                    
                                'lang' => $this->lang,
                            ]);                 
                        ?>
                        <a class="btn btn-primary" href="<?= $btn_link ?>"><?= _t('Добавить пользователя') ?></a>                        
                    </div>
                </div>
            </div>
            <div class="panel-body p-support-users">
                
                <form method="post" action="<?= $this->url('adm.users', ['lang' => $this->lang]) ?>">
                    <table class="table table-striped">
                        <thead>
                        <tr>                        
                            <th>
                                <?= _t('Пользователь') ?>
                            </th>         
                            <th>
                                <?= _t('Контакты') ?>
                            </th>
                            <th>
                                <?= _t('Добавлен') ?>
                            </th>                        
                            <th>
                                <?= _t('Менеджер') ?>
                            </th>                       
                            <th>
                                <?= _t('Комментарий') ?>
                            </th>
                            <th></th>
                        </tr>
                        </thead>
                        <?php /** @var \App\Entity\User $user */
                        foreach ($users as $user) : ?>
                            <tr>
                                <td class="row-user">
                                    <?php
                                        $btn_link = $this->url('adm.users.edit', [
                                            'lang' => $this->lang,
                                            'id' => $user->getId(),
                                        ]);                 
                                        $country = $user->getCountry();
                                    ?>
                                    <span class="user-segment"><?= $user->getSegment() ?></span>
                                    <span class="user-name"><a href="<?= $btn_link?>"><?= $user->getFullname() ?></a></span>
                                    <?php if($country): ?>
                                    <span class="user-country"><span class="user-country flag flag-<?= strtolower($country->getCode()) ?>"><?= $country->getName() ?></span></span>
                                    <?php endif ?>
                                </td>                  
                                <td>                                
                                    <span class="user-email"><?= $user->getEmail() ?></span>
                                    <span class="user-phone"><?= $user->getPhone() ?></span>
                                </td>
                                <td>
                                    <span class="user-create-date"><?= $user->getDateCreate() ?></span>
                                </td>
                                <td>
                                    <?php
                                        $managerName = '';
                                        if(($manager = $user->getManager())) {
                                            $managerName = $manager->getFullname();
                                        }
                                    ?>
                                    <span><?= $managerName ?></<span>
                                </td>                            
                                <td>
                                    <?= nl2br($user->getComment()) ?>
                                </td>
                                <td class="text-right">
                                        <?php
                                            $btn_link = $this->url('adm.users.statistic', [
                                                'lang' => $this->lang,
                                                'id' => $user->getId(),
                                            ]);
                                        ?>
                                        <?php /* ?>
                                        <button type="button" class="btn  btn-primary" data-link="<?= $btn_link ?>"><?= _t(Статистика) ?></button>
                                        <?php */ ?>
                                        <a class="btn btn-primary" href="<?= $btn_link ?>" target="_blank"><?= _t('Статистика') ?></a>

                                        <?php
                                            $btn_class = '';
                                            $btn_value = '';
                                            $btn_title = '';
                                            $btn_unbnlock_title = _t("Разблокировать");
                                            $btn_block_title = _t("Заблокировать");
                                            if($user->getBlocked()) {
                                                $btn_class = 'btn-primary';
                                                $btn_value = 'data-hide';
                                                $btn_title = $btn_unbnlock_title;
                                            } else {
                                                $btn_class = 'btn-danger';
                                                $btn_value = 'data-activate';
                                                $btn_title = $btn_block_title;
                                            }
                                            $btn_activate_link = $this->url('adm.users.status.unblock', [
                                                'id' => $user->getId(),
                                                'lang' => $this->lang,
                                            ]);
                                            $btn_deactivate_link = $this->url('adm.users.status.block', [
                                                'id' => $user->getId(),
                                                'lang' => $this->lang,
                                            ]);                                        
                                        ?>
                                        <button type="button" class="btn  <?= $btn_class ?>"
                                            data-toggle
                                            data-activate="<?= $btn_activate_link ?>"
                                            data-activate-caption="<?= $btn_unblock_title ?>"
                                            data-hide="<?= $btn_deactivate_link ?>"
                                            data-hide-caption="<?= $btn_block_title ?>"
                                            data-value="<?= $btn_value ?>"
                                        ><?= $btn_title ?></button>                                                           
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </form>
            </div>
            <div class="panel-footer">
            </div>
        </div>    
    </div>
</section>
