<?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(); ?>


    <?= $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
        ]
    ])) ?>

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

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

    <p id="save_image_widget_settings" class = "btn btn-primary" >Применить настройки</p>

    <div id="image_widget_block">
        <?php if(!empty($model->image)){
            echo ImageUploader::widget([
                'model'=> $model,
                'field'=>'image',
                'size' => [
                    [
                        'width'=>$model->width,
                        'height'=>$model->height,
                    ],
                ],
                'gallery' =>$model->image,
                'name' => "Загрузить баннер"
            ]);
        }?>
    </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>
