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


function loadGrid() {
    $('#tb').datagrid({
        iconCls: 'icon-list',
        title: '',
        method: "GET",
        height: gridheight + 20,
        width: gridwidth,
        url: g.ctx + 'pc/sys/dict/list',
        singleSelect: true,
        pagination: true,
        nowrap: true,
        idField: 'id',
        loadMsg: "数据加载中......",
        rownumbers: true,
        striped: true,
        page: 1,
        pageSize: 10,
        queryParams: {
            page: 1,
            rows: 10
        },
        onLoadSuccess: function () {
            $('#tb').datagrid("selectRow", -1);
        },
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'name',
                title: '字典类型名称',
                width: '15%',
                align: 'left'

            },
            {
                field: 'code',
                title: '字典类型值',
                width: '15%',
                align: 'left'

            },
            {
                field: 'remark',
                title: '备注',
                width: '45%',
                align: 'left'
            },
            {
                field: 'updateTime',
                title: '最后修改时间 ',
                width: '15%',
                align: 'left'

            },
            {
                field: 'id',
                title: '操作(双击)',
                width: '9%',
                align: 'left',
                // hidden :true
                formatter: function (value, row, index) {
                    return '<a href="javascript:void(0)" onclick=lookupDicItem(' + value + ',' + row + ') >查看字典</a>';
                },
            }


        ]],
        onDblClickRow: function (rowIndex, rowData) {
            lookupDicItem1();
        },
        toolbar: ["-", {
            text: '新增',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                addOrEdit(0, 0);
            }
        }, "-", {
            text: '编辑',
            hide: true,
            iconCls: 'icon-remove',
            handler: function () {
                editDicType();
            }
        }
            , "-", {
                text: '维护字典条目',
                hide: true,
                iconCls: 'icon-add',
                handler: function () {
                    lookupDicItem1();
                }
            }
        ]
    });
}

function delDicType() {
    var cbox = $('#tb').datagrid('getSelected')
    if (cbox == null || cbox == undefined) {
        layer.msg("请选中一条记录");
        return;
    }
    layer.confirm('确定要删除记录吗?', function (index) {
        $.ajax({
            url: '/dic/delDicType.do?dict_type_id=' + cbox.dict_type_id,
            method: "post",
            success: function (data) {
                if (data == '1') {
                    layer.confirm('删除成功!要刷新当前窗口?', function (index) {
                        $('#tb').datagrid('load')
                        layer.close(index);
                        return false;
                    });
                } else if (data == '2') {
                    layer.msg("字典类型有对应条目存在不能删除！");
                    return;
                } else {
                    alert("提交失败!");
                }
            },
            error: function (data) {
                console.log(data);
                alert(data);
            }
        });
    });
}


function editDicType() {
    var cbox = $('#tb').datagrid('getSelected')
    if (cbox == null || cbox == undefined) {
        topWin.layer.msg("请选中一条记录");
        return;
    }
    var value = cbox.id
    if (value == null || value == undefined || value == 'null' || value == 'undefined') {
        topWin.layer.msg("请选中一条记录");
        return;
    }
    addOrEdit(value, 1);
}

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

    var dictItemListDialog = openDialog('dictItemListDialog', g.ctx + 'pc/sys/dict/editItemList?dictTypeId=' + value, {
            title: "字典类型条目",
            width: '900',
            height: '90%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                dictItemListDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.close,
                    handler: function () {
                        dictItemListDialog.dialog('close');
                    }
                }
            ]
        }
    );
}

// 查看字典
function lookupDicItem(value, row) {
    var dictItemListDialog = openDialog('dictItemListDialog', g.ctx + 'pc/sys/dict/editItemList?dictTypeId=' + row.id, {
            title: "字典类型条目",
            width: '900',
            height: '90%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                dictItemListDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.close,
                    handler: function () {
                        dictItemListDialog.dialog('close');
                    }
                }
            ]
        }
    );
}

function addOrEdit(id, type) {
    var dictEditDialog = openDialog('dictEditDialog', g.ctx + 'pc/sys/dict/addOrEdit?id=' + id, {
            title: (type == 1 ? "编辑" : "新增") + "字典类型",
            width: '350px',
            height: '320px',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                dictEditDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.save,
                    handler: function () {
                        topWin.frames['dictEditDialog'].page.save(function () {
                            dictEditDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        dictEditDialog.dialog('close');
                    }
                }
            ]
        }
    );

}