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

var page = {
    initData: function () {
        $("#tb").datagrid({
            height: document.documentElement.clientHeight - $("#searchForm").outerHeight(),
            url: g.ctx + 'pc/supplier/template/rest/list',
            method: "GET",
            singleSelect: true,
            pagination: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 10,
            queryParams: {},
            columns: [[
                {
                    field: 'ck',
                    checkbox: 'true'
                },
                {
                    field: 'templateCode',
                    title: '模板编码',
                    width: '180',
                    align: 'left'
                },
                {
                    field: 'templateName',
                    title: '模板名称',
                    width: '25%',
                    align: 'left'
                },
                {
                    field: 'createName', title: '创建人', width: '100', align: 'left'
                }, {
                    field: 'createTime', title: '创建时间', width: '150', align: 'left', formatter: function (value) {
                        if (!value) {
                            return;
                        }
                        return moment(new Date(value)).format('YYYY-MM-DD HH:mm:ss');
                    }
                }
            ]],
            toolbar: [
                {
                    text: '新增',
                    id: 'btn-add',
                    iconCls: 'icon-add',
                    handler: function () {
                        page.detailDialog();
                    }
                }, {
                    text: '编辑',
                    id: 'btn-edit',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.detailDialog(row[0].id);
                    }
                }, {
                    text: '删除',
                    id: 'btn-remove',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        topWin.layer.confirm(tipsi18n.sfsc, {icon: 3}, function (index) {
                            $.ajax({
                                url: g.ctx + "pc/supplier/template/rest/remove",
                                data: {id: row[0].id},
                                type: "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});
                                }
                            })
                        })
                    }
                }, {
                    text: '预览',
                    id: 'btn-look',
                    iconCls: 'icon-large-smartart',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.invitationPreview(row[0].id);
                    }
                }, {
                    text: '复制',
                    id: 'btn-copy',
                    iconCls: 'icon-plan-copy',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.detailDialog(row[0].id, true);
                    }
                },
            ]
        });
    },
    invitationPreview: function (id) {
        //这个地方特殊情况需要使用layer的弹框
        parent.layer.open({
            title: '预览',
            type: 2,
            scrollbar: false,
            area: ['100%', '100%'],
            content: g.ctx + 'pc/supplier/register?templateId=' + id + "&type=1",
            success: function (layero, index) {
                let body = parent.layer.getChildFrame('body', index);
                let iframeWin = parent.window[layero.find('iframe')[0]['name']];

                let btnDown = body.find('.stepDown-btn');
                let nextBtn = '<button type="button" class="layui-btn layui-btn-lg layui-btn-normal next_btn">下一步</button>';
                $(btnDown[0]).after(nextBtn);
                $(btnDown[1]).after(nextBtn);
                $(btnDown[2]).after(nextBtn);
                $(btnDown[0]).hide();
                $(btnDown[1]).hide();
                $(btnDown[2]).hide();
                body.find('.next_btn').bind('click', function () {
                    iframeWin.loadLayTab(++iframeWin.getSelfData()['indexPart'], 1);
                });
            }
        })
    },
    detailDialog: function (id, copyFlag) {
        let title = ''
        if (id != undefined && copyFlag != undefined) {
            title = '复制'
        } else if (id != undefined) {
            title = '编辑'
        } else {
            title = '新增'
        }
        let url = g.ctx + 'pc/supplier/template/page/detail?copyFlag='
            + (copyFlag == undefined ? '0' : '1')
            + '&id=' + (id == undefined ? '' : id);
        var templateEditDialog = openDialog('templateEditDialog', url, {
            title: title,
            width: screenWidth * 0.8 + "px",
            height: screenHeight + "px",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                templateEditDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.save, handler: function () {
                    topWin.frames['templateEditDialog'].saveOrUpdate(function () {
                        templateEditDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    templateEditDialog.dialog('close');
                }
            }]
        });
    },
    getChecked: function (call) {
        var row = $('#tb').datagrid('getChecked');
        if (row.length != 1) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        call(row[0].id)
    }
}
$(function () {
    page.initData()
})