<?php

use \yii\helpers\Html;
use \yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use yii\helpers\Url;

?>

<div>
    <?php $form = ActiveForm::begin([
        'options' => ['id' => 'create-view'],
    ]); ?>
    
    <?= $form->field($model, 'formId')->widget(Select2::class, [
        'name' => 'form',
        'data' => ArrayHelper::map($forms, 'id', 'name'),
        'options' => [
            'id' => 'forms-list',
            'placeholder' => 'Select form',
        ],
        'pluginEvents' => [
            'change' => 'function(){
                var value = $(this).val();
                $("#loader").show();
                $.ajax({
                    url: "' . Url::base(true) . '/views/check-form-available-domains?form_id=" + value,
                    method: "GET",
                    success: function(response){
                        var content = "";
                        for(var i = 0; i < response.length; i++){
                            content += "<label><input type=\"radio\" name=\"View[domain]\" value=\"" + response[i]["value"] + "\"> " + response[i]["label"] + "</label> &nbsp; &nbsp;";
                        }
                        $("#domains-list").html(content);
                        $("#domains-list-container").slideDown(250);
                        $("#loader").hide();
                    },
                    error: function(error){
                        consoel.log(error);
                        $("#loader").hide();
                    }
                });
            }'
        ],
    ])->label('Select form to create view for') ?>

    <div id="domains-list-container" style="display: none;">
        <?= $form->field($model, 'domain')->radioList([], [
            'encode' => false,
            'separator' => '&nbsp; &nbsp; &nbsp;',
            'id' => 'domains-list'
        ]) ?>
    </div>

    <div class="center-elements" style="margin-top: 20px;">
        <?= Html::submitButton(Yii::t('app', 'Ok'), ['class' => 'btn btn-success btn-expanded']) ?>
    </div>

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