<?php 

use yii\helpers\Html;
use yii\helpers\Url;

$base_url = json_encode(Url::base(true));

$this->registerCssFile('@web/css/themes/proton/style.css');
$this->registerJsFile('@web/js/jstree.js');

$current_active_node = json_encode(Yii::$app->session->get('user.admin_submit_active_node_name'));

$script =<<< JS
    var currentActiveNode = $current_active_node;

    $(document).ready(function(){

        $('#data')
            .on('loaded.jstree', function(e, data){
                $("#data").jstree('open_all');
                $('#data').jstree(true).select_node(currentActiveNode);
                // We mark the choosen nodes as selected when the user opens modal again
            })
            .jstree({
                'core' : {
                    "multiple": false,
                    'themes': {
                        'name': 'proton',
                    },
                    'data': $data
                }
        });

        $('.select-btn').click(function(e){
            e.preventDefault();
            var selectedNode = $('#data').jstree('get_selected');
            $.ajax({
                method: 'POST',
                url: $base_url + '/users/change-admin-active-node',
                data: {
                    selectedNode: selectedNode
                },
                success: function(response){
                    $('#admin-active-node').text(response);
                    $('#tree-modal').modal('hide');
                }
            });
        });
    });

JS;

$this->registerJs($script);

?>

<h2><?= 'Select owner node for the submitted data' ?></h2>
<div id="data" style="margin-bottom: 10px;"></div>

<div class="modal-footer">
    <?= Html::a('Select', [''], ['class' => 'btn btn-success btn-expanded select-btn']) ?>
    <?= Html::a('Close', [''], ['class' => 'btn btn-default btn-expanded', 'data-dismiss' => 'modal']) ?>
</div>
