<?php

use yii\grid\GridView;
use yii\helpers\{
    Url, Html
};

/** @var $this yii\web\View */
/** @var $searchModel app\models\CronSearch */
/** @var $dataProvider yii\data\ActiveDataProvider */

$template = null;
if (Yii::$app->user->identity->can('runCron')) {
    $template .= ' {run}';
}
if (Yii::$app->user->identity->can('updateCron')) {
    $template .= ' {update}';
}
if (Yii::$app->user->identity->can('deleteCron')) {
    $template .= ' {delete}';
}

$this->title = Yii::t('app', 'Планировщик задач');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="crons-index">
    <div class="box">
        <div class="box-header with-border">
            <div class="pull-left">
                <?php
                if (Yii::$app->user->identity->can('createCron')) {
                    echo Html::a(Yii::t('app', 'Новая задача'), ['create'], ['class' => 'btn btn-flat btn-success']);
                }
                ?>
            </div>
            <div class="pull-right">
                <?php
                if (Yii::$app->user->identity->can('resetCron')) {
                    echo Html::a(Yii::t('app', 'Сбросить по умолчанию'), ['reset'],
                        ['class' => 'btn btn-flat btn-success']);
                }
                ?>
            </div>
        </div>
        <div class="box-body table-responsive">
            <?php if (Yii::$app->session->hasFlash('success')): ?>
                <div class="alert alert-success alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <h4><i class="icon fa fa-check"></i> <?= Yii::t('app', 'Успешно') ?></h4>
                    <?= Yii::$app->session->getFlash('success') ?>
                </div>
            <?php endif; ?>
            <?php if (Yii::$app->session->hasFlash('error')): ?>
                <div class="alert alert-danger alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <h4><i class="icon fa fa-ban"></i> <?= Yii::t('app', 'Ошибка') ?></h4>
                    <?= Yii::$app->session->getFlash('error') ?>
                </div>
            <?php endif; ?>
            <?= GridView::widget([
                'dataProvider' => $dataProvider,
                //'filterModel' => $searchModel,
                'tableOptions' => [
                    'class' => 'table table-striped table-bordered table-hover',
                ],
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn','headerOptions' => ['style' => 'width:60px']],
//                    [
//                        'attribute' => 'id',
//                        'headerOptions' => ['style' => 'width:60px'],
//                    ],
                    [
                        'attribute' => 'enabled',
                        'filter' => [0 => Yii::t('app', 'Нет'), 1 => Yii::t('app', 'Да')],
                        'format' => 'boolean',
                        'headerOptions' => ['style' => 'width:100px'],
                    ],
                    'name',
                    'time',
                    'job',
                    [
                        'class' => 'yii\grid\ActionColumn',
                        'headerOptions' => ['width' => '65'],
                        'template' => $template,
                        'buttons' => [
                            'run' => function ($url, $model) {
                                return Html::a('<span class="glyphicon glyphicon-play"></span>',
                                    Url::toRoute(['run', 'id' => $model->id]), [
                                        'title' => Yii::t('yii', 'Запустить'),
                                        'data-confirm' => Yii::t('yii',
                                            'Вы уверены, что хотите удалить этот элемент?'),
                                        'data-method' => 'post',
                                        'data-pjax' => '0',
                                    ]);
                            }
                        ],
                    ],
                ],
            ]); ?>
        </div>
    </div> <!--end box -->
</div>
