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

function loadGrid() {
    $('#tb').datagrid({
        iconCls: 'icon-list',
        method: "GET",
        height: gridheight + 50,
        url: g.ctx + 'pc/sys/dict/itemList?dictType=' + $('#id').val(),
        singleSelect: true,
        pagination: true,
        idField: 'item_id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        striped: false,
        rownumbers: false,
        fitColumns: false,
        remoteSort: true,
        page: 1,
        pageSize: 10,
        queryParams: {
            page: 1,
            rows: 10
        },
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                title: 'id',
                width: '10%',
                align: 'left',
                hidden: true

            },
            {
                field: 'name',
                title: '名称',
                width: '15%',
                align: 'left'

            },
            {
                field: 'enName',
                title: '英文名称',
                width: '15%',
                align: 'left'

            },
            {
                field: 'code',
                title: '字典值',
                width: '10%',
                align: 'left'

            },
            {
                field: 'sort',
                title: '排序',
                width: '10%',
                align: 'left'

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

            }
        ]],
        toolbar: [{
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                addDicItem();
            }
        }, "-", {
            text: '编辑',
            iconCls: 'icon-remove',
            handler: function () {
                editDicItem();
            }
        }, "-", {
            text: '导出',
            iconCls: 'icon-download',
            handler: function () {
                var params = searchParams();
                openExportDialog('pc/sys/dict/export?dictType=' + $('#id').val(),'tb',params);
            }
            // handler: function () {
            //     exportDicItem();
            // }
        },
            "-", {
                text: '导入',
                iconCls: 'icon-import',
                handler: function () {
                    importDicItem();
                }
            }, "-", {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    delDicItem();
                }
            }
        ]
    });
}

/**
 *导入数据，刷新当前页面
 */
window.top.aa = {
    "refreshSpDict": function () {
        $('#tb').datagrid('load');
    }
};

/**
 * 导入
 */
function importDicItem() {
    var a = openImportExcelDialog("static/excel/dict_import_temp.xlsx", '字典数据导入', function (file) {
        let formData = new FormData();
        formData.append("file", file);
        formData.append("dictTypeId", $('#id').val())
        //取到fileId然后进行数据导入操作
        $.ajax({
            url: g.ctx + 'pc/sys/dict/item/importData',
            method: "POST",
            contentType: false,
            processData: false,
            data: formData,
            success: function (data) {
                topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    a.dialog('close');
                    $('#tb').datagrid('reload');
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}

// 新增
function addDicItem() {
    addOrEdit(0, 0)
}

function delDicItem() {
    var cbox = $('#tb').datagrid('getSelected')
    if (cbox == null || cbox == undefined) {
        topWin.layer.msg("请选中一条记录");
        return;
    }
    topWin.layer.confirm('确定要删除记录吗?', {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/sys/dict/item/remove?id=' + cbox.id,
            method: "post",
            success: function (data) {
                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});
            }
        });
    });

}

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

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