<?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,
    ],
    'pluginEvents' => [
        'fileuploaded'=>"function(event, data, previewId, index) {
            $(\"[name='Courses[filename]']\").val(data.files[0].name);
        }",
        'fileclear'=>"function(event) {
            $(\"[name='Courses[filename]']\").val();
        }",
    ],
])."</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охранить']
    ]
];

