<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\grid\GridView;
use yii\bootstrap\Modal;
use app\components\Utilities;

$this->title = Yii::t('app', 'Views');

$script =<<< JS

    $(document).ready(function(){
        $('.create-view-modal-button').click(function(e){
            e.preventDefault();
            $('#create-view-modal').modal('show')
                .find('#create-view-modal-content')
                .load($(this).attr('value'));
        });
    });

JS;

$this->registerJs($script);

?>

<div class="views-index">

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

    <p>
        <?= Html::a('Create View', [''],
                [
                    'class' => 'create-view-modal-button btn btn-success btn-expanded',
                    'value' => Url::base(true) . '/views/show-create-view-modal'
                ])
            ?>
    </p>

    <?= Utilities::getSessionAlertContainer('view_saved', 'success') ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute' => 'name',
                'headerOptions' => ['style' => 'width: 20%'],
            ],
            [
                'attribute' => 'formId',
                'label' => 'Form Name',
                'headerOptions' => ['style' => 'width: 20%'],
                'filter' => ArrayHelper::map(Yii::$app->user->identity->is_admin ? Utilities::getAllForms() : Utilities::getFormsByUserPermissions(), 'id', 'name'),
                'value' => function($model){
                    $query = new \yii\mongodb\Query();
                    $form = $query
                        ->from('forms')
                        ->where([
                            '_id' => Utilities::getIdAsObject($model->formId)
                        ])
                        ->one();
                    if($form){
                        return $form['name'];
                    }
                }
            ],
            [
                'attribute' => 'isDefaultView',
                'label' => 'Default View',
                'filter' => [
                    false => 'No',
                    true => 'Yes'
                ],
                'headerOptions' => ['style' => 'width: 10%'],
                'visible' => ! Yii::$app->user->identity->is_admin,
                'value' => function($model){
                    return $model->isDefaultView == true ? 'Yes' : 'No';
                }
            ],
            [
                'attribute' => 'domain',
                'headerOptions' => ['style' => 'width: 10%'],
                'format' => 'raw',
                'filter' => [
                    'fields' => 'Fields',
                    'matrix' => 'Matrix',
                    'file' => 'File'
                ],
                'value' => function($model){
                    $content = ucfirst($model->domain);
                    if($model->domain == 'matrix'){
                        $content .= '<br><span class="not-set">(' . Utilities::getViewSelectedMatrixName($model->_id) . ')</span>';
                    }
                    return $content;
                }
            ],
            [
                'attribute' => 'additionalData',
                'headerOptions' => ['style' => 'width: 15%'],
                'format' => 'raw',
                'filter' => [
                    0 => 'No',
                    1 => 'Yes'
                ],
                'value' => function($model){
                    return $model->additionalData === 'false' ? 'No' : 'Yes';
                }
            ],
            [
                'attribute' => 'createdAt',
                'headerOptions' => ['style' => 'width: 15%'],
                'value' => function($model){
                    return date('Y-m-d H:i', strtotime($model->createdAt));
                }
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'headerOptions' => ['style' => 'width: 10%'],
                'template' => '{data} {update} {delete}',
                'visibleButtons' => [
                    'update' => function($model){
                        if(! Yii::$app->user->identity->is_admin && $model->isDefaultView){
                            return false;
                        }
                        return true;
                    },
                    'delete' => function($model){
                        if(! Yii::$app->user->identity->is_admin && $model->isDefaultView){
                            return false;
                        }
                        return true;
                    },
                ],
                'buttons' => [
                    'data' => function($url, $model){
                        return Html::a('<span class="glyphicon glyphicon-ok"></span>', ['data', 'id' => (string)$model->_id], ['title' => 'Data', 'style' => 'margin-right: 5px;']);
                    },
                    'update' => function($url, $model){
                        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['update', 'id' => (string)$model->_id], ['style' => 'margin-right: 5px;']);
                    },
                    'delete' => function($url, $model){
                        return Html::a('<span class="glyphicon glyphicon-trash"></span>', 
                            ['delete', 'id' => (string)$model->_id], 
                            [
                                'title' => 'Delete',
                                'data' => [
                                    'confirm' => 'Are you sure you want to delete this form?'
                                ],
                                'style' => 'margin-right: 5px;'
                            ]);
                    },
                ]
            ],
        ],
    ]); ?>
</div>

<?php 
    Modal::begin([
        'header' => '<h3>' . 'Create View' . '</h3>',
        'id' => 'create-view-modal',
        'size' => 'modal-md',
        'closeButton' => false
    ]);

    echo '<div id="create-view-modal-content"></div>';

    Modal::end();
?>