<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\grid\GridView;
use kartik\export\ExportMenu;
use yii\bootstrap\Modal;
use dosamigos\datepicker\DatePicker;

$this->title = Yii::t('app', 'Evaluations');
$this->params['breadcrumbs'][] = $this->title;

$columns = [
    [
        'attribute' => 'trainee_full_name',
        'headerOptions' => ['style' => 'width: 20%;'],
        'value' => function($model){
            return $model->trainingRequest->full_name;
        }
    ],
    [
        'attribute' => 'trainee_university_name',
        'headerOptions' => ['style' => 'width: 15%;'],
        'value' => function($model){
            return $model->trainingRequest->university_name;
        }
    ],
    [
        'attribute' => 'trainee_training_start_date',
        'headerOptions' => ['style' => 'width: 15%;'],
        'format' => 'raw',
        'filter' => DatePicker::widget([
            'name' => 'EvaluationsSearch[trainee_training_start_date]',
            'model' => $searchModel,
            'attribute' => 'trainee_training_start_date',
            'language' => Yii::$app->language,
            'clientOptions' => [
                'autoclose' => true,
                'format' => 'yyyy-mm-dd',
                'todayHighlight' => true,
                'todayBtn' => 'linked',
            ]
        ]),
        'value' => function($model){
            return $model->training->start_date;
        }
    ],
    [
        'attribute' => 'trainee_training_end_date',
        'headerOptions' => ['style' => 'width: 15%;'],
        'format' => 'raw',
        'filter' => DatePicker::widget([
            'name' => 'EvaluationsSearch[trainee_training_end_date]',
            'model' => $searchModel,
            'attribute' => 'trainee_training_end_date',
            'language' => Yii::$app->language,
            'clientOptions' => [
                'autoclose' => true,
                'format' => 'yyyy-mm-dd',
                'todayHighlight' => true,
                'todayBtn' => 'linked',
            ]
        ]),
        'value' => function($model){
            return $model->training->end_date;
        }
    ],
    [
        'attribute' => 'trainee_training_branch',
        'headerOptions' => ['style' => 'width: 10%;'],
        'filter' => ArrayHelper::map($branches, 'id', 'branch'),
        'value' => function($model){
            return $model->training->branch->branch;
        }
    ],
    [
        'attribute' => 'trainee_mobile_phone',
        'headerOptions' => ['style' => 'width: 10%;'],
        'value' => function($model){
            return $model->trainingRequest->mobile;
        }
    ],
    [
        'attribute' => 'evaluation',
        'headerOptions' => ['style' => 'width: 10%;'],
        'filter' => [
            'Less than 25%'     => Yii::t('app', 'Less than 25%'), 
            '25%-50%'           => Yii::t('app', 'Between 25% and 50%'),
            '50%-75%'           => Yii::t('app', 'Between 50% and 75%'),
            'More than 75%'     => Yii::t('app', 'More than 75%'),
        ],
        'format' => 'raw',
        'value' => function($model){
            return '<span dir="ltr">' . $model->evaluation . ' %</span>';
        }
    ],
    [
        'class' => 'yii\grid\ActionColumn',
        'headerOptions' => ['style' => 'width: 5%;'],
        'template' => '{view}'
    ],
];

?>

<div class="evaluations-index">

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

    <?= Html::a(Yii::t('app', 'Advanced Search'), 
        ['#'], 
        [
            'class' => 'btn btn-purple btn-expanded advanced-search-btn',
            'value' => Url::to(['advanced-search', 'from_date' => isset($_GET['from_date']) ? $_GET['from_date'] : null, 'to_date' => isset($_GET['to_date']) ? $_GET['to_date'] : null]),
            'title' => Yii::t('app', 'Advanced Search')
        ]) ?>

    <?= ExportMenu::widget([
        'dataProvider' => $dataProvider,
        'showConfirmAlert' => false,
        'showColumnSelector' => false,
        'asDropdown' => false,
        'filename' => 'Evaluations',
        'exportConfig' => [
            ExportMenu::FORMAT_CSV => [
                'label' => Yii::t('app', 'Export'),
                'icon' => false,
                'options' => [
                    'title' => Yii::t('app', 'Export'), 
                    'class' => 'btn btn-primary btn-expanded btn-export',
                ]
            ],
            ExportMenu::FORMAT_HTML     => false,
            ExportMenu::FORMAT_EXCEL    => false,
            ExportMenu::FORMAT_TEXT     => false,
            ExportMenu::FORMAT_PDF      => false,
            ExportMenu::FORMAT_EXCEL_X  => false,
        ],
        'columns' => $columns
    ]); ?>
    <br><br>

    <?php if(Yii::$app->session->hasFlash('wrong_selected_dates')): ?>
        <div class="alert alert-danger">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
            <?= Yii::$app->session->getFlash('wrong_selected_dates') ?>
        </div>
    <?php endif; ?>

    <?= GridView::widget([
        'pager' => [
            'firstPageLabel' => Yii::t('app', 'First'),
            'lastPageLabel' => Yii::t('app', 'Last')
        ],
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => $columns
    ]); ?>
</div>

<?php 
    Modal::begin([
        'header' => '<h3>' . \Yii::t('app', 'Advanced Search') . '</h3>',
        'id' => 'advanced-search-modal',
        'size' => 'modal-md'
    ]);

    echo '<div id="advanced-search-modal-content"></div>';

    Modal::end();
?>