/**
 *
 * @author BOGON
 * @version 1.0
 */

var page = {
    initDatagrid: function () {
        $('#tb').datagrid({
            iconCls: 'icon-list',
            method: "GET",
            height: gridheight + 20,
            width: gridwidth,
            url: g.ctx + 'pc/sys/printTemp/rest/list',
            singleSelect: true,
            pagination: true,
            idField: 'id',
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            remoteSort: true,
            page: 1,
            pageSize: 10,
            queryParams: {
                page: 1,
                rows: 10
            },
            onLoadSuccess: function () {
            },
            columns: [[
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'code',
                    title: '编号',
                    width: '15%',
                    align: 'center'

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

                },
                {
                    field: 'remark',
                    title: '备注',
                    width: '15%',
                    align: 'center'

                },
                {
                    field: 'create',
                    title: '创建人',
                    width: '15%',
                    align: 'center'

                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: '150',
                    align: 'center'

                },
                {
                    field: 'update',
                    title: '修改人',
                    width: '15%',
                    align: 'center'

                },
                {
                    field: 'updateTime',
                    title: '修改时间',
                    width: '150',
                    align: 'center'

                },
            ]],
            toolbar: [
                {
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        page.detail(0);
                    }
                }, {
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function () {
                        let row = $("#tb").datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.detail(row.id);
                    }
                }, {
                    text: '设计',
                    iconCls: 'icon-edit',
                    handler: function () {
                        let row = $("#tb").datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.design(row.id);
                    }
                }, {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        let row = $("#tb").datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }

                        topWin.layer.confirm("是否删除", {icon: 0}, function (index) {
                            let id = row.id;
                            $.ajax({
                                url: g.ctx + 'pc/sys/printTemp/rest/del',
                                method: "post",
                                data: {id: id},
                                complete: function () {
                                    topWin.layer.close(index);
                                },
                                success: function (data) {
                                    topWin.layer.msg('操作成功', {icon: 1});
                                    $("#tb").datagrid('reload');
                                },
                                error: function (data) {
                                    topWin.layer.msg(data.responseJSON.message, {icon: 2});
                                }
                            });
                            topWin.layer.close(index);
                        })
                    }
                }]
        });
    },
    detail: function (id) {
        var detailDialog = openDialog('detailDialog', g.ctx + 'pc/sys/printTemp/page/detail?id=' + id, {
            title: id ? "新增" : "编辑",
            width: '400px',
            height: '250px',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                detailDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.confirm,
                    handler: function () {
                        topWin.frames['detailDialog'].page.save(function () {
                            detailDialog.dialog('close');
                            $("#tb").datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        detailDialog.dialog('close');
                    }
                }
            ]
        });
    },
    design: function (id) {
        var designDialog = openDialog('designDialog', g.ctx + 'pc/sys/printTemp/page/design?id=' + id, {
            title: "设计",
            width: '100%',
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                designDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.confirm,
                    handler: function () {
                        topWin.frames['designDialog'].page.save(function () {
                            designDialog.dialog('close');
                            $("#tb").datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        designDialog.dialog('close');
                    }
                }
            ]
        });
    }
}
$(function () {
    page.initDatagrid()
})