<?php

use common\modules\file\widgets\ImageUploader;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

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

<div class="banner-form">

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


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

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

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

    <?= $form->field($model, 'status')->widget(Select2::className(),([
        'name' => 'status',
        'hideSearch' => true,
        'data' => [1 => 'Active', 2 => 'Inactive'],
        'options' => ['placeholder' => 'Select status...'],
        'pluginOptions' => [
            'allowClear' => true
        ]
    ])) ?>


    <div id="image_widget_block">

        <?= $form->field($model, 'image')->widget(\kartik\file\FileInput::classname(), [
            'language' => 'ru',
            'options' => [
                'accept' => 'image/*',
                'multiple' => false,
            ],
            'pluginOptions' => [
                'allowedFileExtensions' => ['jpg', 'gif', 'png'],
                'initialPreview' => !empty($model->imageUrl) ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'list') : '',
                'overwriteInitial' => true,
                'showRemove' => false,
                'showUpload' => false,
                'previewFileType' => 'image',
            ],
        ]); ?>
    </div>

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

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

</div>
<script>
    $('#save_image_widget_settings').click(function(){
        var width = $('#banner-width').val();
        var height = $('#banner-height').val();
        save_image_widget_settings( width, height, function(data){
            $('#image_widget_block').html = '';
            $('#image_widget_block').html(data.html);
        });
    });
    function save_image_widget_settings( width, height, callback )
    {
        $.ajax({
            url: '/admin/banner/save-image-settings',
            data        :
            {
                'width'       : width,
                'height'   : height
            },
            type : 'POST',
            dataType: 'json',
            success: function (data)
            {
                if(callback)
                    callback(data);
            },
            error: function()
            {
                console.info('error');
            }
        });
    }
</script>
