<?php

use yii\grid\GridView;
use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model common\models\Event */
/* @var $permissions common\modules\event\models\EventPermissions */

$this->title = \Yii::t('event', 'Event Details');
$this->params['breadcrumbs'][] = ['label' => Yii::t('event', 'Events'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

/**
 * @var \common\modules\event\EventModule
 */
$module = \common\modules\event\EventModule::getInstance();

function isReservationOpen($model)
{
    $canceled = isset($model['event_registration_canceled_at']);
    $deleted = isset($model['event_registration_deleted_at']);
    return !$canceled && !$deleted;
}


function getCommonColumnsHtmlOptions($model)
{
    $options = [];
    if (!isReservationOpen($model)) {
        $options['style'] = 'text-decoration: line-through;';
    }
    return $options;
}

?>
<div class="event-view">

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

    <p>
        <?php
        if ($permissions->allowEdit) {
            echo Html::a(Yii::t('event', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']);
            echo "&nbsp";
            echo Html::a("Felszerelés", ['equipment-types-assignment', 'id' => $model->id], ['class' => 'btn btn-primary']);
        }
        if ($model->canReserve()) {
            echo "&nbsp";
            echo Html::a(Yii::t('event', 'Reservation'), ['reserve-card', 'id' => $model->id], ['class' => 'btn btn-primary']);
        }
        ?>
        <?php
        if ($permissions->allowDelete) {

            if ($model->canDelete()) {
                echo Html::a(Yii::t('event', 'Delete'), ['delete', 'id' => $model->id], [
                    'class' => 'btn btn-danger pull-right',
                    'data' => [
                        'confirm' => Yii::t('event', 'Are you sure you want to delete this item?'),
                        'method' => 'post',
                    ],
                ]);
            }
        }
        ?>
    </p>

    <?php echo $this->render('_view', ['model' => $model]); ?>

    <div class="panel panel-default">
        <div class="panel-heading">Szükséges felszerelés</div>
        <div class="panel-body">
            <?php
            echo GridView::widget([
                'dataProvider' => $equipmentAssignmentDataProvider,
                'columns' => [
                    [
                        'label' => "Felszerelés",
                        'value' => function ($data) {
                            return $data->eventEquipmentType->name;
                        },
                    ],
                    [
                        'label' => "Rendelkezésre álló mennyiség",
                        'value' => function ($data) {
                            return $data->count;
                        },
                    ],
                ],
            ]);
            ?>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Foglalások</h3>
        </div>
        <div class="panel-body">
            <?php try {
                echo GridView::widget([
                    'dataProvider' => $dataProvider,
                    'columns' => [
                        [
                            'attribute' => 'card_number',
                            'label' => \Yii::t('event', 'Card Number'),
                            'contentOptions' => function ($model) {
                                return getCommonColumnsHtmlOptions($model);
                            },
                            'format' => 'raw',
                            'value' => function ($model, $key, $index, $column) {
                                if ( \common\modules\event\EventModule::getInstance()->mode =='frontend' ){
                                    return Html::a($model['card_number'], ['/ticket/create', 'number' => $model['card_number']]);
                                }else{
                                    return Html::a($model['card_number'], ['card/view', 'id' => $model['card_id_card']]);
                                }
                            }
                        ],
                        [
                            'attribute' => 'customer_name',
                            'label' => \Yii::t('event', 'Customer Name'),
                            'contentOptions' => function ($model) {
                                return getCommonColumnsHtmlOptions($model);
                            },
                            'format' => 'raw',
                            'value' => function ($model, $key, $index, $column) {
                                if ( \common\modules\event\EventModule::getInstance()->mode =='frontend' ) {
                                    return Html::a($model['customer_name'], ['/ticket/create', 'number' => $model['card_number']]);
                                }else{
                                    return Html::a($model['customer_name'], ['customer/view', 'id' => $model['customer_id_customer']]);
                                }
                            }
                        ],
                        [
                            'attribute' => 'customer_email',
                            'label' => \Yii::t('event', 'Customer Email'),
                            'contentOptions' => function ($model) {
                                return getCommonColumnsHtmlOptions($model);
                            }
                        ],
                        [
                            'attribute' => 'event_registration_created_at',
                            'label' => \Yii::t('event', 'Event Registration Created At'),
                            'contentOptions' => function ($model) {
                                return getCommonColumnsHtmlOptions($model);
                            }
                        ],
                        [
                            'attribute' => 'event_registration_canceled_at',
                            'format' => 'datetime',
                            'label' => \Yii::t('event', 'Canceled At'),
                            'contentOptions' => function () {
                                $options = [];
                                return $options;
                            }
                        ],
                        [
                            'attribute' => 'event_registration_deleted_at',
                            'format' => 'datetime',
                            'label' => \Yii::t('event', 'Deleted At'),
                            'contentOptions' => function () {
                                $options = [];
                                return $options;
                            },
                        ],
                        [
                            'class' => 'yii\grid\ActionColumn',
                            'template' => '{cancel-registration} {delete-registration}',
                            'urlCreator' => function ($action, $model) {
                                $params = ['id' => $model['event_registration_id']];
                                $params[0] = "event" . '/' . $action;
                                return \yii\helpers\Url::toRoute($params);
                            },
                            'buttons' => [
                                'cancel-registration' => function ($url, $model) {
                                    if (isset($model['event_registration_canceled_at'])) {
                                        return "";
                                    }
                                    $options = [
                                        'title' => Yii::t('event', 'Cancel'),
                                        'aria-label' => Yii::t('event', 'Cancel'),
                                        'data-confirm' => Yii::t('event', 'Are you sure you want to cancel this item? Usage count won\'t be restored!'),
                                        'data-method' => 'post',
                                        'data-pjax' => '0',
                                    ];
                                    return Html::a('<span class="glyphicon glyphicon-ban-circle"></span>', $url, $options);
                                },
//                                'delete-registration' => function ($url, $model) {
//                                    if (isset($model['event_registration_canceled_at'])) {
//                                        return "";
//                                    }
//                                    $options = [
//                                        'title' => Yii::t('yii', 'Delete'),
//                                        'aria-label' => Yii::t('yii', 'Delete'),
//                                        'data-confirm' => Yii::t('event', 'Are you sure you want to delete this item? Usage count will be restored!'),
//                                        'data-method' => 'post',
//                                        'data-pjax' => '0',
//                                    ];
//                                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
//                                },
                            ]

                        ],
                    ]
                ]);
            } catch (Exception $e) {
                echo "Failed to render registrations";
            }
            ?>
        </div>
    </div>


</div>
