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) {
            var obj = {};
            obj.purchaseGroupId = item.id;
            if ($('input[name="setDefault"]:checked').val() === item.id) {
                obj.mark = 1;
            } else {
                obj.mark = 0;
            }
            obj.userId = userId;
            data.push(obj);
        });
        var loading = topWin.layer.msg('处理中, 请等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/sys/user/updatePurchaseGroupRel',
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function () {
                topWin.layer.close(loading);
                call();
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
};


$(function () {
    var userId = $('#userId').val(), orgTree = '#orgTree';
    var loadIndex = layer.msg('加载中...', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        type: 'GET', url: g.ctx + 'pc/sys/purchaseGroup/rest/getForTree', data: {
            userId: userId
        }, success: function (res) {
            layer.close(loadIndex);

            $(orgTree).tree({
                data: res, checkbox: true, checkOnSelect: true, formatter: function (node) {
                    var s = node.text;
                    if (node.checked) {
                        if (node.defaultFlag) {
                            s += '<text style="position: absolute;right: 90px;"><input type="radio" name="setDefault" checked value="' + node.id + '">默认</text>';
                        } else {
                            s += '<text style="position: absolute;right: 90px;"><input type="radio" name="setDefault" value="' + node.id + '">默认</text>';
                        }

                    }
                    return s;
                }, onCheck: function (node) {
                    var s = node.text;
                    if (node.checked) {
                        $(node.target).children(".tree-title").append('<text style="position: absolute;right: 90px;"><input type="radio" name="setDefault" value="' + node.id + '">默认</text>')
                    } else {
                        $(node.target).children(".tree-title").text(s)
                    }
                }, 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});
        }
    });
});

