<?php

use app\models\Bills;
use app\models\Lots;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;

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

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

$this->registerJs(<<<JS
$('.payed-radio  input[type="radio"]').on('change', function(e){
    var radio = $(this);
    $.get(
        radio.parents('.payed-radio').data('url') + '&payed=' + radio.val());
});
JS
);
?>
<div class="panel panel-primary">

    <div class="panel-heading"><span
                class="glyphicon glyphicon-stats"></span><strong> <?= Html::encode($this->title) ?></strong></div>
    <div class="panel-body">

        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                [
                    'attribute' => 'user_at_org',
                    'value' => 'user.at_org',
                    'visible' => Yii::$app->user->can('admin'),
                ],
                [
                    'attribute' => 'bid_id',
                    'value' => function ($model) {
                        return Html::a($model->bid_id,
                            [
                                '/bidding/view',
                                'id' => $model->bid_id,
                            ],
                            ['target' => '_blank']
                        );
                    },
                    'format' => 'raw',
                ],
                [
                    'attribute' => 'auction_id',
                    'value' => function ($model) {
                        return Html::a(isset($model->auction->id) ? $model->auction->id : 0,
                            [
                                '/publishing/view',
                                'id' => isset($model->auction->id) ? $model->auction->id : 0
                            ],
                            ['target' => '_blank']
                        );
                    },
                    'format' => 'raw',
                ],
                [
                    'attribute' => 'type',
                    'value' => function ($model) {
                        return getValue(Bills::types(), $model->type);
                    },
                    'filter' => Bills::types(),
                ],
                [
                    'attribute' => 'payed',
                    'filter' => Bills::payStatuses(),
                    'format' => 'raw',
                    'value' => function ($model) {
                        if (!Yii::$app->user->can('admin')) {
                            return getValue(Bills::payStatuses(), $model->payed);
                        } else {
                            return Html::radioList('payed' . $model->id,
                                [$model->payed],
                                Bills::payStatuses(),
                                [
                                    'class' => 'payed-radio',
                                    'data-url' => Url::to([
                                        '/bills/payed',
                                        'id' => $model->id
                                    ])
                                ]
                            );
                        }
                    }
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{edit}{download}',
                    'buttons' => [
                        'download' => function ($url, $model, $key) {
                            return Html::a(
                                '<i class="glyphicon glyphicon-circle-arrow-down"></i>',
                                [
                                    '/bills/download-requisites',
                                    'id' => $model->id
                                ],
                                [
                                    'target' => '_blank',
                                    'title' => Yii::t('app', 'Download bill')
                                ]
                            );
                        },
                    ]
                ],
            ],
        ]); ?>
    </div>
</div>
