<?php
use \common\modules\analyticsSchool\models\AnalyticsSchoolLesson;
use \yii\grid\GridView;
use \yii\data\ActiveDataProvider;
use \yii\bootstrap\Html;

/**
 * @var AnalyticsSchoolLesson $model
 * @var ActiveDataProvider $dataProvider
 */

echo Html::tag('h2', 'Вложения');

if ($model->getErrors('file'))
    echo Html::tag('span', current($model->getErrors('file')), ['style' => 'color: #ff5b57;']);

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'options' => [
        'id' => 'lesson-file'
    ],
    'columns' => [
        'title',
        'file',

        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{delete}',
            'buttons' => [
                'delete' => function($url, $model) {
                        return Html::tag(
                            'a',
                            Html::tag('span', '', ['class' => 'glyphicon glyphicon-trash']),
                            ["href" => Yii::$app->urlManager->createUrl(['/analyticsSchool/lesson-admin/deletefile', 'id' => $model->id])]
                        );
                    }
            ]
        ],
    ],
]);
echo Html::button('Добавить файл', ['id' => 'add_file', 'class' => 'btn btn-primary pull-right']);
?>

<script type='text/javascript'>
    $(document).on('click', '#add_file', function() {
        $('div#lesson-file').children('table').children('tbody').append(
            '<tr><td><input type="text" name="AnalyticsSchoolLesson[filename][]" class="form-control"></td><td><input type="file" name="AnalyticsSchoolLesson[file][]"></td><td><input type="button" class="btn btn-warning cancel" value="Отменить"></td></tr>'
        );
    });
    $(document).on('click', 'input.cancel', function() {
        $(this).parent('td').parent('tr').remove();
    });
</script>