<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\data\ArrayDataProvider;
use common\components\RoleDefinition;
use common\models\User;

/* @var $this yii\web\View */
/* @var $model common\models\User */
/* @var $form yii\widgets\ActiveForm */
?>

<?php 

$roleOptions = RoleDefinition::roleLabels();
asort($roleOptions);

?>

<div class="user-form">

    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'username')->textInput() ?>
    <?= $form->field($model, 'email')->textInput() ?>
    <?= $form->field($model, 'status')
    ->dropDownList(User::statuses())->hint("Csak aktív felhasználó tud bejelentkezni") ?>
    <?= $form->field($model, 'password_plain')->passwordInput() ?>
    <?= $form->field($model, 'password_repeat')->passwordInput() ?>
    <?= $form->field($model, 'role')->dropDownList($roleOptions) ?>

	<?php 
	
	$selectedAccounts = $model->selected_accounts;
	
	?>

	<h3>Engedélyezett kasszák</h3>
	<?php echo GridView::widget([
			'dataProvider' => new ArrayDataProvider( [
	      	'allModels' => $accounts,
	      	'sort' => false,
	      	'pagination' => false,
  		]),
			'columns' => [
					[
			          'class' => 'yii\grid\CheckboxColumn',
					  'name' =>  (new ReflectionClass( $model->classname() ))->getShortName()  . '[selected_accounts]',
					  'checkboxOptions' => function ($model, $key, $index, $column)  use ($selectedAccounts){
						    $result = [];
							$result['value'] =  $model->id_account   ;
							
							if ( isset($selectedAccounts) ){
								if ( is_array($selectedAccounts) ){
								    if ( array_search($model->id_account , $selectedAccounts ) !== false){
								    	$result['checked'] = 'checked'  ;
									}
								}
							}
							
					     	return $result;
					 }
			      ],
					[ 'attribute' => 'name' ],
				],
	])?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Mentés') : Yii::t('app', 'Mentés'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
