var page = {
    save: function (call) {
        var rows = $('#orgTree').tree('getChecked');
        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.attributes);
        });
        $.ajax({
            url: g.ctx + 'pc/sys/user/updateFactoryRel',
            type: 'POST',
            data: {userId: userId, factoryCodes: data.join(",")},
            success: function () {
                call();
            },
            error: function (data) {
                topWin.layer.alert(data, {icon: 2});
            }
        });
    }
};


$(function () {
    var userId = $('#userId').val(), orgCodes = $('#orgCodes').val(), orgTree = '#orgTree';
    var loadIndex = layer.msg('加载中...', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        type: 'GET',
        url: g.ctx + 'pc/sys/factory/rest/getForTree',
        data: {
            userId: userId
        },
        success: function (res) {
            layer.close(loadIndex);
            $(orgTree).tree({
                data: res,
                checkbox: true,
                checkOnSelect: true,
                onDblClick: function (node) {
                    if (node.id === '-1') {
                        topWin.layer.alert('只能选择对应工厂', {icon: 0});
                    } /*else {
                        select(node.id, node.text);
                    }*/
                }
            });
        },
        error: function (e) {
            layer.close(loadIndex);
            var respObj = JSON.parse(e.responseText);
            topWin.layer.alert(respObj.message || (typeof (respObj === 'string') ? respObj : "出现错误"), {icon: 2});
        }
    });
});

