var pageii = null;

$(function () {
    loadGrid();
});


function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + 'pc/sys/role/list',
        method: "GET",
        iconCls: 'icon-list',
        title: '',
        height: gridheight + 20,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "loading......",
        rownumbers: true,
        fitColumns: false,
        nowrap: true,
        remoteSort: true,
        striped: true,
        page: 1,
        pageSize: 10,
        queryParams: {
            page: 1,
            rows: 10,
            name: "",
            desc: ""
        },
        onLoadSuccess: function () {
            $('#tb').datagrid("selectRow", -1);
            $('div.datagrid-toolbar a').eq(2).linkbutton('disable');
            $('div.datagrid-toolbar a').eq(3).linkbutton('disable');
            $('div.datagrid-toolbar a').eq(1).linkbutton('disable');
            $('div.datagrid-toolbar a').eq(4).linkbutton('disable');
            $('div.datagrid-toolbar a').eq(5).linkbutton('disable');

        },
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'name',
                title: roleI18n['roleName'],
                width: '20%',
                align: 'left',
                sortable: true
            },
            {
                field: 'rolekey',
                title: roleI18n['roleCode'],
                width: '20%',
                align: 'left',
                sortable: true
            },
            {
                field: 'description',
                title: roleI18n['roleDesc'],
                width: '59%',
                align: 'left'

            }
        ]],
        toolbar: ["-", {
            text: buttonI18n['add'],
            iconCls: 'icon-add',
            hidden: true,
            id: 'btn_add',
            handler: function () {
                addRole();
            }
        }, "-", {
            text: buttonI18n['edit'],
            id: 'btn_edit',
            iconCls: 'icon-edit',
            handler: function () {
                editRole1();
            }
        }, "-", {
            text: buttonI18n['delete'],
            hide: true,
            id: 'btn_remove',
            iconCls: 'icon-remove',
            handler: function () {
                delRole();
            }
        }, "-", {
            text: buttonI18n['assignAuthority'],
            hide: true,
            iconCls: 'icon-ok',
            id: 'btn_assignAuthority',
            handler: function () {
                permissions(1, "权限分配");
            }
        }, "-", {
            text: buttonI18n['distrIndex'],
            hide: true,
            id: 'btn_distr',
            iconCls: 'icon-plan-copy',
            handler: function () {
                distrIndex();
            }
        }, "-", {
            text: buttonI18n['reportIndex'],
            hide: true,
            id: 'btn_report',
            iconCls: 'icon-lock',
            handler: function () {
                ReportIndex();
            }
        }, "-", {
            text: '小程序菜单管理',
            hide: true,
            id: 'btn_weichar_menu',
            iconCls: 'icon-ok',
            handler: function () {
                permissions(2, "小程序权限分配");
            }
        }

        ],
        onBeforeLoad: function (param) {
        	displayButtonByUserRole('角色管理');
        },
        onCheck: function (rowIndex, field, value) {
            $('div.datagrid-toolbar a').eq(2).linkbutton('enable');
            $('div.datagrid-toolbar a').eq(3).linkbutton('enable');
            $('div.datagrid-toolbar a').eq(1).linkbutton('enable');
            $('div.datagrid-toolbar a').eq(4).linkbutton('enable');
            $('div.datagrid-toolbar a').eq(5).linkbutton('enable');
        },
    });
}

function permissions(type, title) {
    var row = $('#tb').datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    var id = row.id;
    var permissionsDialog = openDialog('permissionsDialog', g.ctx + 'pc/sys/role/permissions?type=' + type + '&id=' + id, {
            title: title,
            width: '85%',
            height: '80%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                permissionsDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.save,
                    handler: function () {
                        topWin.frames['permissionsDialog'].save(function () {
                            permissionsDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        permissionsDialog.dialog('close');
                    }
                }
            ]
        }
    );
}

/**
 * 分配角色的待办及提醒指标
 */
function distrIndex() {
    var row = $('#tb').datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    var dealtConfigDialog = openDialog('dealtConfigDialog', g.ctx + 'pc/sys/role/dealtConfig?id=' + row.id, {
            title: "分配指标项",
            width: '70%',
            height: '90%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                dealtConfigDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.save,
                    handler: function () {
                        topWin.frames['dealtConfigDialog'].submitForm(function () {
                            dealtConfigDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        dealtConfigDialog.dialog('close');
                    }
                }
            ]
        }
    );
}

function ReportIndex() {
    var row = $('#tb').datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    var id = row.id;

    pageii = parent.layer.open({
        title: "分配报表项",
        type: 2,
        scrollbar: false,
        area: ["900px;", "100%"],
        content: '/reportRole/report/index?id=' + id
    });
}


function addRole() {
    var roleAddDialog = openDialog('roleAddDialog', g.ctx + 'pc/sys/role/add', {
            title: "新增",
            width: '350px',
            height: '320px',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                roleAddDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.save,
                    handler: function () {
                        topWin.frames['roleAddDialog'].save(function () {
                            roleAddDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        roleAddDialog.dialog('close');
                    }
                }
            ]
        }
    );
}


function editRole1() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox == null || cbox == undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var roleEditDialog = openDialog('roleEditDialog', g.ctx + 'pc/sys/role/edit?id=' + cbox.id, {
            title: "编辑",
            width: '350px',
            height: '320px',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                roleEditDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.save,
                    handler: function () {
                        topWin.frames['roleEditDialog'].save(function () {
                            roleEditDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        roleEditDialog.dialog('close');
                    }
                }
            ]
        }
    );
}

function delRole() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox == null || cbox == undefined) {
        layer.msg("请选中一条记录");
        return;
    }
    if (cbox.rolekey == 'purchaser' || cbox.rolekey == 'supplier' || cbox.rolekey == 'admin' || cbox.rolekey == 'bid_expert') {
        layer.msg("内置角色不能删除");
        return;
    }
    var id = cbox.id;

    parent.layer.confirm('是否删除？', function (index) {
       // var url = '/role/deleteRole.do';
        var url = '/pc/sys/role/remove?id=' + id;
        $.ajax({
            url: url,
            method: "post",
            data: {roleId: id},
            success: function (data) {
                debugger
                console.log('data'+data);
                console.log(data);
                if (data == "") {
                    $('#tb').datagrid('load');
                    parent.layer.alert("删除成功！", function () {
                        parent.layer.closeAll();
                    });
                } else {
                    parent.layer.alert("提交失败!");
                }
            },
            error: function (data) {
                console.log(data);
                alert(data);
            }
        });
    });
}
