<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\data\ArrayDataProvider;


/* @var $this yii\web\View */
/* @var $model common\models\User */

$this->title = Yii::t('app', 'Jogosultság szerkesztő');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Felhasználók'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<?php 
	
	$availablePermissions = $model->availablePermissions ;

	$permissions = $model->permissions;
	
?>
<div class="user-create">

    <h1><?= Html::encode($this->title) ?></h1>


	<div class="user-form">

    <?php $form = ActiveForm::begin(); ?>
    
    	
    	
    	<h3>Engedélyezett jogosultságok</h3>
	<?php echo GridView::widget([
			'dataProvider' => new ArrayDataProvider( [
	      	'allModels' => $availablePermissions,
	      	'sort' => false,
	      	'pagination' => false,
  		]),
			'columns' => [
					[
			          'class' => 'yii\grid\CheckboxColumn',
					  'name' =>  (new ReflectionClass( $model->classname() ))->getShortName()  . '[permissions]',
					  'checkboxOptions' => function ($model, $key, $index, $column)  use ($permissions){
						    $result = [];
							$result['value'] =  $model['name']   ;
							
							if ( isset($permissions) ){
								if ( is_array($permissions) ){
								    if ( array_search($model['name'] , $permissions ) !== false){
								    	$result['checked'] = 'checked'  ;
									}
								}
							}
							
					     	return $result;
					 }
			      ],
					[ 'attribute' => 'description' ],
				],
	])?>

      <div class="form-group">
        <?= Html::submitButton(  Yii::t('app', 'Mentés'), ['class' =>  'btn btn-primary' ,'name' => 'RoleForm[save]']) ?>
    	</div>
    <?php ActiveForm::end(); ?>
    </div>
    

</div>
