<?php

use yii\helpers\Html;
use yii\grid\GridView;
use app\components\Utilities;

$this->title = Yii::t('app', 'Groups');
?>

<div class="groups-index">

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

    <p>
        <?= Html::a(Yii::t('app', 'Add Group'), ['create'], ['class' => 'btn btn-success btn-expanded']) ?>
    </p>

    <?= GridView::widget([
        'pager' => [
            'firstPageLabel' => Yii::t('app', 'First Page'),
            'lastPageLabel' => Yii::t('app', 'Last Page')
        ],
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute' => 'name',
                'headerOptions' => ['style' => 'width: 70%;'],
            ],
            [
                'attribute' => 'usersCount',
                'label' => '#Users',
                'headerOptions' => ['style' => 'width: 10%;'],
                'value' => function($model){
                    $query = new \yii\mongoDb\Query();
                    $res = $query
                        ->from('users')
                        ->where(['IN', 'groups', (string)$model->_id])
                        ->all();
                    return $res != null ? count($res) : 0;
                }
            ],
            [
                'attribute' => 'formsCount',
                'label' => '#Forms',
                'headerOptions' => ['style' => 'width: 10%;'],
                'value' => function($model){
                    return count($model->forms);
                }
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'headerOptions' => ['style' => 'width: 10%;'],
                'visibleButtons' => [
                    'delete' => function($model){
                        return Utilities::canDeleteGroup($model->_id);
                    }
                ]
            ],
        ],
    ]); ?>
</div>
