<?php

use  \app\models\base\Comments;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\ActionColumn;
use yii\grid\GridView;

/** @var yii\web\View $this */
/** @var yii\data\ActiveDataProvider $dataProvider */

$this->title = 'Список комментариев';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="comments-index">
    <?php \yii\widgets\Pjax::begin([
            'timeout'=>5000,
            'enablePushState'=>false
    ])?>
<?
//    $dataProvider->setSort([
//        'attributes' => [
//			'created_at'
//        ]
//    ]);
/*
	$dataProvider->sort = ['attributes' => [
	'created_at' =>
		[
                        'asc' => ['created_at' => SORT_ASC],
                        'desc' => ['created_at' => SORT_DESC],
                        'default' => SORT_DESC
        ],
	]];*/
?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
		'filterModel' => $searchModel,
		'layout' => "{errors}\n{summary}\n{pager}\n{items}\n{pager}",
		'tableOptions' => ['id'=>'utab', 'class' => 'table table-striped table-bordered table-min'],
		'filterPosition' =>'header',
		'pager' => [
			'prevPageCssClass' => 'page-item',
			'firstPageLabel' => 'Начало',
			'lastPageLabel' => 'Конец',
			'linkOptions' => [ 'class'=>'page-link'],
			'pageCssClass' => 'page-item',
			'disabledListItemSubTagOptions' => ['tag' => 'a', 'class' => 'page-link'],
		],
        'columns' => [
			'created_at',
            ['label'=>"Новость","format"=>"raw","value"=>fn($model)=>isset( $model->post->title )?Html::a($model->post->title,$model->post->getUrl(),["target"=>"_blank"]):''],
            'message:ntext',
			'fakename',
			'ip_address',
			//['label'=>"user_agent","format"=>"raw","value"=>fn($model)=>isset( $model->user_agent )?$model->user_agent:''],
			'user_agent',
            [
                'class' => ActionColumn::className(),
				'template' => '{view}',
                'urlCreator' => function ($action, Comments $model, $key, $index, $column) {
                    return Url::toRoute([$action, 'id' => $model->id]);
                 },
				'header'=>"Действия",
				'buttonOptions' => ['class' => 'btn btn-default btn-sm'],
				'contentOptions'=>['style'=>'min-width: 40px;'],
				'options' =>
				[
					'class' => 'nowrap',
				],
            ],
        ],
    ]); ?>
    <?php \yii\widgets\Pjax::end()?>


</div>
<script>
$('#utab').DataTable({
				paging: false,
				columnDefs: [
				{
					orderable: false,
					targets: 1
				},
				{
					orderable: false,
					targets: 2
				},
				{
					orderable: false,
					targets: 6
				},
				{
					orderable: false,
					targets: 6
				},
				{
					orderable: false,
					targets: 6
				},
				{
					orderable: false,
					targets: 6
				},

				],
				"responsive": true, "lengthChange": false, "autoWidth": false,
//				order: [[1, 'desc']]
			});
			$('.dataTables_length').addClass('bs-select');
</script>
<style>
.table-min td{ max-width:450px; width: auto;}
</style>
