<?php
    
    use common\models\ProductSpec;
    use common\models\ProductSpecLang;
    use common\modules\language\widgets\LanguageForm;
    use common\modules\product\models\ProductLang;
    use common\modules\rubrication\models\TaxGroup;
    use yii\db\ActiveQuery;
    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 yii\web\View                          $this
     * @var common\modules\product\models\Product $model
     * @var ProductLang[]                         $model_langs
     * @var ProductSpec                           $product_spec
     * @var ProductSpecLang[]                     $product_spec_langs
     * @var yii\widgets\ActiveForm                $form
     * @var ActiveQuery                           $groups
     */
?>

<div class="product-form">
    
    <?php $form = ActiveForm::begin([
        'options' => [ 'enctype' => 'multipart/form-data' ],
    ]); ?>
    
    <?= $form->field($model, 'is_top')
             ->checkbox([ 'label' => Yii::t('app', 'top_form') ]) ?>
    <?= $form->field($model, 'is_new')
             ->checkbox([ 'label' => Yii::t('app', 'new_form') ]) ?>
    <?= $form->field($model, 'akciya')
             ->checkbox([ 'label' => Yii::t('app', 'action_form') ]) ?>
    
    <?= $form->field($model, 'video')
             ->textarea(); ?>
    
    <?= $form->field($model, 'brand_id')
             ->dropDownList(ArrayHelper::map(ProductHelper::getBrands()
                 ->with('lang')
                                                          ->all(), 'brand_id', 'lang.name'), [
                 'prompt' => Yii::t('product', 'Select brand'),
             ]) ?>
    
    <?= $form->field($model, 'categories')
             ->widget(Select2::className(), [
                 'data'          => ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'lang.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,
                     'uploadAsync'           => !empty( $model->product_id ),
                     'previewFileType'       => 'image',
                 ],
             ]); ?>
    <?php
        
        $certOptions = [];
        foreach($model->productCertificates as $productCertificate) {
            $certOptions[] = [
                'url' => \yii\helpers\Url::to([
                    '/product/manage/delcert',
                    'id' => $productCertificate->product_certificate_id,
                ]),
            ];
        }
        echo $form->field($model, 'certificateUpload[]')
                  ->widget(\kartik\file\FileInput::className(), [
                      'language'      => 'ru',
                      'options'       => [
                          'multiple' => true,
                      ],
                      'pluginOptions' => [
                          'allowedFileExtensions' => [
                              'pdf',
                          ],
                          'initialPreview'        => $model->productCertificates ? ArrayHelper::getColumn($model->productCertificates, 'link') : [],
                          'initialPreviewConfig'  => $certOptions,
                          'overwriteInitial'      => false,
                          'showRemove'            => false,
                          'showUpload'            => false,
                          'previewFileType'       => 'image',
                      ],
                  ]); ?>
    
    <?php if(!empty( $groups )) {
        foreach($groups->with('lang')->all() as $group) {
            /**
             * @var TaxGroup $group
             */
             echo $form->field($model, 'options')
                     ->checkboxList(ArrayHelper::map($group->getOptions()->with('lang')->all(), 'tax_option_id', 'lang.value'), [
                         'multiple' => true,
                         'unselect' => NULL,
                     ])
                     ->label($group->lang->name);
        }
    }
    ?>
    
    <hr>
    
    <?php
        echo $form->field($product_spec, 'techCharFile')
                  ->widget(\kartik\file\FileInput::className(), [
                      'language'      => 'ru',
                      'pluginOptions' => [
                          'allowedFileExtensions' => [
                              'txt',
                              'pdf',
                              'xls',
                              'xlsx',
                          ],
                          'initialPreview'        => $product_spec->getTechCharUrl() ? : '',
                          'showUpload'            => false,
                          'showRemove'            => false,
                          'previewFileType'       => 'image',
                      ],
                  ]);
        
        echo $form->field($product_spec, 'techSpecFile')
                  ->widget(\kartik\file\FileInput::className(), [
                      'language'      => 'ru',
                      'pluginOptions' => [
                          'allowedFileExtensions' => [
                              'txt',
                              'pdf',
                              'xls',
                              'xlsx',
                          ],
                          'initialPreview'        => $product_spec->getTechSpecUrl() ? : '',
                          'showUpload'            => false,
                          'showRemove'            => false,
                          'previewFileType'       => 'image',
                      ],
                  ]);
    ?>
    
    <?= LanguageForm::widget([
        'model_langs' => $model_langs,
        'formView'    => '@common/modules/product/views/manage/_form_language',
        'form'        => $form,
    ]) ?>
    
    <?= LanguageForm::widget([
        'model_langs' => $product_spec_langs,
        'formView'    => '@common/modules/product/views/manage/_form_spec_language',
        'form'        => $form,
        'id_prefix'   => 'spec',
    ]) ?>
    
    <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>
