<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\components\RoleDefinition;

/* @var $this yii\web\View */
/* @var $searchModel frontend\models\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Felhasználók');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php  echo $this->render('_search', ['model' => $searchModel]); ?>

    <?php if ( RoleDefinition::isAdmin()){ ?>
    <p>
        <?= Html::a(Yii::t('app', 'Új felhasználó'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    
    <?php } ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'username',
            'email:email',
            'created_at:datetime',
			['attribute' => 'status' , 'value' => 'statusHuman'],
        	[
        		'attribute' => 'role',
        		'value' => function ($model){
    				$roles = \Yii::$app->authManager->getRolesByUser($model->id );
    				
    				return implode(', ', array_map(function ($role) { return sprintf("%s", RoleDefinition::getRoleLabel($role->name)); }, $roles ));
    			}
    		]	,
        		
            ['class' => 'yii\grid\ActionColumn',
             'template' => RoleDefinition::getRoleTemplate([ 'admin' => '{view} {update}', 'employee' => '{view}' ] )
    ],
        ],
    ]); ?>

</div>
