<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\grid\GridView;
use kartik\editable\Editable;

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

<div class="branch-index">

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

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

    <?= GridView::widget([
        'pager' => [
            'firstPageLabel' => Yii::t('app', 'First'),
            'lastPageLabel' => Yii::t('app', 'Last')
        ],
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute' => 'branch',
                'headerOptions' => ['style' => 'width: 25%;']
            ],
            [
                'attribute' => 'address',
                'headerOptions' => ['style' => 'width: 25%;']
            ],
            [
                'attribute' => 'capacity',
                'headerOptions' => ['style' => 'width: 15%;']
            ],
            [
                'attribute' => 'branch_user_id',
                'format' => 'raw',
                'headerOptions' => ['style' => 'width: 20%;'],
                'filter' => ArrayHelper::map($branches_admins, 'id', 'username'),
                'value' => function($model) use($branches_admins) {
                    return Editable::widget([
                        'name' => 'branch_user_id', 
                        'model' => $model,
                        'attribute' => 'branch_user_id',
                        'asPopover' => true, 
                        'closeOnBlur' => true,
                        'displayValue' => $model->branchUser->username,
                        'displayValueConfig' => ArrayHelper::map($branches_admins, 'id', 'username'),
                        'format' => Editable::FORMAT_LINK,
                        'formOptions' => ['action' => Url::base(true) . '/branches/change-branch-admin?id='. $model->id, 'method' => 'get'],
                        'preHeader' => Yii::t('app', 'Edit') . ' ',
                        'header' => Yii::t('app', 'Branch Admin'),
                        'footer' => '{buttons}' . Html::a('<i class="glyphicon glyphicon-ban-circle"></i>', [''], ['class' => 'btn btn-sm btn-danger close-editable']),
                        'buttonsTemplate' => '{submit}' . (Yii::$app->language == 'ar' ? ' ' : ''),
                        'size'=> 'md',
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,
                        'data' => ArrayHelper::map($branches_admins, 'id', 'username'),
                        'options' => ['id' => 'branch_admin_' . $model->id],
                    ]);
                }
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'headerOptions' => ['style' => 'width: 10%;'],
                'buttons' => [
                    'delete' => function($url, $model){
                        if(! $model->trainings && ! $model->interviews){
                            return Html::a('<span class="glyphicon glyphicon-trash"></span>',
                            ['delete', 'id' => $model->id],
                            [
                                'data' => [
                                    'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
                                    'method' => 'post',
                                ],
                            ]);
                        }
                    }
                ]
            ],
        ],
    ]); ?>
</div>
