<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use yii\helpers\Url;

use common\modules\school\models\Courses;
use common\modules\testings\models\Test;

$initialImages = '';
if ($model->image) {
    $initialImages[] = Html::img($model->getUrl($model->image));
}

$image = "<div class='file-widget'>".FileInput::widget([
    'model' => $model,
    'attribute' => 'upload_image',
    'language' => 'en',
    'options' => ['multiple' => false, 'accept' => 'image/*'],
    'pluginOptions' => [
        'uploadUrl' => Url::to(['/school/courses-admin/upload']),
        'showUpload' => false,
        'overwriteInitial'=>true,
        'initialPreview'=>$initialImages,
        'showRemove' => true,
    ],
    'pluginEvents' => [
        'fileuploaded'=>"function(event, data, previewId, index) {
            $(\"[name='Courses[filename]']\").val(data.files[0].name);
        }",
         'filepredelete' => "function(event, key) {
                return (!confirm('Are you sure you want to delete ?')); 
            }",
             'filedelete' => 'function(event, key) { console.log(\'File is delete\'); }',
    ],
])."</div>";


$elements = [
    'test_id' => ['type' => 'dropdownlist', 'items' => ArrayHelper::map(Test::find()->all(), 'id', 'name', 'session.name'), 'empty' => 'Не задан'],
    'title'	=> ['type' => 'text'],
    'description' => ['type' => 'textarea'],
    'description_for_course' => ['type' => 'textarea'],
    'type' => [
        'type' => 'dropdownlist',
        'items' => Courses::$type_list,
    ],
    'spec_proposition'	=> ['type' => 'checkbox'],
    'filename' => ['type' => 'hidden'],
    'image'	=> $image,
];


return [
    'activeForm'=>[
        'id' => 'course-form',
        'options' => [
            'enctype' => 'multipart/form-data'
        ],
    ],
    'elements'       => $elements,
    'buttons' => [
        'submit' => ['type' => 'submit', 'value' => 'Cохранить']
    ]
];

