$(document).ready(function () {
    $('#jstree').parent().css('height', $(window).height() - 185);
    $('#jstree').jstree({
        checkbox: {
            "keep_selected_style": false,
            "tie_selection": false,
            "whole_node": false
        },
        plugins: [
            "search", "checkbox"
        ],
        core: {
            data: {
                type: 'POST',
                url: "RoleEdit/GetTreeRoot",
                dataType: 'JSON',
                data: '{roleCode:' + getQuery('roleCode') + '}',
                contentType: "application/json; charset=utf-8",
                success: function (children) {
                }
            }
        }
    });

    $('#jstree').bind('select_node.jstree', function (e, data) {
        
        $('#tblAccess').html('');
        var json = eval($('#hfAccess').val());
        var result = json.filter(function (item) { return (item.MenuName == data.node.id); });
        $.each(result, function (index, item) {
            $('#tblAccess').append('<div style="width: 100%; height: 40px; background-color: white; border-bottom: 1px solid #E6E6E6;">' +
                '<div style="float: right; width: 90%; font-size: 10px; color: black; line-height: 33px; font-weight: bold; margin: 0;">' + item.ActionNameFarsi + '</div>' +
                '<input id="' + index + '" data-id="' + item.ActionName + '" style="float: left" type="checkbox" hidden="hidden" ' + (item.HasAccess ? ' checked="checked"' : '') + ' />' +
                '<label class="switch" style="float: left; margin-top: 14px;" for="' + index + '"></label>' +
                '</div>');
        });
    });
});

function nextStep() {
    $('#step1').hide();
    $('#step2').show();
    $('#spStep1').removeClass('step-active');
    $('#spStep2').addClass('step-active');
    $('.button-accept').val(window.top.okFinish);
    $('.label-cancel').text(window.top.previous);
    $('.button-accept').attr('onclick', 'save();return false;');
    $('.label-cancel').attr('onclick', 'previousStep();');
}
function previousStep() {
    $('#step1').show();
    $('#step2').hide();
    $('#spStep1').addClass('step-active');
    $('#spStep2').removeClass('step-active');
    $('.button-accept').val(window.top.next);
    $('.label-cancel').text(window.top._cancel);
    $('.button-accept').attr('onclick', 'nextStep();return false;');
    $('.label-cancel').attr('onclick', 'closeTopModal();');
}

function save() {
    var lst = "";
    var delLink4 = new Array();

    $.each($("#jstree").jstree("get_checked", true), function () { lst += this.id + ","; });

    $('#tblAccess [data-id]').each(function () {
        if ($(this).is(':checked'))
            delLink4.push($(this).attr('data-id'));
        else
            delLink4.push($(this).attr('data-id'));
    });
    
}
