var page = {
    save: function (call) {
        var treeObj = $.fn.zTree.getZTreeObj("orgTree");
        var rows = treeObj.getCheckedNodes(true);
        if (rows.length < 1) {
            topWin.layer.alert('未选中任何数据', {icon: 0});
            return false;
        }
        var userId = $('#userId').val();
        var data = [];
        $.each(rows, function (index, item) {
            data.push(item.id);
        });
        $.ajax({
            url: g.ctx + 'pc/sys/user/updateOrgRel',
            type: 'POST',
            data: {userId: userId, orgIds: data.join(',')},
            success: function () {
                call();
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
};


$(function () {
    initTree('');
});

function initTree(searchName) {
    var userId = $('#userId').val(), orgCodes = $('#orgCodes').val(), orgTree = '#orgTree';
    $(orgTree).initZtree({
        service: g.ctx + 'pc/sys/org/tree?userId=' + userId + "&searchName=" + searchName
    }, {
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "s", "N": "ps"}
        },
        view: {
            showLine: false
        },
        callback: {
            /**
             * 树的节点点击事件
             */
            // onClick: page.tree_choice
        }
    });
}

function searchTree() {
    var searchName = $("#search_name").textbox("getValue");
    initTree(searchName);
}
