<?php
    
    use common\modules\comment\models\Comment;
    use yii\helpers\Html;
    use yii\grid\GridView;
    use yii\helpers\Url;
    
    /* @var $this yii\web\View */
    /* @var $dataProvider yii\data\ActiveDataProvider */
    
    $this->title = 'Комменты';
    $this->params[ 'breadcrumbs' ][] = $this->title;
?>
<div class="comment-index">
    
    <h1><?= Html::encode($this->title) ?></h1>
    
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns'      => [
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{approve} {update} {delete}',
                'buttons' => [
                    'approve' => function($url, $model, $key) {
                        /**
                         * @var Comment $model
                         */
                        $options = array_merge([
                            'title' => "Подтвердить",
                            'aria-label' => "Подтвердить",
                            'data-confirm' => "Комментарий и оценка отобразится на публичной части сайта, подтвердить?",
                            'data-method' => 'post',
                            'data-pjax' => '0',
                        ]);
                        $glyphicon = 'glyphicon-ok';
                        if($model->status == $model::STATUS_ACTIVE) {
                            $url = Url::to(['comment/disapprove', 'id' => $model->comment_id]);
                            $glyphicon = 'glyphicon-remove';
                        }
                        return Html::a('<span class="glyphicon '.$glyphicon.'"></span>', $url, $options);
                    }
                ]
            ],
            'comment_id',
            [
                'attribute' => 'rating.value',
                'label' => 'Оценка',
            ],
            'text:ntext',
            [
                'content' => function($model) {
                    /**
                     * @var Comment $model
                     */
                    if(!empty( $model->user )) {
                        return $model->user->username . " (ID: " . $model->user->id . ")";
                    } else {
                        return $model->user_name . " (Гость: " . $model->user_email . ")";
                    }
                },
                'label'   => 'Пользователь',
            ],
            [
                'attribute' => 'status',
                'value'     => function($model) {
                    /**
                     * @var Comment $model
                     */
                    $status = '';
                    switch($model->status) {
                        case Comment::STATUS_ACTIVE:
                            $status = 'Активный';
                            break;
                        case Comment::STATUS_HIDDEN:
                            $status = 'Новый';
                            break;
                        case Comment::STATUS_DELETED:
                            $status = 'Удаленный';
                            break;
                        default:
                            $status = 'Неизвестно';
                    };
                    return $status;
                },
            ],
            'date_add',
            [
                'attribute' => 'model',
                'value'     => function($model) {
                    /**
                     * @var Comment $model
                     */
                    return $model->model . " (ID: " . $model->model_id . ")";
                },
            ],
        ],
    ]); ?>
</div>
