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


function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + 'pc/sys/role/list?type=1',
        method: "GET",
        iconCls: 'icon-list',
        height: gridheight + 20,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        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: 'supplierShow',
                title: '对外展示',
                width: '140',
                align: 'left',
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return '是';
                        case 1:
                            return '否'
                    }
                }
            },
            {
                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();
                }
            }

        ],
        // 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');
        },
    });
}


function permissions() {
    var row = $('#tb').datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    var id = row.id;
    var permissionsDialog = openDialog('permissionsDialog', g.ctx + 'pc/sys/role/permissions?id=' + id, {
            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 addRole() {
    var roleAddDialog = openDialog('roleAddDialog', g.ctx + 'pc/sys/role/add?supplierFlag=1', {
            title: "新增",
            width: '360px',
            height: '350px',
            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?supplierFlag=1&id=' + cbox.id, {
            title: "编辑",
            width: '360px',
            height: '350px',
            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) {
        topWin.layer.msg("请选中一条记录");
        return;
    }
    if (cbox.rolekey == 'purchaser' || cbox.rolekey == 'supplier' || cbox.rolekey == 'admin' || cbox.rolekey == 'bid_expert') {
        topWin.layer.msg("内置角色不能删除");
        return;
    }
    var roleId = cbox.id;

    topWin.layer.confirm('是否删除？', function (index) {
        $.ajax({
            url: g.ctx + 'pc/sys/role/remove',
            type: "POST",
            data: {id: roleId},
            success: function (data) {
                topWin.layer.close(index);
                topWin.layer.alert("删除成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    $('#tb').datagrid('reload');
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}
