<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\components\artboxtree\ArtboxTreeHelper;
use common\modules\product\helpers\ProductHelper;
use kartik\select2\Select2;

/* @var $this yii\web\View */
/* @var $model common\modules\product\models\Product */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="product-form">

    <?php $form = ActiveForm::begin([
        'options' => ['enctype' => 'multipart/form-data']
    ]); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'is_top')->checkbox(['label' => 'ТОП']) ?>
    <?= $form->field($model, 'is_new')->checkbox(['label' => 'Новинка']) ?>
    <?= $form->field($model, 'akciya')->checkbox(['label' => 'Акционный']) ?>

    <?= $form->field($model, 'description')->widget(\mihaildev\ckeditor\CKEditor::className(),['editorOptions' => [ 'preset' => 'full', 'inline' => false, ], ]); ?>
    <?= $form->field($model, 'video')->textarea(); ?>

    <?= $form->field($model, 'brand_id')->dropDownList(
        ArrayHelper::map(ProductHelper::getBrands()->all(), 'brand_id', 'name'),
        [
            'prompt' => Yii::t('product', 'Select brand')
        ]
    ) ?>

    <?= $form->field($model, 'categories')->widget(Select2::className(), [
            'data' => ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'name'),
            'language' => 'ru',
            'options' => [
                'placeholder' => Yii::t('product', 'Select categories'),
                'multiple' => true,
            ],
            'pluginOptions' => [
                'allowClear' => true
            ],
        ]
    ) ?>

    <?= $form->field($model, 'imagesUpload[]')->widget(\kartik\file\FileInput::classname(), [
        'language' => 'ru',
        'options' => [
            'accept' => 'image/*',
            'multiple' => true,
        ],
        'pluginOptions' => [
            'allowedFileExtensions' => ['jpg', 'gif', 'png'],
            'initialPreview' => !empty($model->imagesHTML) ? $model->imagesHTML : [],
            'initialPreviewConfig' => $model->imagesConfig,
            'overwriteInitial' => false,
            'showRemove' => false,
            'showUpload' => false,
//            'uploadUrl' => empty($model->product_id) ? null : \yii\helpers\Url::to(['/product/manage/uploadImage']),
            'uploadAsync' => !empty($model->product_id),
            'previewFileType' => 'image',
        ],
    ]); ?>

    <?php if(isset($groups)) :?>
    <?php  foreach($groups->all() as $group) :?>
        <?= $form->field($model, 'options')->checkboxList(
        ArrayHelper::map($group->options, 'tax_option_id', 'ValueRenderFlash'),
        [
            'multiple' => true,
            'unselect' => null,
        ]
        )->label($group->name);?>
    <?php endforeach?>
    <?php endif?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('product', 'Create') : Yii::t('product', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
