/**
 * 角色权限分配指标项
 * @Author wang
 */

var el = {
    id: '#id',
    tb: '#tb',
    resources: '#resources'
};

var selfData = {
    resList: []
};

$(function () {
    //初始化表格
    loadGrid();
    //初始化数据
    initData();
});

//初始化数据
function initData() {
    var resources = $(el.resources).val();
    selfData.resList = eval('(' + resources + ')');

    //获取权限表格数据。并处理数据，做成树结构
    $.get(g.ctx + 'pc/sys/dealt/info/rest/tree/grid', {}, function (data) {
        $(el.tb).treegrid('loadData', data);
    });
}

//初始化表格
function loadGrid() {
    $(el.tb).treegrid({
        iconCls: 'icon-list',
        title: '',
        singleSelect: false,
        pagination: false,
        idField: 'id',
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: true,
        remoteSort: true,
        striped: true,
        treeField: 'text',
        animate: true,
        queryParams: {
            type: 1
        },
        columns: [[
            {
                field: 'text',
                title: '分配指标项',
                width: '300',
                align: 'left'
            },
            {
                title: 'id',
                field: 'id',
                checkbox: true,
                align: 'center',
                width: 60
            }
        ]],
        onLoadSuccess: function () {
            $.each(selfData.resList, function (index, item) {
                $(el.tb).treegrid('select', item);
            });
        },
        onSelect: function (row) {
            //选中时，父级节点都要选中
            var parent = $(el.tb).treegrid('getParent', row.id);
            if (parent) {
                $(el.tb).treegrid('select', parent.id);
            }
        },
        onUnselect: function (row) {
            //取消选中，所有子节点都要取消选中
            var children = $(el.tb).treegrid('getChildren', row.id);
            if (children.length > 1) {
                $.each(children, function (index, item) {
                    $(el.tb).treegrid('unselect', item.id);
                })
            }
        }
    });
}

//分配指标项分配 保存事件
function submitForm(call) {

    var rows = $(el.tb).treegrid('getSelections');
    var roleId = $(el.id).val();

    var resources = [];
    if (rows.length > 0) {
        $.each(rows, function (index, item) {
            if (Number(item.isLast) === 1) {
                resources.push(item.id);
            }
        });

        var dealtWithTargetRoleDto = {};
        dealtWithTargetRoleDto.roleId = roleId;
        dealtWithTargetRoleDto.targetInfoIds = resources;
        $.ajax({
            url: g.ctx + 'pc/sys/role/updateDealtConfig',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(dealtWithTargetRoleDto),
            success: function (data) {
                topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    call();
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    } else {
        layer.msg('未选择任何数据');
    }

}