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

var page = {
    initDatagrid: function () {
        $('#tb').datagrid({
            iconCls: 'icon-list',
            title: '',
            method: "GET",
            singleSelect: true,
            pagination: true,
            nowrap: true,
            idField: 'id',
            loadMsg: "数据加载中......",
            rownumbers: true,
            striped: true,
            url: g.ctx + 'pc/supplier/qualification/rest/template/list?scene=' + $("#scene").val(),
            columns: [[{field: 'ck', checkbox: true}, {
                field: 'state', title: '状态', width: '60', align: 'left', formatter: function (value) {
                    if (value === 1) {
                        return "<span style='color: green'>正常</span>"
                    }
                    if (value === 2) {
                        return "<span style='color: red'>禁用</span>"
                    }
                }
            }, {
                field: 'code', title: '模板编码', width: '100', align: 'left'
            }, {
                field: 'name', title: '模板名称', width: '200', align: 'left'
            }, {
                field: 'remark', title: '备注', width: '80', 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');
                }
            }]],
            height: $(window).height() - $('#searchForm').outerHeight() - 10,
            width: '100%',
            toolbar: [{
                text: '新增', id: 'btn_add', iconCls: 'icon-add', handler: function () {
                    page.openDetailDialog()
                }
            }, "-", {
                text: '编辑', id: 'btn_edit', iconCls: 'icon-edit', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    if (row.state !== 1 && row.state !== 4) {
                        layer.msg("只有新建和未通过状态可以编辑");
                        return;
                    }
                    page.openDetailDialog(row.id)
                }
            }, "-", {
                text: '复制', iconCls: 'icon-add', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    page.openDetailDialog(row.id, 1)
                }
            }, "-", {
                text: '启用|禁用', iconCls: 'icon-remove', handler: function () {
                    var row = $('#tb').datagrid('getSelected');
                    if (!row) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    var state = row.state;
                    var msg = "是否修改状态?";
                    var new_state = 1;
                    if (state === 1) {
                        msg = "是否禁用?";
                        new_state = 2;
                    }
                    if (state === 2) {
                        msg = "是否启用?";
                    }
                    topWin.layer.confirm(msg,{icon: 3}, function (index) {
                        $.ajax({
                            url: g.ctx + "pc/supplier/qualification/rest/template/updateState",
                            data: {id: row.id, state: new_state},
                            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});
                            }
                        })
                    })
                }
            }]
        });
    }, openDetailDialog: function (id, copyFlag) {
        let title = '';
        if (id == undefined) {
            title = '新增'
        } else if (copyFlag == undefined) {
            title = '编辑'
        } else {
            title = '复制'
        }
        let url = g.ctx + 'pc/supplier/qualification/template/detail?scene=' + $("#scene").val()
            + '&id=' + (id == undefined ? '' : id)
            + '&copyFlag=' + (copyFlag == undefined ? 0 : 1);
        const qualificationTemplateDetailDialog = openDialog('qualificationTemplateDetailDialog', url, {
            title: title,
            width: '90%',
            height: '90%',
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                qualificationTemplateDetailDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.save, handler: function () {
                    topWin.frames['qualificationTemplateDetailDialog'].page.saveOrUpdate(function () {
                        qualificationTemplateDetailDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    qualificationTemplateDetailDialog.dialog('close');
                }
            }]
        });
    }
}
$(function () {
    page.initDatagrid()
})